1. 程式人生 > 其它 >ValueError: shapes (1,1568) and (1,1568) not aligned: 1568 (dim 1) != 1 (dim 0)

ValueError: shapes (1,1568) and (1,1568) not aligned: 1568 (dim 1) != 1 (dim 0)

技術標籤:pythonpython

問題:
想把a=1 * 1568,b=1 * 1568用python做內積,但是報錯:
ValueError: shapes (1,1568) and (1,1568) not aligned: 1568 (dim 1) != 1 (dim 0)

解決:把b向量轉換為1568*1的列向量。

舉例

a = np.arange(3).reshape(1,3)
b = np.arange(3,6).reshape(1,3)

得到:
在這裡插入圖片描述
在這裡插入圖片描述
如果你在這時候對a, b做內積,則會報如下:
在這裡插入圖片描述
當我們對b做一個變換:

>>> c = b.reshape(3,1)
>>> c
array([[3],
       [4],
       [5]])

再做內積

np.dot(a,c)

得到:

>>> np.dot(a,c)
array([[14]])

在這裡插入圖片描述