CS 230 — Mathematics, Statistics and Data for AI · Weeks 1–2
Vectors, matrices and dimensional reasoning
Full teaching sequence and cumulative subject work
Study the complete on-site source sections below, then work the original example, changed-condition problem and transfer laboratory. Study Dive into Deep Learning: Data Manipulation and Linear Algebra in full; complete their exercises. Write shapes beside every operation.
Shapes, units and the meaning of a matrix product
A matrix product is meaningful when both dimensions and represented units align. Shape compatibility prevents one class of error but cannot establish that features are in the correct order or measured in the intended units. A dot product silently using dollars against a coefficient trained for cents can be numerically valid and semantically wrong.
Keep a feature schema alongside the parameters. Batch rows represent observations; columns represent features in an explicit order. A bias term is an additional model component, not a reason to ignore units.
Worked problem
Two jobs have features [hours,visits]: [2,3] and [4,1]. The coefficient vector is [1000 cents/hour,500 cents/visit], with bias 200 cents. Compute predictions and dimensions.
Trace the complete reasoning
X has shape 2×2 and w has shape 2×1. Xw has shape 2×1, one prediction per job.
The first dot product is 2000+1500=3500 cents; the second is 4000+500=4500 cents.
Add the 200-cent bias to each result: 3700 and 4700 cents. The bias is not multiplied by the number of features.
Record feature order and units. Swapping hours and visits produces a different model even though the array dimensions still match.
Independent transfer problem
The hours feature is converted to minutes. What coefficient preserves the same prediction, and what error occurs if the old coefficient is retained?
Compare the transfer solution
Divide the hours coefficient by 60: 1000/60 cents per minute. Then 120 minutes contributes the same 2000 cents as two hours. Retaining 1000 as cents per minute inflates that contribution by a factor of 60. Matching dimensions alone will not detect the unit error.
Learning objectives
- Explain and trace the mechanisms in vectors, matrices and dimensional reasoning.
- Apply the method to the worked example and changed-condition exercise.
- Identify assumptions, failure cases and the evidence needed to justify a result.
Study the mechanism
A vector is an ordered collection of components; a matrix can represent a collection of observations or a linear transformation. Meaning depends on the declared axes. If X has n rows and d columns, rows may be observations and columns features. Multiplying X by a d-component coefficient vector produces n predictions. A dimension mismatch often reveals a conceptual mistake before code runs.
A dot product sums componentwise products. For a linear model it combines features with weights, but the units matter. A weight multiplying hours differs from a weight multiplying dollars. Feature scaling changes coefficient interpretation and can affect optimization. Broadcasting is a programming convenience that expands compatible dimensions implicitly; it can create a numerically valid but unintended calculation. Never use successful execution as proof that shapes mean what you intended.
Linear independence concerns whether one vector can be built from others. Redundant features can make coefficient estimation unstable or nonunique even if predictions remain similar. Matrix multiplication is generally not commutative. Track the interpretation of AB rather than assuming BA describes the same process. Begin with tiny hand calculations and verify them in code before using large tensors.
Worked example
def dot(x, w):
if len(x) != len(w):
raise ValueError("Shape mismatch")
return sum(a * b for a, b in zip(x, w))
assert dot([2, 3], [10, 5]) == 35Two hours at weight 10 and three visits at weight 5 contribute 20 and 15, giving 35 in the model’s output units. zip alone would silently truncate unequal inputs, so the explicit length check is part of correctness. The function computes a mathematical operation, not a fitted or validated prediction system.
Check your understanding
Why label matrix dimensions before implementing a model?
- To check whether each operation matches its intended meaning
- Because matching shapes guarantee a good model
- To eliminate all numerical error
Answer and reasoning for every option
Correct: To check whether each operation matches its intended meaning Shape reasoning catches many representation errors early.
Reconsider: Because matching shapes guarantee a good model Mathematically compatible shapes can still encode the wrong task.
Reconsider: To eliminate all numerical error Finite precision remains a separate numerical issue.
Transfer laboratory: A shape-compatible but meaningless model
An array of shape 100×4 stores customers by row. A colleague transposes it and calls each row a customer because the multiplication still runs with different weights. Explain the semantic defect.
Work through the full solution
A successful matrix operation checks compatibility, not the interpretation of axes. The transpose now has four rows representing features and 100 columns representing original customers. The new weights combine customers rather than features. State the observation unit and output shape before fitting and verify a tiny hand-calculated case with named rows/columns.
Evidence of understanding
Explain axis meaning and distinguish numerical compatibility from the specified prediction task.
Complete on-site teaching readings
These are full attributed source sections, not previews. Read them alongside the original Academy explanation and code.
- Dive into Deep Learning · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 2.3. Linear Algebra Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 22.1. Geometry and Linear Algebraic Operations Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
- 22.2. Eigendecompositions Open the notebook in SageMaker Studio Lab · Aston Zhang, Zachary C. Lipton, Mu Li and Alexander J. Smola
Open the complete practice sequence and cumulative assessment →