Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy number array to strings with trailing zeros removed

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!

like image 624
user1269942 Avatar asked Nov 01 '22 18:11

user1269942


1 Answers

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.

like image 56
user1269942 Avatar answered Nov 09 '22 14:11

user1269942