question: is my method of converting a numpy array of numbers to a numpy array of strings with specific number of decimal places AND trailing zeros removed the 'best' way?
import numpy as np
x = np.array([1.12345, 1.2, 0.1, 0, 1.230000])
print np.core.defchararray.rstrip(np.char.mod('%.4f', x), '0')
outputs:
['1.1235' '1.2' '0.1' '0.' '1.23']
which is the desired result. (I am OK with the rounding issue)
Both of the functions 'rstrip' and 'mod' are numpy functions which means this is fast but is there a way to accomplish this with ONE built in numpy function? (ie. does 'mod' have an option that I couldn't find?) It would save the overhead of returning copies twice which for very large arrays is slow-ish.
thanks!
Thanks to Warren Weckesser for providing valuable comments. Credit to him.
I converted my code to use:
formatter = '%d'
if num_type == 'float':
formatter = '%%.%df' % decimals
np.savetxt(out, arr, fmt=formatter)
where out is a file handle to which I had already written my headers. Alternatively, I could also use the headers=
argument in np.savetxt
. I have no clue how I didn't see those options in the documentation.
For a numpy array 1300 by 1300, creating the line by line output as I did before (using np.core.defchararray.rstrip(np.char.mod('%.4f', x), '0')
) took ~1.7 seconds and using np.savetxt
takes 0.48 seconds.
So np.savetxt
is a cleaner, more readable, and faster solution.
Note: I did try:
np.savetxt(out, arr, fmt='%.4g')
in an effort to not have a switch based on number type but it did not work as I had hoped.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With