Python * 不支持的操作数类型:“numpy.ndarray”和“numpy.float64”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14408122/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:19:52  来源:igfitidea点击:

unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.float64'

pythonnumpyoperand

提问by Jason

long time reader, first time writer.

长期读者,第一次写作。

I searched around on google and stack overflow, but wasn't really able to find a general answer to this question.

我在谷歌和堆栈溢出上四处搜索,但并没有真正找到这个问题的一般答案。

I am getting an "unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.float64'" error in python 2.7.3 using numpy 1.6.2.

我在使用 numpy 1.6.2 的 python 2.7.3 中收到“*不支持的操作数类型:'numpy.ndarray'和'numpy.float64'”错误。

The error comes from multiplying a numpy array and a numpy float, but it doesn't happen every time.

错误来自于将一个 numpy 数组和一个 numpy 浮点数相乘,但并非每次都会发生。

For example:

例如:

x = np.tan(1) # numpy.float64
y = np.array([0,1,2,3]) # numpy.ndarray
np.multiply(x,y) # works no problem

Or

或者

x = np.tan(np.abs(np.multiply(-31,41)))  # numpy.float64
y = np.square(np.add(np.divide(np.zeros(100),42),(-27)**40)) # numpy.ndarray
np.multiply(x,y) # works no problem

Both work

两者都工作

Now for the problem children:

现在对于问题儿童:

np.multiply(np.square(np.add(np.divide(np.zeros(100),42),-27)**40)),
np.tan(np.abs(np.multiply(-31,41))))

or, with x defined as above:

或者,x 定义如上:

np.multiply(np.square(np.add(np.divide(np.zeros(100),42),(-27)**40)),x)

both produce the error: NotImplemented

两者都会产生错误:NotImplemented

I know the random functions and numbers seem odd, but conceptually this still should work, as it worked when both were set to variables individually.

我知道随机函数和数字看起来很奇怪,但从概念上讲这仍然应该有效,因为当两者都单独设置为变量时它有效。

Why does this happen? How can I fix it in a general sense?

为什么会发生这种情况?我该如何解决一般意义上的问题?

Thanks so much! Jason

非常感谢!杰森

采纳答案by Luke Woodward

I suspect that the problem here is that NumPy cannot store Python longvalues in its arrays. As soon as you try to do this, it switches the data type of the array to object. Arithmetic operations on the array then become trickier because NumPy can no longer do the arithmetic itself.

我怀疑这里的问题是 NumPy 无法long在其数组中存储 Python值。一旦您尝试这样做,它就会将数组的数据类型切换为object。数组上的算术运算变得更加棘手,因为 NumPy 不再能够自己进行算术运算。

>>> np.array(27**40)
array(1797010299914431210413179829509605039731475627537851106401L, dtype=object)
>>> np.array(27**40) * np.tan(1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.float64'

Oddly enough, swapping the order of the arguments can sometimes work:

奇怪的是,交换参数的顺序有时可以工作:

>>> np.tan(1) * np.array(27**40)
2.7986777223711575e+57

In this second case, the type of the result is a Python float, not a NumPy array.

在第二种情况下,结果的类型是 Python float,而不是 NumPy 数组。

The fix is to avoid creating longvalues in NumPy arrays, and use floats instead:

解决方法是避免long在 NumPy 数组中创建值,float而是使用s:

>>> np.array(27.0**40)
array(1.797010299914431e+57)
>>> np.array(27.0**40) * np.tan(1)
2.7986777223711575e+57
>>> np.multiply(np.square(np.add(np.divide(np.zeros(10),42),(-27.0)**40)),np.tan(1))
array([  5.02925269e+114,   5.02925269e+114,   5.02925269e+114,
         5.02925269e+114,   5.02925269e+114,   5.02925269e+114,
         5.02925269e+114,   5.02925269e+114,   5.02925269e+114,
         5.02925269e+114])

If you do get an error like this is the future, the first thing to do is to check the dtypeof the array being multiplied. Does it contain NumPy values or Python objects?

如果您确实遇到这样的错误,那么首先要做的是检查dtype要相乘的数组的 。它是否包含 NumPy 值或 Python 对象?

回答by Stefano M

Is this a quiz? I do not understand why the question is so obfuscated... Everything boils down to this simple fact.

这是测验吗?我不明白为什么这个问题如此模糊......一切都归结为这个简单的事实。

Given

给定的

>>> x = 10**100
>>> type(x)
<type 'long'>
>>> y = np.float64(1)

we have

我们有

>>> y.__mul__(x)
1e+100
>>> y.__rmul__(x)
NotImplemented

This is the bug (or feature, I do not know)since it should be y.__mul__(x) == y.__rmul__(x)(at least for these particular values of xand y).

这是错误 (或功能,我不知道)因为它应该是y.__mul__(x) == y.__rmul__(x)(至少对于这些特定值xy)。

Python longdoes not know how to handle multiplication with numpy.float64(but this is correct.)

Pythonlong不知道如何处理乘法numpy.float64(但这是正确的。)

>>> x.__mul__(y)
NotImplemented
>>> x.__rmul__(y)
NotImplemented

So y*xevaluates to y.__mul__(x)and gives the expected result. On the contrary x*yis first tried as x.__mul__(y)(not implemented, OK) than as y.__rmul__(x)(not implemented but a bug.).

所以y*x评估y.__mul__(x)并给出预期的结果。相反x*y,首先尝试 as x.__mul__(y)(未实现,OK)而不是 as y.__rmul__(x)(未实现,但存在错误。)。

As already pointed out, we can have nd.arraysof arbitrary objects, and everything becomes clear.

正如已经指出的,我们可以拥有nd.arrays任意对象,一切都变得清晰。

Edit

编辑

This bug has been corrected (probably in numpy ver. 1.7):

此错误已得到纠正(可能在 numpy 1.7 版中):

>>> np.version.version
'1.13.1'
>>> x = 10**100
>>> y = np.float64(1)
>>> x.__mul__(y)
NotImplemented
>>> x.__rmul__(y)
NotImplemented
>>> y.__mul__(x)
1e+100
>>> y.__rmul__(x)
1e+100
>>> x*y
1e+100
>>> y*x
1e+100