Module 2: Math Foundations — Linear Algebra
Lesson 2.1 of 40

Scalars, Vectors, Matrices
— And Why ML Needs Them

22 min 📚 Beginner Math
Section 1

Why Bother With Linear Algebra At All?

Here's the honest motivation before any notation: every piece of data a machine learning model touches gets turned into numbers, and those numbers get organized into vectors and matrices so the computer can do millions of calculations on them at once instead of one at a time. An image is a matrix of pixel brightness values. A sentence becomes a list of numbers (you'll see exactly how in Tier 4). A dataset of houses with their size, location, and price is a matrix where each row is one house.

You don't need to love math to use ML well, but you do need enough comfort with this vocabulary that terms like "dot product" or "matrix multiplication" don't make you tune out when they show up in documentation, error messages, or a colleague's explanation. That's the bar this lesson is aiming for — comfort and intuition, not academic rigor.

Section 2

The Three Building Blocks

🔢
Scalar
Just a single number. The temperature outside (72), a price ($19.99), a single test score (88). No direction, no structure — just a value.
Vector
An ordered list of numbers. A house described by [1800 sqft, 3 bedrooms, 2 bathrooms] is a vector with 3 numbers — one "feature" per position.
📊
Matrix
A grid of numbers — rows and columns. A dataset of 100 houses, each with 3 features, is a 100×3 matrix: 100 rows, 3 columns.
💡
A simple way to keep these straight: a scalar has zero dimensions (just a value), a vector has one dimension (a line of numbers), and a matrix has two dimensions (rows and columns). Later in Tier 3, you'll meet "tensors," which just extend this idea to three or more dimensions — same concept, more dimensions.
Section 3

Vectors, Concretely

Say you're describing a house for a price-prediction model (the exact project you'll build in Module 7). You might represent it as:

A house as a vector house = [1800, 3, 2, 15]

Here, position 1 is square footage (1800), position 2 is bedrooms (3), position 3 is bathrooms (2), and position 4 is the house's age in years (15). The order matters — [1800, 3, 2, 15] and [3, 1800, 2, 15] are completely different things, even though they contain the same numbers, because position 1 always means "square footage" by convention in this dataset.

This is exactly what a "feature vector" is in machine learning — a structured list of numbers describing one example. Every row in Module 5's pandas DataFrames is, underneath, a vector like this one.

Section 4

Matrices, Concretely

Now imagine 100 houses, not just one. Stack their vectors into rows, and you get a matrix:

          sqft  beds  baths  age
House 1:  1800,   3,    2,   15
House 2:  2400,   4,    3,    5
House 3:  1100,   2,    1,   40
   ...
House 100: ...   ...   ...  ...

This 100×4 matrix (100 rows, 4 columns) is exactly what gets fed into the linear regression model you'll build in Module 7 — the entire dataset, represented as one mathematical object the computer can process all at once using matrix operations, rather than looping through houses one at a time. That single shift — from "loop through each item" to "operate on the whole matrix at once" — is the core reason NumPy (Module 5) is dramatically faster than plain Python loops for this kind of work.

Section 5

Matrix Shape — A Habit Worth Building Now

ML practitioners constantly talk about a matrix's shape — written as (rows, columns). Getting comfortable reading shapes now will save you real debugging time later, since "shape mismatch" errors are one of the most common bugs in any ML code, including the labs later in this tier.

ObjectShapeRead As
Single house vector (4,) 4 numbers, no row/column structure yet
100 houses, 4 features each (100, 4) 100 rows, 4 columns
Grayscale image, 28×28 pixels (28, 28) 28 rows of pixels, 28 columns of pixels
⚠️
A genuinely common beginner bug: trying to combine two matrices whose shapes don't line up correctly (e.g. trying to multiply a (100, 4) matrix with another (100, 4) matrix the wrong way). You'll see exactly why this fails and how to fix it once we cover matrix multiplication rules in Lesson 2.3.
✅ Quick Check — Lesson 2.1
1. What is the key difference between a scalar and a vector?
2. A dataset of 250 students, each with 6 recorded features, would have what matrix shape?
3. Why does order matter within a feature vector like [1800, 3, 2, 15]?
🎉 Lesson 2.1 complete! Next: what you can actually do with vectors.
🗒 Cheat Sheet 📝 Worksheet