Matrix Multiplication
Rows times columns.
Earlier, one dot product gave a prediction for one house. What about a thousand houses? Matrix multiplication does all the dot products at once, in one line, and very fast.

- One dot product, one house
- A thousand houses?
- Matrix multiplication
X times w
X holds four houses, and W holds the weights. X at W takes the dot product of every row of X with W. Run it. Four predictions in one go. This single line is how linear models predict, for any number of rows.
import numpy as np
X = np.array([[120, 4, 10],
[ 80, 3, 35],
[200, 6, 2],
[150, 5, 20]])
w = np.array([2.0, 10.0, -1.5])
print(X @ w)[265. 137.5 457. 320. ]
The shape rule
You can multiply two matrices when the inner sizes match. A four by three times a three by two gives a four by two. Each entry is the dot product of a row from the first, and a column from the second. If the inner sizes don't match, NumPy raises an error. Run it.
import numpy as np
A = np.ones((4, 3))
B = np.ones((3, 2))
print("(4,3) @ (3,2) ->", (A @ B).shape)
try:
A @ np.ones((2, 2))
except ValueError:
print("(4,3) @ (2,2) -> shapes don't match")(4,3) @ (3,2) -> (4, 2) (4,3) @ (2,2) -> shapes don't match
Summary
Matrix multiplication does many dot products at once. The inner sizes must match, and the outer sizes give the result's shape. Next, matrices as transformations.
Key takeaways- X @ w
- (m, n) @ (n, k) → (m, k)
- Inner sizes must match