本文介绍了Numpy.dot TypeError:无法根据规则“安全"将数组数据从dtype('float64')转换为dtype('S32')的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在使用np.dot(a,b.T)时出现此错误:

Why am I getting this error when using np.dot(a,b.T):

TypeError: Cannot cast array data from dtype('float64')
               to dtype('S32') according to the rule 'safe'

a和b的类型为numpy.ndarray.我的NumPy版本是1.11.0.

a and b are of type numpy.ndarray. My NumPy version is 1.11.0.

推荐答案

只需从BrenBarn和Warren Weckesser的输入中提供一个应运行的代码段(通过将字符串转换为float即可):

Just taking the input from BrenBarn and Warren Weckesser to provide a code snippet which should run (by converting your strings to float):

a = map(lambda x: float(x),a)
b = map(lambda x: float(x),b)
np.dot(a,b.T)

或更简单,如@JLT所建议

or simpler as suggested by @JLT

a = map(float,a)
b = map(float,b)
np.dot(a,b.T)

但是正如沃伦·韦克瑟(Warren Weckesser)所说的那样,您应该检查数组的类型,很可能其中一个已经包含浮点数.

But as Warren Weckesser already said, you should check the types of the array, most likely one already contains floats.

这篇关于Numpy.dot TypeError:无法根据规则“安全"将数组数据从dtype('float64')转换为dtype('S32')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 17:19