NumPy, RuntimeWarning: invalid value detected at power

I am working with Python 3.6.
I'm really confused, why did this happen?

In [1]: import numpy as np

In [2]: a = np.array(-1)

In [3]: a
Out[3]: array(-1)

In [4]: a ** (1/3)
/Users/wonderful/anaconda/bin/ipython:1: RuntimeWarning: invalid        value encountered in power
  #!/Users/wonderful/anaconda/bin/python
Out[4]: nan
+3
source share
2 answers

Numpy does not seem to allow fractional powers of negative numbers, even if the power does not lead to a complex number. (I actually had the same problem earlier today, unrelated). One way is to use

np.sign(a) * (np.abs(a)) ** (1 / 3)
+6
source

change dtype to complex numbers

a = np.array(-1, dtype=np.complex)

The problem arises when you work with the roots of negative numbers.

+4
source

Source: https://habr.com/ru/post/1694240/


All Articles