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\'
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.