Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the exponent of each number in a np.array?

Tags:

python

numpy

Lets say I have the array:

x = np.array([0.00001,0.001])

numpy will make the numbers to

array([  1.00000000e-05,   1.00000000e-03])

Now I want to get the exponents, something like

x.get_exponent()

with result

[-5,-3]
like image 720
varantir Avatar asked Jan 24 '13 19:01

varantir


2 Answers

You can use np.floor(np.log10(np.abs(x))).

For example:

In [13]: x = np.array([0.00001, -0.001, 0.0000025, 0.09, -13.25, 9876.5])

In [14]: x
Out[14]: 
array([  1.00000000e-05,  -1.00000000e-03,   2.50000000e-06,
         9.00000000e-02,  -1.32500000e+01,   9.87650000e+03])

In [15]: np.floor(np.log10(np.abs(x))).astype(int)
Out[15]: array([-5, -3, -6, -2,  1,  3])
like image 114
Warren Weckesser Avatar answered Sep 30 '22 01:09

Warren Weckesser


Use log10

In [49]: x = np.array([0.00001,0.001])
In [50]: np.log10(x)
Out[50]: array([-5., -3.])
like image 37
tzelleke Avatar answered Sep 30 '22 00:09

tzelleke