NumPy has log
, log2
, and log10
methods which can perform vectorized log base e / 2 / 10 (respectively). However, for doing the inverse operation (exponentiation), I only see exp
. Why is there no exp2
/ exp10
/ etc?
I've tried using np.power(10, nums)
, but it won't let me raise to a negative power. 10 ** nums
does not work either.
It should work fine with 10 ** nums
provided you use the float
dtype. Otherwise it will create an integer array:
>>> a = numpy.array([-1, 0, 1, 2, 3], dtype=int)
>>> 2 ** a
array([0, 1, 2, 4, 8])
>>> 10 ** a
array([ 0, 1, 10, 100, 1000])
>>> a = numpy.array([-1, 0, 1, 2, 3], dtype=float)
>>> 10 ** a
array([ 1.00000000e-01, 1.00000000e+00, 1.00000000e+01,
1.00000000e+02, 1.00000000e+03])
You can also coerce to float
by using 10.0
instead of 10
:
>>> a = numpy.array([-1, 0, 1, 2, 3], dtype=int)
>>> 10.0 ** a
array([ 1.00000000e-01, 1.00000000e+00, 1.00000000e+01,
1.00000000e+02, 1.00000000e+03])
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