gpt4 book ai didi

python - Numpy 矩阵乘法错误

转载 作者:行者123 更新时间:2023-11-28 22:54:47 25 4
gpt4 key购买 nike

我有两个大矩阵:

       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 相乘。发生错误。

  >>> 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 的元素从字符串转换为 float 。

关于python - Numpy 矩阵乘法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com