Part 02 / Topic 1 of 7
Vectors and matrices
A vector is a 1-D array of numbers; a matrix is 2-D. In ML, a row is usually one example and a column one feature — the churn table from Part 1 is a matrix with 2,000 rows.
Matrix multiplication A @ B works when the inner shapes agree: (3, 2) @ (2, 3) gives (3, 3). The transpose A.T flips rows and columns.
import numpy as np
A = np.array([[1., 2.], [3., 4.], [5., 6.]]) # (3, 2)
B = np.array([[1., 0., 2.], [0., 1., 1.]]) # (2, 3)
print((A @ B).shape) # (3, 3)
print(A.T.shape) # (2, 3)
Real-world example: A linear model's prediction for a whole dataset is one matrix multiplication: X @ w — examples times weights.
🧪 Quick quiz
Ais(3, 2)andBis(2, 3). What shape isA @ B, and wouldB @ Aalso work?
Show the answer
A @ B is (3, 3). B @ A works too: (2, 3) @ (3, 2) has matching inner sizes, so it gives (2, 2). Both products exist, but they are different matrices.
Done here? The practice set for Part 2 — 4 exercises with solutions — is on the part page.