AI Hints
Statistics & Math for ML · Lesson 82 of 138

Matrix Multiplication

Rows times columns.

⏱ 2 min read▶ Video at 1:36:54🎓 Free
Watch this lesson in the Statistics & Math for ML courseWatch on YouTube ↗Full Statistics & Math for ML course

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.

Matrix Multiplication
From the video: Matrix Multiplication
  • 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.

matmul.pypython
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)
Output
[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.

shapes.pypython
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")
Output
(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
Next up: Matrices as Transformations — stretch and rotate