Machine learning is built on mathematics, but that does not mean you need to master advanced mathematics before you can build your first model.
What you do need is a working understanding of a few ideas that appear again and again: averages, variation, probability, relationships between variables, functions, error, vectors, and matrices.
These ideas are much easier to understand when they are attached to an actual problem rather than presented as a list of formulas. So instead of starting with mathematical notation, we will start with a small machine-learning problem and introduce the mathematics as we need it.
Imagine that we want to predict a student's final exam score.
Our dataset looks like this:
| Hours Studied | Attendance | Previous Grade | Final Score |
|---|---|---|---|
| 2 | 65 | 55 | 58 |
| 4 | 78 | 67 | 70 |
| 5 | 82 | 72 | 76 |
| 7 | 91 | 81 | 87 |
| 8 | 95 | 88 | 93 |
The first three columns describe each student. The final column contains the result we would eventually like a model to predict.
For a new student, we might know:
Hours studied: 6
Attendance: 88
Previous grade: 79
and want our model to produce something like:
Predicted final score: 84
Before we can understand how a model might learn to make that prediction, we need a small amount of mathematics.
Not because the model enjoys mathematics, but because our data is made of numbers and we need useful ways to describe what those numbers are doing.
Start by Understanding the Data
Before training anything, we usually want to know what a typical value in our dataset looks like.
Suppose five students received the following scores:
70, 80, 75, 90, 85
The most familiar way to summarize them is the mean, or average.
We add the values and divide by the number of values:
(70 + 80 + 75 + 90 + 85) / 5 = 80
So the mean score is 80.
In Python:
scores = [70, 80, 75, 90, 85]
mean = sum(scores) / len(scores)
print(mean)
With NumPy:
import numpy as np
scores = np.array([70, 80, 75, 90, 85])
print(np.mean(scores))
Both give us:
80.0
The mean is useful because it compresses many values into one simple description of the center of the data.
But averages can hide quite a lot.
Consider these two groups:
Group A: 78, 79, 80, 81, 82
Group B: 40, 60, 80, 100, 120
Both have a mean of 80.
Yet they clearly describe very different groups.
The values in Group A are tightly clustered around 80. Group B contains much larger differences.
This is why we also care about standard deviation.
Standard deviation gives us a way to describe how much the values are spread around their mean. A small standard deviation means the values tend to stay close to the average. A larger standard deviation means they are more widely scattered.
We can calculate it with NumPy:
scores = np.array([70, 80, 75, 90, 85])
print(np.mean(scores))
print(np.std(scores))
The result is approximately:
80.0
7.07
At this point, you do not need to memorize the formula for standard deviation. What matters is being able to read those two numbers.
The mean tells us roughly where the data is centered.
The standard deviation tells us how much it varies around that center.
Those two pieces of information already tell us much more about a dataset than the average alone.
They also become important later when features use very different numerical scales. A dataset might contain age values around 20–70, income values around 20,000–150,000, and percentages between 0–100. Machine-learning algorithms often need us to think carefully about those differences.
For now, remember the intuition rather than the formula.
Mean describes the center. Standard deviation describes the spread.
Looking for Relationships
Now return to our students.
Look only at hours studied and final score:
2 hours → 58
4 hours → 70
5 hours → 76
7 hours → 87
8 hours → 93
There seems to be a pattern.
Students who study for more hours tend to receive higher scores.
That does not mean study time explains everything, and it certainly does not mean every student who studies for eight hours will score exactly 93. But there is a relationship in the data that may be useful for prediction.
This is one of the central ideas of machine learning.
We provide examples, and the algorithm tries to discover relationships that allow it to make useful predictions about examples it has not seen before.
One of the simplest relationships we can model is a linear relationship: one that can be approximated by a straight line.
Suppose we invent this very simple prediction rule:
predicted score = 5 × hours studied + 50
For a student who studies four hours:
5 × 4 + 50 = 70
For six hours:
5 × 6 + 50 = 80
For eight hours:
5 × 8 + 50 = 90
If you remember the equation:
y = mx + b
this is the same idea.
Here, x is the input, y is the output, m controls how strongly the output changes as the input changes, and b shifts the entire line up or down.
There is nothing particularly "AI" about that equation. It is simply a mathematical way of describing a relationship between two quantities.
What makes the machine-learning version interesting is that we do not necessarily choose the values ourselves.
Instead of manually deciding that the multiplier should be 5 and the starting value should be 50, we can give an algorithm examples and ask it to find values that fit the data.
That is the basic idea behind linear regression, which we will encounter later.
One important warning belongs here: a useful relationship does not automatically imply cause and effect.
If two variables move together, we can say they are related. We cannot automatically conclude that changing one causes the other to change. Machine-learning models are excellent at finding predictive patterns, but a predictive pattern is not the same thing as a causal explanation.
A Model Is a Function
There is another mathematical idea that programmers already understand surprisingly well: the function.
Consider this Python function:
def double(x):
return x * 2
Give it an input:
double(5)
and it produces:
10
Mathematical functions follow the same basic idea. They take an input, transform it according to some rule, and produce an output.
Our simple exam prediction rule could therefore be written as:
def predict_score(hours):
return 5 * hours + 50
Calling:
predict_score(6)
returns:
80
A machine-learning model is more complicated, but at a high level it can still be viewed in the same way:
inputs → model → prediction
For our student:
[6, 88, 79] → model → 84
The model receives information about the student and transforms that information into a prediction.
A neural network may contain millions or even billions of learned parameters, but conceptually the basic pattern remains the same: information goes in, mathematical operations happen, and an output comes out.
Thinking about models as functions makes many later ideas easier to understand.
Training a model then becomes a question of finding a function whose predictions match the real data reasonably well.
Which immediately creates another question:
How do we know whether its predictions are good?
Error: Comparing a Prediction With Reality
Suppose our model predicts that a student will score 84.
The student actually scores 80.
The model made a mistake.
We can measure it:
actual - predicted
80 - 84 = -4
The prediction was four points too high.
For another student:
actual = 90
predicted = 84
we get:
90 - 84 = 6
This time the model predicted six points too low.
The sign tells us the direction of the mistake, but sometimes we only care about its size.
A prediction that is four points too high and one that is four points too low are both four points away from the correct answer.
We can express that using the absolute value:
|-4| = 4
|4| = 4
This gives us the absolute error.
If:
actual = 80
predicted = 84
then:
absolute error = |80 - 84| = 4
Now suppose our model makes five predictions:
| Actual | Predicted | Absolute Error |
|---|---|---|
| 80 | 84 | 4 |
| 65 | 63 | 2 |
| 90 | 86 | 4 |
| 72 | 75 | 3 |
| 88 | 90 | 2 |
One prediction error does not tell us much about the whole model, so we need a way to summarize all five.
We already know one.
Take the mean.
(4 + 2 + 4 + 3 + 2) / 5 = 3
The model's average absolute error is 3.
This measurement is called Mean Absolute Error, or MAE.
Its interpretation is refreshingly straightforward:
The model's predictions are about 3 points away from the real scores on average.
This is an important moment because several pieces of mathematics have now joined together.
We made predictions using a function. We compared those predictions with real values. We measured the absolute error of each prediction. Then we calculated the mean of those errors.
That is a real model-evaluation metric built from very basic mathematics.
Calculating Prediction Error With NumPy
Let's calculate the same thing in code.
import numpy as np
actual = np.array([80, 65, 90, 72, 88])
predicted = np.array([84, 63, 86, 75, 90])
Because these are NumPy arrays, we can subtract them directly:
errors = actual - predicted
print(errors)
Output:
[-4 2 4 -3 -2]
Now take the absolute value of every error:
absolute_errors = np.abs(errors)
print(absolute_errors)
Output:
[4 2 4 3 2]
Finally, calculate the mean:
mae = np.mean(absolute_errors)
print(mae)
Output:
3.0
The entire calculation can also be written as:
mae = np.mean(np.abs(actual - predicted))
If you encountered that line without knowing the mathematics behind it, it might look cryptic.
Now we can read it from the inside out.
First:
actual - predicted
calculates each prediction error.
Then:
np.abs(...)
keeps the size of each error without caring whether the prediction was too high or too low.
Finally:
np.mean(...)
calculates the average.
In ordinary language, that one line says:
Measure how far every prediction was from the correct value, then calculate the average distance.
This is the level of mathematical understanding that is most valuable when you begin machine learning. You do not need to calculate thousands of errors by hand. You need to know what the computer is calculating and how to interpret the result.
Probability: When the Answer Is Not Just a Number
So far, our model has predicted an exam score.
Other models answer different kinds of questions.
Suppose we want to predict whether a student will pass an exam. A model might not immediately produce the word pass. Instead, it could produce:
Probability of passing = 0.82
A probability is usually expressed as a value between 0 and 1.
0.00 = 0%
0.50 = 50%
0.82 = 82%
1.00 = 100%
Our application could then choose a threshold:
if probability >= 0.5:
prediction = "pass"
else:
prediction = "fail"
Probability allows models to represent uncertainty.
An image classifier, for example, might produce:
cat 0.91
dog 0.07
rabbit 0.02
Instead of simply saying "cat," the model gives us information about how strongly its output favors each possibility.
This is useful, but probability must be interpreted carefully.
A predicted probability of 0.82 is not a guarantee that an event will happen. It represents uncertainty. Even events with high probabilities can fail to occur, while low-probability events can still happen.
Later, probability becomes much more important when we study classification and how models make decisions under uncertainty. At this stage, the main idea is enough:
Regression models often predict quantities. Classification models often work with probabilities.
Vectors: Describing One Example
We introduced vectors in the previous article when looking at NumPy, but now we can connect them more directly to machine learning.
One student has several pieces of information:
Hours studied: 6
Attendance: 88
Previous grade: 79
Instead of treating them as three unrelated values, we can collect them into a vector:
[6, 88, 79]
In machine learning, this vector can represent one example.
A house might be represented as:
[120, 3, 15]
where the numbers mean:
120 square meters
3 bedrooms
15 years old
A customer might be represented as:
[34, 52000, 17]
where the values correspond to age, income, and number of purchases.
This is why you will frequently see the letter x used for an input example:
x = [6, 88, 79]
and y used for the correct answer:
y = 84
In our case, that simply means:
student information → 84
The notation may look abstract at first, but the underlying idea is not.
Matrices: Describing a Dataset
If a vector can represent one student, a matrix can represent many students.
For example:
[
[5.5, 92, 78],
[2.0, 68, 55],
[7.0, 95, 91],
[3.5, 81, 73],
[4.5, 88, 79]
]
Each row represents one student.
Each column represents one feature.
If we had 1,000 students and recorded 20 features for each one, our matrix would have a shape of:
(1000, 20)
In plain English:
1,000 examples, each described using 20 values.
This is why machine-learning code often looks like:
model.fit(X, y)
X typically contains the features for many examples, while y contains the values we want the model to learn to predict.
A matrix may sound like an advanced mathematical object, but in practical machine learning it is often easiest to think of it as a numerical table.
Rows are examples.
Columns are features.
The mathematics becomes more sophisticated later, when models begin performing operations on those vectors and matrices, but their basic role remains the same: they provide an efficient way to represent information.
What Mathematics Do You Actually Need Right Now?
At this point it is useful to separate two goals.
The first is using machine learning.
The second is understanding every mathematical detail of how machine-learning algorithms work internally.
Those goals require different depths of mathematics.
To begin building models, you should be comfortable with basic algebra, averages, variation, probability, functions, vectors, matrices, and simple ideas about error.
As you go deeper, more mathematics becomes useful. Linear algebra helps explain how models operate on vectors and matrices. Probability and statistics become essential for understanding data and uncertainty. Calculus helps explain how many models learn by adjusting their parameters. Optimization explains how those adjustments are used to reduce error.
You will encounter all of those ideas eventually.
But there is little value in stopping your progress for months so that you can study every branch of mathematics before writing a single line of machine-learning code.
A better approach is to deepen the mathematics when a machine-learning problem gives it a purpose.
When we study linear regression, the equation of a line suddenly matters.
When we study model evaluation, averages and errors matter.
When we study classification, probability matters.
When we eventually study gradient descent, derivatives will have a reason to exist.
That context makes the mathematics easier to learn and much easier to remember.
Learn to Translate the Notation
You will still encounter equations.
The useful skill is not avoiding them; it is learning to translate them.
Mean Absolute Error, for example, can be written more formally as:
MAE = (|actual₁ - predicted₁| + ... + |actualₙ - predictedₙ|) / n
If you look only at the notation, it may appear more complicated than the Python version:
mae = np.mean(np.abs(actual - predicted))
But both describe the same idea:
Take each prediction, compare it with the correct value, keep the size of the difference, and average the results.
Whenever you encounter an unfamiliar formula, try asking:
What goes in? What happens to it? What comes out? What does the result tell me?
That is often enough to turn an intimidating equation into an understandable process.
Practical Exercise: Measuring Prediction Error
Suppose a model predicts apartment prices in thousands of euros.
The real prices are:
actual = [180, 240, 310, 150, 275]
The predictions are:
predicted = [175, 255, 300, 160, 280]
Use NumPy to calculate the errors:
import numpy as np
actual = np.array([180, 240, 310, 150, 275])
predicted = np.array([175, 255, 300, 160, 280])
errors = actual - predicted
absolute_errors = np.abs(errors)
mae = np.mean(absolute_errors)
print("Errors:", errors)
print("Absolute errors:", absolute_errors)
print("Mean Absolute Error:", mae)
Before running it, try to work out the answer yourself.
More importantly, when you get the final MAE, describe what it means in a sentence.
That is the habit worth developing: do not stop at the number. Interpret it.
Mini Challenge
A model makes five predictions:
Actual: [50, 70, 90, 60, 80]
Predicted: [55, 68, 84, 63, 79]
Calculate the absolute error for each prediction and then find the Mean Absolute Error.
You can check your result with:
import numpy as np
actual = np.array([50, 70, 90, 60, 80])
predicted = np.array([55, 68, 84, 63, 79])
mae = np.mean(np.abs(actual - predicted))
print(mae)
Do the calculation manually before running the code.
If you can explain what the final value means, you understand the important part.
The Important Part Is the Meaning
You have now seen most of the mathematics we need before building our first simple machine-learning model.
The mean helps us describe a typical value. Standard deviation tells us how much the data varies. Relationships help us identify useful patterns. Functions describe how inputs become outputs. Probability gives us a language for uncertainty. Error allows us to compare predictions with reality. Vectors represent individual examples, and matrices allow us to represent entire datasets.
None of these ideas exists only for machine learning. They are ordinary mathematical tools that become useful because machine learning works with numerical data.
And that is the most useful way to approach mathematics in AI.
Do not begin with the formula and ask yourself how to memorize it.
Begin with the problem.
Then ask what mathematical idea helps you solve it.
In the next article, we can finally take the pieces we have built so far—Python, pandas, NumPy, features, labels, vectors, matrices, functions, and error—and use them to train an actual machine-learning model.
At that point, "learning from data" will stop being an abstract phrase.
We will be able to watch it happen.