>>> a = np.array([1,2,3])
>>> b = np.array([2,3,4])
>>> np.dot(a, b)
20
>>> A = np.array([[1,2],[2,3],[3,4]]) # 3x2
>>> B = np.array([[1,2,3],[2,3,4]]) # 2x3
>>> np.dot(A, B)
array([[ 5, 8, 11],
[ 8, 13, 18],
[11, 18, 25]])
As shown above, numpy.dot
with two 1d arrays gives an inner product, and two 2d arrays gives a matrix product,
What if I pass one 1d array and one 2d array?
>>> A = np.array([[1,2],[2,3],[3,4]]) # 3x2
>>> b = np.array([1,2,3])
>>> np.dot(b, A)
array([14, 20])
The 3D 1d array passed as the first argument, b
, is treated like a 1x3 2d array.
The matrix product is converted to a 1d array and returned.
>>> A = np.array([[1,2],[2,3],[3,4]]) # 3x2
>>> B = np.array([[1,2,3]]) #1x3
>>> np.dot(B, A)
array([[14, 20]])
>>> A = np.array([[1,2],[2,3],[3,4]]) # 3x2
>>> b = np.array([1,2])
>>> np.dot(A, b)
array([ 5, 8, 11])
When passed as the second argument, it will be treated like a 2x1 2d array.
>>> A = np.array([[1,2],[2,3],[3,4]]) # 3x2
>>> B = np.array([[1],[2]]) # 2x1
>>> np.dot(A, B)
array([[ 5],
[ 8],
[11]])
N-dimensional 1d array is
numpy.dot
, treat it like a 1xN 2d arraynumpy.dot
, treat it like a 2d array of Nx1
Then, the matrix product is converted to 1d array and returned.Recommended Posts