numpy矩阵乘法误差 [英] Numpy matrix multiplication error
本文介绍了numpy矩阵乘法误差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有2个大矩阵:
Xn = np.matrix(X)
Xnt = Xn.transpose()
然后Xn是这样的:
>>> Xn
matrix([['0,208', '0,22', '0,208', ..., '0,194', '0,205', '0,205'],
['0,22', '0,208', '0,214', ..., '0,205', '0,205', '0,214'],
['0,208', '0,214', '0,22', ..., '0,205', '0,214', '0,211'],
...,
['0,214', '0,214', '0,208', ..., '0,199', '0,211', '0,226'],
['0,214', '0,208', '0,208', ..., '0,211', '0,226', '0,252'],
['0,208', '0,208', '0,211', ..., '0,226', '0,252', '0,24']],
dtype='|S5')
但是我想乘Xn和Xnt.发生错误.
But I want to multiply Xn and Xnt. An error occurs.
>>> print(Xn*Xnt)
Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
print(Xn*Xnt)
File "C:\Python27\lib\site-packages\numpy\matrixlib\defmatrix.py", line 330, in __mul__
return N.dot(self, asmatrix(other))
ValueError: data type must provide an itemsize
出什么问题了?
推荐答案
您的矩阵元素是字符串-打印矩阵时请注意dtype='|S5'
.尝试先将X的元素从字符串转换为浮点数.
Your matrix elements are strings - note dtype='|S5'
when you print the matrix. Try converting the elements of X from strings to floats first.
这篇关于numpy矩阵乘法误差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文