A = np.array([[1, 2], [3, 4]])
B = np.array([[3, 3], [3, 3]])
print(A)
print(B)
print("---")
print(A * B)
[[1 2]
[3 4]]
[[3 3]
[3 3]]
---
[[ 3 6]
[ 9 12]]
# One way to do matrix multiplication
print(np.matmul(A, B))
# Another way to do matrix multiplication
print(A @ B)
[[ 9 9]
[21 21]]
[[ 9 9]
[21 21]]
u = np.array([1, 2, 3])
v = np.array([1, 10, 100])
print(np.dot(u, v))
# Can also call numpy operations on the numpy array, useful for chaining together multiple operations
print(u.dot(v))
321
321
W = np.array([[1, 2], [3, 4], [5, 6]])
print(v.shape)
print(W.shape)
# This works.
print(np.dot(v, W))
print(np.dot(v, W).shape)
(3,)
(3, 2)
[531 642]
(2,)
# This does not. Why?
print(np.dot(W, v))
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-61-c21878d615cb> in <cell line: 2>()
1 # This does not. Why?
----> 2 print(np.dot(W, v))
/usr/local/lib/python3.10/dist-packages/numpy/core/overrides.py in dot(*args, **kwargs)
ValueError: shapes (3,2) and (3,) not aligned: 2 (dim 1) != 3 (dim 0)
# We can fix the above issue by transposing W.
print(np.dot(W.T, v))
print(np.dot(W.T, v).shape)
[531 642]
(2,)