Why Math Matters for ML

Updated · 5 min read
mediumPython 3.13aimachine-learningmathnumpyoverview

Mathematics for machine learning is the small set of math ideas that every model is built from. Linear algebra stores and combines the numbers, probability describes uncertainty, statistics summarizes data and measures error, and calculus tells a model how to change its parameters to get better. You don't need to prove theorems to use ML well, but understanding these four makes models far less mysterious.

Four Kinds of Math, One Model

Navika is looking at houses in Dallas. A tiny model predicts a house's price from its size: price = w × size + b, where w and b are the model's two parameters. Training means finding the w and b that make the predictions closest to real prices.

Predict, measure, compute the gradient, adjust
AreaDescriptionIn the house-price model
Linear algebraIt is used to store data as vectors and matrices and combine them fastAll three predictions in one operation
ProbabilityIt is used to reason about uncertainty"There's a 90% chance the price is between ..."
StatisticsIt is used to summarize data and measure errorThe mean squared error (the loss)
CalculusIt is used to find which way to change parameters to reduce errorThe gradient of the loss

Example: All Four in Ten Lines

Python
import numpy as np
 
# Three past house sales in Dallas: size (1,000s of square feet) and price ($1,000s)
size = np.array([1.5, 2.0, 2.5])
price = np.array([300, 380, 470])
 
w, b = 150.0, 70.0                       # a guess: price = w * size + b
predicted = w * size + b                 # linear algebra: one operation on a vector
error = predicted - price
loss = np.mean(error ** 2)               # statistics: the average squared error
grad_w = np.mean(2 * error * size)       # calculus: how the loss changes with w
grad_b = np.mean(2 * error)              # ... and with b
 
print("Predicted:", predicted)
print("Loss:", round(loss, 2))
print("Gradient: w", round(grad_w, 2), "| b", round(grad_b, 2))
Output
Predicted: [295. 370. 445.]
Loss: 250.0
Gradient: w -60.0 | b -26.67

Explanation: Sizes are in thousands of square feet and prices in thousands of dollars. w * size + b predicts all three prices at once. The loss averages the squared errors: 250. The gradient says how the loss changes as each parameter grows; both values are negative, so increasing w and b will reduce the loss. Repeating "predict, measure, compute the gradient, adjust" thousands of times is what training is.

How Much Math Do You Need?

GoalWhat you need
Use AI tools and APIsIntuition only: what a loss and a probability mean
Train classical ML modelsVectors, averages, variance, the idea of a gradient
Build and debug deep learningMatrix shapes, the chain rule, probability distributions
Research new methodsMuch deeper linear algebra, probability and optimization

Common Mistakes

Trying to learn all the math before any ML. Learn each idea when a model needs it.

Skipping intuition for formulas. Know what a gradient means before memorizing derivative rules.

Ignoring units and shapes. Most math bugs in ML are mismatched shapes or scales.

Interview Corner

Which areas of math does machine learning use?

Linear algebra for data and computation, probability for uncertainty, statistics for summarizing data and measuring error, and calculus for optimizing parameters with gradients.

What is a loss function?

A number that measures how wrong a model's predictions are, such as the mean squared error. Training adjusts the parameters to make it smaller.

Practice

Exercise: try other parameters. Change w and b to 160 and 60, then to 170 and 40. Which pair gives the smallest loss? What do the signs of the gradient tell you each time?

Check your understanding:

  1. What does the loss measure?
  2. What does a negative gradient for w mean?
  3. Which area of math makes all predictions in one operation?

Quick check: why math matters

4 questions
1What does the gradient tell a model?
2What was the loss for w = 150 and b = 70?
3Which area of math describes uncertainty?
4What is training, in one sentence?

Key Takeaways

Quick reference

IdeaCode
Predictionsw * size + b
Lossnp.mean((predicted - actual) ** 2)
Gradient for wnp.mean(2 * error * size)

Was this tutorial helpful?

Lesson 115 of 186