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?
We can easily format to raw decimal places with "%f" which is same as "%. 6f" by default. "%f" % 8.99284722486562e-05 produces '0.000090' . "%f" % 0.01 produces '0.010000' .
Convert scientific notation to decimal formDetermine the exponent, n , on the factor 10 . Move the decimal n places, adding zeros if needed. If the exponent is positive, move the decimal point n places to the right. If the exponent is negative, move the decimal point |n| places to the left.
Use numpy. set_printoptions() to print an array without scientific notation. Call set_printoptions(suppress=True) to suppress scientific notation when printing.
Summary: Use the string literal syntax f"{number:. nf}" to suppress the scientific notation of a number to its floating-point representation.
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).
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:
In [1]: import numpy as np
In [2]: x = 1e9 * np.random.random(5)
In [3]: x
Out[3]:
array([ 4.96602724e+08, 5.42486095e+08, 4.74495681e+08,
7.37709684e+07, 9.75410927e+08])
In [4]: np.set_printoptions(formatter={'float_kind':'{:f}'.format})
In [5]: x
Out[5]:
array([496602723.824146, 542486095.316912, 474495680.688025,
73770968.413642, 975410926.873148])
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:
In [6]: x[0]
Out[6]: 496602723.82414573
In [7]: x[0] * 2
Out[7]: 993205447.64829147
string
sNow let's say we had converted them to a list of strings:
In [1]: import numpy as np
In [2]: x = 1e9 * np.random.random(5)
In [3]: x
Out[3]:
array([ 2.56619581e+08, 2.55721261e+08, 3.36984986e+08,
2.67541556e+08, 9.01048842e+08])
In [4]: x = ['{:f}'.format(item) for item in x]
In [5]: x
Out[5]:
['256619580.697790',
'255721261.271977',
'336984986.430552',
'267541556.373619',
'901048842.193849']
Now they're a list
of string
s. If we operate on them mathematically, they'll behave like strings, not numbers:
In [6]: x[0] * 2
Out[6]: '256619580.697790256619580.697790'
numpy
arrays are saved with savetxt
Finally, if you're using numpy.savetxt
, and would like to control how the data is output to disk, consider using the fmt
parameter instead of manually converting elements of the array to strings.
For example, if we were to do:
np.savetxt('temp.txt', x)
By default, the ascii representation of the array would use scientific notation if it is more compact:
8.702970453168644905e+08
9.991634082796489000e+08
5.032002956810175180e+08
2.382398232565869987e+08
1.868727085152311921e+08
However, we can control that using fmt
. Note that it expects the "old-style" %
formatting strings:
np.savetxt('temp2.txt', x, fmt='%f')
And we'll get:
870297045.316864
999163408.279649
503200295.681018
238239823.256587
186872708.515231
If you just want to print them without using scientific notation, you can do np.set_printoptions(suppress=True)
.
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