在电源中遇到无效的值

在电源中遇到无效的值

本文介绍了NumPy,RuntimeWarning:在电源中遇到无效的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am working with Python 3.6.
I am really confused, why this happened ?

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

推荐答案

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

Numpy does not seem to allow fractional powers of negative numbers, even if the power would not result in a complex number. (I actually had this same problem earlier today, unrelatedly). One workaround is to use

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

这篇关于NumPy,RuntimeWarning:在电源中遇到无效的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 15:29