Python Numpy.dot TypeError:无法根据规则“安全”将数组数据从 dtype('float64') 转换为 dtype('S32')
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34173101/
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
Numpy.dot TypeError: Cannot cast array data from dtype('float64') to dtype('S32') according to the rule 'safe'
提问by user2212461
Why am I getting this error when using np.dot(a,b.T)
:
为什么我在使用时收到此错误np.dot(a,b.T)
:
TypeError: Cannot cast array data from dtype('float64')
to dtype('S32') according to the rule 'safe'
a and b are of type numpy.ndarray
. My NumPy
version is 1.11.0.
a 和 b 是类型numpy.ndarray
。我的NumPy
版本是 1.11.0。
采纳答案by Romeo Kienzler
Just taking the input from BrenBarn and Warren Weckesser to provide a code snippet which should run (by converting your strings to float):
只需从 BrenBarn 和 Warren Weckesser 获取输入即可提供应运行的代码片段(通过将字符串转换为浮点数):
a = map(lambda x: float(x),a)
b = map(lambda x: float(x),b)
np.dot(a,b.T)
or simpler as suggested by @JLT
或更简单的@JLT建议
a = map(float,a)
b = map(float,b)
np.dot(a,b.T)
But as Warren Weckesser already said, you should check the types of the array, most likely one already contains floats.
但是正如 Warren Weckesser 已经说过的,您应该检查数组的类型,很可能已经包含浮点数。
回答by VishnuVardhanA
Try converting whole numpy array into float Example:
尝试将整个 numpy 数组转换为 float 示例:
train = train.astype(float)
train_target = train_target.astype(float)