NumPy,RuntimeWarning:电源中遇到无效值

dohp0rv5  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(82)

我正在使用Python 3.6。
我真的很困惑,为什么会这样?

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
qxsslcnc

qxsslcnc1#

Numpy似乎不允许负数的分数幂,即使幂不会导致复数。(其实我也有同样的问题,今天早些时候,无关)。一种解决方法是使用

np.sign(a) * (np.abs(a)) ** (1 / 3)
roejwanj

roejwanj2#

将dtype更改为复数

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

当你处理负数的根时,问题就出现了。

相关问题