I have a numpy array come of whose elements are in scientific format and I want to convert them into decimal format. My numpy array looks like this:
[array([ 93495052.96955582, 98555123.06146193])]
[array([ 1.00097681e+09, 9.98276347e+08])]
[array([ 6.86812785e+09, 6.90391125e+09])]
[array([ 7.75127468e+08, 8.02369833e+08])]
and this is formed using this line in my code:
list1.append(np.array(regr.predict(data),dtype = np.float))
Now I want to convert elements in list1
from scientific format to decimal format. I looked around for some solution and found out that print format(0.00001357, 'f')
converts numbers from scientific format to decimal format but how do I use it to convert elements of my array?
Best Answer
First off, as several people have noted, there's a very large difference between how the numbers are displayed and how they're stored.
If you want to convert them to strings, then use
'{:f}'.format(x)
(or the%
equivalent).However, it sounds like you're only wanting the numbers to be displayed differently when you're working interactively (or through a
print
statement).Changing how
numpy
arrays are printedThe way that numpy arrays are displayed interactively is controlled by
numpy.set_printoptions
.Note that this does not convert the numbers to strings or change them in any way.
As a quick example:
We've only changed how
numpy
will display the numbers. They're still floats.We can operate on them mathematically, and they'll behave like numbers:
Converting to
string
sNow let's say we had converted them to a list of strings:
Now they're a
list
ofstring
s. If we operate on them mathematically, they'll behave like strings, not numbers:Controlling how
numpy
arrays are saved withsavetxt
Finally, if you're using
numpy.savetxt
, and would like to control how the data is output to disk, consider using thefmt
parameter instead of manually converting elements of the array to strings.For example, if we were to do:
By default, the ascii representation of the array would use scientific notation if it is more compact:
However, we can control that using
fmt
. Note that it expects the "old-style"%
formatting strings:And we'll get: