【发布时间】:2019-10-08 10:48:47
【问题描述】:
当我更新到最新版本的 numpy 时,我的很多代码都坏了,因为现在每次我在矩阵和数组上调用 np.dot() 时,它都会返回一个 1xn 矩阵,而不仅仅是一个数组。
当我尝试将新向量/数组乘以矩阵时,这会导致错误
例子
A = np.matrix( [ [4, 1, 0, 0], [1, 5, 1, 0], [0, 1, 6, 1], [1, 0, 1, 4] ] )
x = np.array([0, 0, 0, 0])
print(x)
x1 = np.dot(A, x)
print(x1)
x2 = np.dot(A, x1)
print(x2)
output:
[0 0 0 0]
[[0 0 0 0]]
Traceback (most recent call last):
File "review.py", line 13, in <module>
x2 = np.dot(A, x1)
ValueError: shapes (4,4) and (1,4) not aligned: 4 (dim 1) != 1 (dim 0)
我希望矩阵的点和向量都会返回向量,或者矩阵的点和 1xn 矩阵会按预期工作。
使用 x 的转置并不能解决此问题,使用 A @ x、A.dot(x) 或 np.matmul(A, x) 的任何变体也不能解决此问题
【问题讨论】:
-
很烦人,这可能是get rid of
matrix的好时机。
标签: numpy python-3.7