为什么负数numpy.float64用小数取幂会产生nan? [英] Why do negative numpy.float64 yield nan when expontiated with a fractional number?
问题描述
以分数(即小数,即有理数)取幂的负 numpy.float64
会产生 nan
结果和警告.
A negative numpy.float64
exponentiated with a fractional (i.e., decimal, i.e., rational) number will yield a nan
result and a warning.
使用Python的 float
类型的相同数字将返回 complex
结果.
The same number using Python's float
type returns a complex
result.
这是使用Python 3.6.6的最小示例(有关Python 2.7.15的评论,请参见下文):
Here is a minimal example using Python 3.6.6 (for a comment on Python 2.7.15, see below):
>>> import numpy as np
>>> f = -2.0
>>> npf = np.float64(-2.0)
>>> f**1.1
(-2.0386342710747223-0.6623924280875919j)
>>> npf ** 1.1
__main__:1: RuntimeWarning: invalid value encountered in double_scalars
nan
我尝试了 numpy.power
函数,获得了不同的警告和相同的结果.
I tried the numpy.power
function getting a different warning and the same result.
>>> np.power(f, 1.1)
__main__:1: RuntimeWarning: invalid value encountered in power
nan
>>> np.power(npf, 1.1)
nan
后者中的警告仅在首先执行任何操作之后出现.
The warning in the latter appears only after whatever is executed first.
我使用浮点数的 numpy.array
s遇到了这个问题,在其他所有情况下(?)的行为都与Python浮点数相同.从 float
到 numpy.float64
的转换是隐式发生的,因此花了我一段时间才找到问题的根源.
I ran into this using numpy.array
s of floats, which in all other cases(?) just behave the same as Python floats. The conversion from float
to numpy.float64
happens implicitly so it took me a while to find the source of the problem.
现在,我可以通过在将数组创建为 numpy.complex
时显式转换为或指定 dtype
来解决此问题:
Now, I can get around this by explicitly converting to or specifying the dtype
when creating the array as numpy.complex
:
>>> npc = np.complex(-2.0)
>>> npc ** 1.1
(-2.0386342710747223-0.6623924280875919j)
>>> np.power(npc, 1.1)
(-2.0386342710747223-0.66239242808759191j)
(请注意输出O_o的精度不同,不过我可以接受)
(note the different precision of the output O_o, I can live with that, though)
我的问题是:为什么?为什么在必要时numpy不返回 numpy.complex
.例如,当分割 numpy.int64
时,它会执行转换为 numpy.float64
:
My question is: Why?? Why doesn't numpy return a numpy.complex
when necessary. It does convert to numpy.float64
when, e.g., dividing a numpy.int64
:
>>> ai = np.array([1])
>>> ai.dtype
dtype('int64')
>>> ai/2
array([ 0.5])
>>> (ai/2).dtype
dtype('float64')
当 numpy.float64
无法表达计算结果并改用 numpy.complex64
时,为什么不采用相同的原理?
Why not apply the same philosophy when numpy.float64
is not capable of expressing the result of a calculation and use numpy.complex64
instead?
在python 2.7.15上的注释::在此版本中,用小数对 float
求幂会引发异常,明确地使用 complex
解决问题:
comment on Python 2.7.15: with this version, exponentiating a float
with a fractional number throws an exception, explicitly using complex
solves the problem:
>>> f ** 1.1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: negative number cannot be raised to a fractional power
>>> complex(-2.0) ** 1.1
(-2.0386342710747223-0.6623924280875919j)
这等效于numpy的行为.
This is equivalent to the behaviour of numpy.
推荐答案
也许numpy开发人员只是没有想到要涵盖这种情况.您可以在 Github 上提出问题.否则,您只需要显式地进行 complex
转换.最好放这个问题,以便numpy开发人员可以解决这个问题.
Probably the numpy developers just did not think to cover this case. You could put up an issue on Github. Otherwise, you will just have to do the complex
conversion explicitly. It would be good to put in the issue so that the numpy developers can work on it.
这篇关于为什么负数numpy.float64用小数取幂会产生nan?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!