*:'numpy.ndarray'和'numpy.float64'不受支持的操作数类型 [英] unsupported operand type(s) for *: 'numpy.ndarray' and 'numpy.float64'

查看:1892
本文介绍了*:'numpy.ndarray'和'numpy.float64'不受支持的操作数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

长期的读者,第一次的作家.

我在Google上搜索时发现堆栈溢出,但实际上并不能找到该问题的一般答案.

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

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

例如:

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

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

两项工作

现在有问题的孩子们:

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

或,x定义如上:

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

都产生错误:NotImplemented

我知道随机函数和数字看起来很奇怪,但是从概念上讲,这仍然应该起作用,因为当将它们分别设置为变量时,它仍然起作用.

为什么会这样?从一般意义上来说,我该如何解决?

非常感谢! 杰森

我怀疑这里的问题是NumPy无法在其数组中存储Python long值.尝试执行此操作后,它会将阵列的数据类型切换为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'
 

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

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

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

解决方法是避免在NumPy数组中创建long值,而改用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])
 

如果将来会出现类似这样的错误,则要做的第一件事是检查要相乘的数组的dtype.它包含NumPy值还是Python对象?

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.

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

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:

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

both produce the error: 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

解决方案

I suspect that the problem here is that NumPy cannot store Python long values 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.

>>> 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.

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

>>> 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 dtype of the array being multiplied. Does it contain NumPy values or Python objects?

这篇关于*:'numpy.ndarray'和'numpy.float64'不受支持的操作数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆