Skip to main content

Command Palette

Search for a command to run...

Why Does Machine Learning Need So Much Math?

Updated
•14 min read•View as Markdown
F
Building software, exploring systems, and documenting what I learn along the way.

I started learning machine learning and immediately ran into a problem.

Math.

Not just one kind of math either.

Linear algebra. Calculus. Probability. Statistics. Optimization.

At some point, I started wondering whether I was supposed to become a mathematician before I was allowed to train a model.

You see a lot of ML tutorials that make the process look deceptively simple:

load data
    ↓
train model
    ↓
model.predict()
    ↓
accuracy: 94%

A few lines of Python and suddenly you have machine learning.

But there is a lot happening underneath those lines.

The model has to represent data somehow. It needs to make predictions. It needs to measure how wrong those predictions are. It needs a way to change its parameters so that its predictions hopefully get better.

And every one of those problems leads back to mathematics.

So instead of treating math as a giant prerequisite wall, I wanted to understand something simpler:

Why does machine learning need all this math in the first place?


Machine Learning Is Mostly About Numbers

Let's strip away the terminology for a moment.

Suppose I want a model to predict the price of a house.

I give it information like:

Area:       1200 sq ft
Bedrooms:   3
Age:        8 years

A machine learning model can't directly perform mathematical operations on concepts like "three bedrooms" or "eight years."

Eventually, these things have to become numbers.

We could represent one house as a vector:

$$ x = \begin{bmatrix} 1200 \ 3 \ 8 \end{bmatrix} $$

Now we have something the model can calculate with.

This is where linear algebra starts showing up.

The model might have parameters such as:

$$ w = \begin{bmatrix} 200 \ 50000 \ -1000 \end{bmatrix} $$

It could then calculate a weighted combination of the features:

$$ w^Tx $$

The result is a number produced from the relationship between the features and the model's parameters.

That's already a very simple machine learning model hiding inside a little bit of linear algebra.

And this was one of the first things that clicked for me:

Machine learning isn't using mathematics because someone decided ML should be difficult. It's using mathematics because mathematics gives us tools for representing data, measuring things, and finding patterns.


1. Linear Algebra: How Do We Represent Data?

A dataset can become enormous very quickly.

Imagine we have 100,000 houses and each house has 20 features.

We can represent the entire dataset as a matrix:

$$ X \in \mathbb{R}^{100000 \times 20} $$

That's just a fancy way of saying:

"I have 100,000 rows and 20 columns of numbers."

The rows represent examples.

The columns represent features.

              features
           ↓     ↓     ↓

house 1   1200    3     8
house 2   1800    4     3
house 3    950    2    15
   .
   .
   .

A matrix is a natural way to represent this kind of data.

Once data is represented as vectors and matrices, we can perform operations such as multiplication, transformations, projections, and similarity calculations.

This is why linear algebra appears everywhere in machine learning.

It becomes especially important in neural networks, where layers perform operations that can be expressed using matrix multiplication followed by nonlinear functions.

You don't need to memorize a collection of linear algebra definitions before starting ML.

The important idea is:

Linear algebra gives machine learning a language for representing and transforming data.


2. But Representing Data Isn't Enough

Let's say we've built a model.

It makes a prediction:

Actual price:     $300,000
Model prediction: $250,000

The model is wrong.

The obvious question is:

How wrong?

We need a way to measure that.

That's where a loss function comes in.

For a simple regression problem, we could use squared error:

$$ L = (y - \hat{y})^2 $$

where:

  • (y) is the actual value
  • (\hat{y}) is the model's prediction
  • (L) is the loss

For our example:

$$ L = (300000 - 250000)^2 $$

The actual number gets very large because we're working with dollars, but the idea is simple.

The model needs a numerical signal telling it:

"That prediction was this far from the answer."

Now we've created another problem.

How do we make the loss smaller?

And this is where calculus enters the picture.


3. Calculus: How Do We Make the Model Better?

Imagine you're standing somewhere on a mountain.

You want to reach the lowest point.

You can't see the entire mountain. You only know what the terrain around you looks like.

One useful question is:

"Which direction goes downhill?"

That is essentially the problem gradient descent tries to solve.

A simple machine learning model might look like this:

$$ \hat{y} = wx + b $$

We have two parameters:

  • (w), the weight
  • (b), the bias

Different values of (w) and (b) produce different predictions.

Different predictions produce different losses.

So we can think of the loss as a function of the model's parameters:

$$ L(w,b) $$

Our goal is:

$$ \min_{w,b} L(w,b) $$

In plain English:

Find the values of (w) and (b) that produce the smallest loss.

But how do we know which direction to move?

Derivatives.


4. A Derivative Tells Us How Things Are Changing

This was one of the things I wanted to understand properly instead of just memorizing:

"The derivative is the slope."

That's correct, but the idea becomes much more useful when we apply it to machine learning.

Suppose:

$$ L(w) = w^2 $$

The derivative is:

$$ \frac{dL}{dw} = 2w $$

If:

$$ w = 5 $$

then:

$$ \frac{dL}{dw} = 10 $$

The positive derivative tells us that increasing (w) increases the loss.

So if we want to reduce the loss, we should move in the opposite direction.

That gives us the basic gradient descent update:

$$ w_{\text{new}}

w_{\text{old}}

\alpha \frac{dL}{dw} $$

where (\alpha) is the learning rate.

The model is essentially saying:

"The loss changes in this direction. I'll take a small step in the opposite direction."

Then it calculates the loss again.

And again.

And again.

That repeated process is one of the fundamental ideas behind training many machine learning models.


5. The Learning Rate Controls the Size of the Step

Suppose you're walking downhill.

You could take tiny steps:

step → step → step → step → step

Or enormous steps:

          STEP
            ↓

Both can cause problems.

If the learning rate is too small, training can take a very long time.

If it's too large, the model can overshoot the minimum and bounce around instead of getting closer to it.

Conceptually:

$$ w_{\text{new}}

w_{\text{old}}

\alpha \nabla L $$

The gradient tells us the direction.

The learning rate tells us how large a step to take.

So when you see something like:

learning_rate = 0.001

it's not just some magical number copied from a tutorial.

It controls how aggressively the model changes its parameters during training.


6. Probability: The World Isn't Certain

Now imagine we're building a spam classifier.

Given an email, the model might output:

Spam:     0.93
Not spam: 0.07

Why probabilities?

Because machine learning usually deals with uncertainty.

The model doesn't have access to some universal label floating above the email saying:

THIS IS SPAM

It has evidence.

Certain words might make spam more likely.

Certain patterns might make legitimate email more likely.

Probability gives us a mathematical language for expressing that uncertainty.

Instead of saying:

"This email is definitely spam."

we can say:

$$ P(\text{spam} \mid x) = 0.93 $$

This can be read as:

"Given the observed input (x), the model assigns a 93% probability to spam."

That distinction matters.

The model isn't necessarily saying:

"I know this is spam."

It's saying:

"Based on what I've learned, spam is the more likely outcome."

Probability becomes particularly important when working with:

  • classification
  • probability distributions
  • likelihood
  • Bayesian methods
  • generative models
  • uncertainty

So:

Probability gives machine learning a way to reason about uncertainty.


7. Statistics: Is the Pattern Actually Real?

Now suppose I train a model on 100 examples.

It gets 99 correct.

Great?

Maybe.

What if another model is trained on 10 million examples and also gets 99% accuracy?

That's a very different situation.

Statistics gives us tools for understanding data and reasoning about whether patterns we observe are meaningful.

Things like:

  • mean
  • variance
  • standard deviation
  • covariance
  • probability distributions

help us understand what our data actually looks like.

Statistics also gives us a framework for thinking about sampling and generalization.

And that's important because the real goal of machine learning isn't:

"Can my model memorize the training data?"

It's:

"Can my model make useful predictions on data it hasn't seen before?"

That's a much harder question.


8. Overfitting: When the Model Learns Too Much

Imagine I give a student the exact questions that will appear on an exam.

They memorize every answer.

They score 100%.

Then I change the questions slightly.

Suddenly they have no idea what to do.

That's roughly the intuition behind overfitting.

A model can become extremely good at the training data while becoming bad at generalizing to new data.

For example:

Training data
      ↓
    Model
      ↓
 99% accuracy

Looks great.

But then:

New data
    ↓
  Model
    ↓
61% accuracy

Now we have a problem.

The model learned the training examples too specifically instead of learning patterns that generalize.

This is one reason we separate our data into things like:

Training set
Validation set
Test set

The training set is used to learn the model's parameters.

The validation set can help us make decisions about the model during development.

The test set gives us a final estimate of how well the model performs on data it hasn't seen.

Statistics gives us the tools and intuition to reason about these differences.


9. Optimization: Finding Good Parameters

Now we can connect everything.

A model has parameters:

$$ \theta $$

It makes predictions:

$$ \hat{y} = f(x;\theta) $$

We define a loss:

$$ L(\theta) $$

And then we want:

$$ \theta^* = \arg\min_{\theta} L(\theta) $$

That strange-looking expression basically means:

Find the parameter values that give us the smallest loss.

That's an optimization problem.

And this is where several areas of mathematics start working together.

Linear algebra helps us represent the data and parameters.

Calculus helps us understand how the loss changes.

Probability helps us deal with uncertainty.

Statistics helps us understand the data and whether our results generalize.

Optimization gives us methods for finding useful parameter values.

That's why ML can feel like several mathematics courses crashed into the same project.

They kind of did.


10. Then Neural Networks Make Everything Bigger

Take a tiny neural network:

x₁ ─────┐
        │
x₂ ─────┼──> neuron ──> prediction
        │
x₃ ─────┘

Inside the neuron, we might calculate:

$$ z = w_1x_1 + w_2x_2 + w_3x_3 + b $$

Then apply an activation function:

$$ a = \sigma(z) $$

The network produces a prediction.

We calculate a loss.

Then we need to figure out:

Which weights contributed to that error?

That leads us to derivatives and the chain rule.

For a network with millions or billions of parameters, we need to calculate enormous numbers of derivatives efficiently.

That's where backpropagation comes in.

A single line like:

loss.backward()

can trigger gradient calculations throughout an entire neural network.

The framework handles those calculations for us, but underneath it is still mathematics.

Lots of it.


11. So Why Can't We Just Let Libraries Do the Math?

We absolutely can.

And most of the time, we should.

You don't need to manually calculate every gradient when PyTorch can do it for you.

You don't need to manually multiply thousands of matrices when optimized libraries can do it in milliseconds.

You don't need to implement every statistical method from scratch.

But there's a difference between:

using a tool

and

understanding what the tool is doing.

Consider:

model.fit(X, y)

That's incredibly convenient.

But if you don't understand what happens underneath it, debugging becomes difficult.

You might not know:

  • why your model is overfitting
  • why changing the learning rate broke training
  • why normalization matters
  • why your loss isn't decreasing
  • why your predictions are unstable
  • why your model performs well on training data but badly on new data

You don't need to calculate everything by hand.

You need enough mathematical intuition to know what the machine is actually doing.


12. Do I Need to Learn All the Math First?

This is probably the most important thing I've realized while looking at the ML math stack.

You can make the prerequisite list terrifying:

Algebra
   ↓
Trigonometry
   ↓
Calculus
   ↓
Linear Algebra
   ↓
Probability
   ↓
Statistics
   ↓
Optimization
   ↓
Real Analysis
   ↓
Tensor Calculus
   ↓
Maybe you're finally allowed to train a model

Please don't do this to yourself.

There is a difference between:

"What mathematics exists behind machine learning?"

and:

"What mathematics do I need right now to understand this algorithm?"

Those are completely different questions.

If I'm learning linear regression, I don't need to master every branch of probability theory first.

I need to understand things like:

$$ y = wx + b $$

loss functions, derivatives, and optimization.

When I reach neural networks, I can learn more calculus.

When I reach probabilistic models, I can learn more probability.

When matrices start becoming confusing, I can go deeper into linear algebra.

The math can grow alongside the ML.


13. The Four Pieces I Keep Coming Back To

If I had to reduce the entire "why does ML need math?" question to four ideas, I'd put it like this.

Linear Algebra

How do we represent and transform data?

Vectors.

Matrices.

Dot products.

Transformations.


Calculus

How does changing something affect the result?

Derivatives.

Gradients.

Chain rule.

Backpropagation.


Probability

How do we deal with uncertainty?

Probabilities.

Distributions.

Likelihood.

Conditional probability.


Statistics

What can we learn from the data we have?

Mean.

Variance.

Sampling.

Correlation.

Generalization.


And sitting over all of them:

Optimization

How do we find parameters that make the model work well?

Minimize a loss.

Adjust parameters.

Repeat.


14. The Bigger Picture

This is the mental model I'm taking with me as I start learning ML:

                  MACHINE LEARNING
                         │
          ┌──────────────┼──────────────┐
          ↓              ↓              ↓
    Represent data    Measure error   Understand
          │              │            uncertainty
          ↓              ↓              ↓
   Linear algebra     Loss functions  Probability
                         │              │
                         ↓              ↓
                       Calculus     Statistics
                         │
                         ↓
                    Optimization
                         │
                         ↓
                  Better parameters
                         │
                         ↓
                  Better predictions

The math isn't there to make machine learning unnecessarily complicated.

Each piece exists because the problem itself demands it.

We need numbers to represent things.

We need functions to make predictions.

We need loss functions to measure mistakes.

We need derivatives to understand how to reduce those mistakes.

We need optimization to find useful parameters.

We need probability to deal with uncertainty.

We need statistics to understand whether the patterns we're learning actually generalize.

Once I looked at it this way, the math stopped looking like a giant wall of prerequisites.

It started looking more like a toolbox.

And that's probably how I'm going to learn it.

Not:

"I need to finish all the mathematics before I can start ML."

But:

"I'm going to learn the mathematics when I reach the problem it was invented to solve."

That's a much less intimidating way to start.

And probably a much more interesting one.