Negative exponent with NumPy array operand
Which version of python are you using? Perfectly works for me in Python 2.6, 2.7 and 3.2:
>>> 3**-3 == 1.0/3**3True
and with numpy 1.6.1:
>>> import numpy as np>>> arr = np.array([1,2,3,4,5], dtype='float32')>>> arr**-3 == 1/arr**3array([ True, True, True, True, True], dtype=bool)
It may be a Python 3 thing as I'm using 3.5.1 and I believe this is the error you have...
for c in np.arange(-5, 5): print(10 ** c)---------------------------------------------------------------------------ValueError Traceback (most recent call last)<ipython-input-79-7232b8da64c7> in <module>() 1 for c in np.arange(-5, 5):----> 2 print(10 ** c)ValueError: Integers to negative integer powers are not allowed.
Just change it to a float and it'll should work.
for c in np.arange(-5, 5): print(10 ** float(c))1e-050.00010.0010.010.11.010.0100.01000.010000.0
oddly enough, it works in base python 3:
for i in range(-5, 5): print(10 ** i)1e-050.00010.0010.010.1110100100010000
it seemed to work just fine for Python 2.7.12:
Python 2.7.12 (default, Oct 11 2016, 05:24:00) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwinType "help", "copyright", "credits" or "license" for more information.>>> import numpy as np>>> for c in np.arange(-5, 5):... print(10 ** c)... 1e-050.00010.0010.010.1110100100010000
Perhaps use the NumPy/SciPy built-in, power
>>> import numpy as NP>>> A = 10*NP.random.rand(12).reshape(4, 3)>>> A array([[ 5.7 , 5.05, 7.28], [ 3.61, 9.67, 6.27], [ 5.29, 2.8 , 0.58], [ 5.94, 4.9 , 1.68]])>>> NP.power(A, -2) array([[ 0.03, 0.04, 0.02], [ 0.08, 0.01, 0.03], [ 0.04, 0.13, 2.98], [ 0.03, 0.04, 0.35]])