Math for ML — Less Than You Fear
What you'll learn
- The 3 math areas ML actually uses (and why)
- What a vector is, intuitively
- Averages, weights, and slopes — the real core
- You'll compute a weighted prediction yourself
Good news: you need far less math than people fear. Intuition beats formulas here.
Three tools, not a degree 🎓
Machine learning leans on three math areas — but you can start with the intuition of each:
1. Linear Algebra = organizing numbers in rows. A vector is just a list of numbers describing something. A house might be [120, 3, 2] = (size, bedrooms, bathrooms). An image is a huge vector of pixel brightnesses. ML represents everything as vectors, then does arithmetic on them. That's 90% of the "scary" linear algebra.
2. Statistics = understanding data. Averages, spread, probability. "What's typical? How much do things vary? How likely is this?" You already computed averages last lesson — that is the statistics ML leans on daily.
3. Calculus = finding the best answer by tiny steps. You won't compute derivatives by hand. The one idea: a model learns by checking "if I nudge this knob slightly, does my error go down?" and stepping in the better direction, over and over. That downhill-stepping is called gradient descent — imagine a ball rolling to the bottom of a valley. That's the engine of nearly all ML training.
The single most important concept: weights. A prediction is usually a weighted sum — multiply each input by how much it matters, then add:
price = (size × 1000) + (bedrooms × 5000) + (bathrooms × 3000)
"Training a model" mostly means: find the best weights so predictions match reality. Everything else is detail.
Comparing two vectors: dot products and similarity 🧭
You now know a vector is just a list of numbers describing something. But ML constantly needs to answer a different question: how similar are two vectors? "Is this new email like the spam ones I've seen? Is this product similar to ones this customer already liked?"
The building block is the dot product: multiply the matching entries of two vectors together, then add up the results. You've actually already computed one — the weighted sum from a moment ago (price = size×1000 + bedrooms×5000 + ...) is a dot product between a "features" vector and a "weights" vector!
a = [1, 2, 3]
b = [4, 5, 6]
dot(a, b) = (1×4) + (2×5) + (3×6) = 4 + 10 + 18 = 32
On its own, the dot product's size is hard to interpret — it grows just because vectors are long or have big numbers. So ML usually uses cosine similarity: the dot product divided by the lengths of both vectors, which rescales the result to always sit between -1 and 1:
- 1 → the vectors point in exactly the same direction (very similar)
- 0 → no relationship (at right angles)
- -1 → pointing in opposite directions (very different)
This one number is how a search engine finds the passage most relevant to your question, how a recommendation system finds "customers like you," and how an embedding lesson later will measure whether "king" and "queen" are close in meaning. Same math, everywhere: multiply matching entries, add them up, then rescale.
Compute a model's prediction as a weighted sum. Write predict(features, weights) where both are equal-length lists. Multiply each feature by its weight and add them all up.
Example: features [120, 3, 2], weights [1000, 5000, 3000] → 120000 + 15000 + 6000 = 141000
Write dot_product(a, b) for two equal-length lists of numbers: multiply matching entries and sum the results. This is the exact operation behind similarity search and recommendation systems.
Example: dot_product([1, 2, 3], [4, 5, 6]) → 32
Real-world case study: recommendation systems 🔍
When Netflix, Spotify, or Amazon suggest "you might also like," they aren't magic — they're mostly vector similarity at massive scale. Each user gets turned into a vector describing their taste (learned from what they watched, played, or bought), and each item gets turned into a vector describing its characteristics. Finding good recommendations becomes a search for items whose vectors have the highest cosine similarity to a user's vector.
This approach was proven at industrial scale during the well-documented Netflix Prize (2006–2009): Netflix publicly released a dataset of movie ratings and offered $1,000,000 to any team that could improve their recommendation accuracy by 10%. Thousands of teams competed for nearly three years; the winning approaches leaned heavily on representing users and movies as vectors in a shared "taste space" and measuring their similarity — the same dot-product idea from this lesson, just with far more dimensions and far more data.
The takeaway: a concept as simple as "multiply matching numbers, add them up, rescale" — something you just implemented in a few lines of Python — is the mathematical core of one of the most commercially valuable ideas in modern software.
Lab exercise 1
PythonGraded in Code LabWalk downhill. Gradient descent minimises a loss by repeatedly stepping against its slope. Use the loss
f(w) = (w - 3)^2 + 1
whose derivative is f'(w) = 2 * (w - 3). Each step does w = w - lr * f'(w).
Read one line w0 lr steps (start value, learning rate, number of steps) from standard input. Apply the update steps times and print the final w with 4 decimals, then on the next line the loss f(w) with 4 decimals.
- One step from zero
- Converges towards the minimum at w = 3
- A learning rate of 0.5 jumps straight to the minimum
- Too large a learning rate diverges
- Zero steps changes nothing
Example run
- Input
0 0.1 1
- Expected output
0.6000 6.7600
w, lr, steps = input().split()
w = float(w)
lr = float(lr)
steps = int(steps)
def loss(w):
# TODO 1: return (w - 3) ** 2 + 1
return 0.0
def gradient(w):
# TODO 2: return the derivative 2 * (w - 3)
return 0.0More lines in Code Lab: 5
Hints and instant feedback are in Code Lab. Optional practice: it does not block finishing the lesson.
JavaScript and Python also run in your browser without an account; other languages use the secure server runner and need a free account.
Question 1 of 6
What is a vector in ML?
Question 2 of 6
What does 'training a model' mostly mean?
Question 3 of 6
Gradient descent is like…
Question 4 of 6
What does the dot product of two vectors compute?
Question 5 of 6
A cosine similarity of 1 between two vectors means…
Question 6 of 6
What did the Netflix Prize competition rely on mathematically?
Summary
- ML math = linear algebra (vectors), statistics (understanding data), calculus (gradient descent)
- The core concept is weights: a prediction is a weighted sum of inputs
- Training = finding the weights that minimize error
- The dot product measures alignment between vectors; cosine similarity rescales it to -1..1
- You computed a real weighted prediction and a real dot product
- Case study: the Netflix Prize showed vector similarity is the mathematical core of recommendation systems
Next: Machine Learning — putting these pieces together into a model that learns from examples.
Practice
Reading is not learning until you run something. Pick one.
Graded lab exercises
- Lab exercise 1PythonNot passed yet Try in Code Lab