AI & Machine Learning Topic
Maths for Machine Learning: Learn the Ideas with Small Examples
Maths helps you describe what a model is doing. You do not need to memorise a page of symbols first. Begin with numbers you can check by hand, then connect each idea to a model.
Vectors and matrices
A vector is a list of numbers. A flower might be described by four measurements. A matrix is a grid of numbers: one row per flower and one column per measurement. The shape tells you how many rows and columns it has.
A weighted sum multiplies each input by a weight and adds the results. For two inputs, a simple prediction is x1 × w1 + x2 × w2 + b. The bias b shifts the prediction. Matrix operations let software calculate this for many rows efficiently.
A prediction you can calculate
Imagine made-up study data: one, two and three hours have scores of three, five and seven. The rule score = 2 × hours + 1 fits those three points. This does not prove it predicts real people. It is just a clear arithmetic example.
from statistics import mean, median
def predict(hours, weight, bias):
return hours * weight + bias
def mse(actual, predicted):
if len(actual) != len(predicted) or not actual:
raise ValueError("Use two non-empty lists of equal length")
return sum((a-p)**2 for a,p in zip(actual,predicted)) / len(actual)
if __name__ == "__main__":
hours = [1, 2, 3]
scores = [3, 5, 7]
print("Predictions:", [predict(h, 2, 1) for h in hours])
print("Loss:", mse(scores, [predict(h, 1, 1) for h in hours]))
values = [10, 11, 12, 100]
print("Mean and median:", mean(values), median(values))
Save it as math_demo.py and run python math_demo.py. You should see predictions 3, 5 and 7. The loss for the deliberately wrong rule is about 4.67. The mean and median of the last list are 33.25 and 11.5.
Loss measures a kind of error
The mean squared error takes each difference, squares it and averages the squared differences. Squaring makes large misses count more. It is one possible loss, not the right choice for every task. Mean absolute error uses absolute differences and is often easier to read in the original units.
What a derivative tells you
A derivative describes how an output changes when an input changes a little. During training, a gradient tells us how the loss changes with model parameters. Gradient descent takes a small step in a direction that reduces the loss locally.
The learning rate controls the step size. A very large step can jump past a useful region. A very small step can take a long time. The process does not guarantee the best possible model, especially for a complicated loss surface.
Probability means uncertainty
A probability is a number between zero and one. A model may assign 0.8 to a class, but that is not a promise. To treat it as a reliable probability, check calibration: among similar predictions near 0.8, does the event happen about 80% of the time?
Conditional probability asks about one event when another fact is known. Correlation means two things move together in the data. It does not show that one causes the other. Ice-cream sales and hot days can move together without ice cream causing the weather.
Statistics helps you question the sample
The mean is the arithmetic average. The median is the middle value after sorting, or the average of the two middle values. A large unusual value can move the mean a lot. Variance and standard deviation describe spread.
A sample may not represent the people or situations where your model will be used. More rows from the same narrow source do not automatically fix that problem. Report the data source and limits along with a score.
Practice
Change the weight in the example from one to two and compare the loss. Add an extreme value to the final list and watch the mean and median. Explain why a low training loss does not prove success on new data. That question leads into model evaluation.
Official references
Reviewed on 25 October 2026. Use documentation for your installed versions.
Comments (0)
No comments yet. Be the first to comment!