How to format in numpy savetxt such that zeros are saved only as “0”

前端 未结 3 1180
难免孤独
难免孤独 2020-12-19 10:14

I am saving a numpy sparse array (densed) into a csv. The result is I have a 3GB csv. The problem is 95% of the cells are 0.0000. I used fmt=\'%5.4f\'

3条回答
  •  隐瞒了意图╮
    2020-12-19 10:54

    Another simple option that may work given your requirements is the 'g' specifier. If you care more about significant digits and less about seeing exactly x number of digits and don't mind it switching between scientific and float, this does the trick well. For example:

    np.savetxt("foo.csv", arrayDense, fmt='%5.4g', delimiter=',') 
    

    If arrayDense is this:

    matrix([[ -5.54900000e-01,   0.00000000e+00,   0.00000000e+00],
        [  0.00000000e+00,   3.43560000e-08,   0.00000000e+00],
        [  0.00000000e+00,   0.00000000e+00,   3.43422000e+01]])
    

    Your way would yield:

    -0.5549,0.0000,0.0000
    0.0000,0.0000,0.0000
    0.0000,0.0000,34.3422
    

    The above would yield instead:

    -0.5549,    0,    0
    0,3.436e-08,    0
    0,    0,34.34
    

    This way is also more flexible. Notice that using 'g' instead of 'f', you don't lose data (i.e. 3.4356e-08 instead of 0.0000). This obviously is dependent on what you set your precision to however.

提交回复
热议问题