Often we want to predict a number: the price of a house, tomorrow's temperature, a student's exam mark. The oldest and simplest method is to draw a straight line through the data and use it to predict. This is called linear regression.
The idea
Suppose students who studied more hours scored higher. If we plot hours studied against marks, the dots roughly follow an upward line. A line has two numbers: a slope (how fast it rises) and an intercept (where it starts). Learning means finding the slope and intercept that fit the dots best.
Finding the best line
The classic recipe is called least squares. We do not need to derive it here; the code below applies it directly.
hours = [1, 2, 3, 4, 5, 6]
marks = [35, 45, 52, 61, 70, 78]
n = len(hours)
mean_x = sum(hours) / n
mean_y = sum(marks) / n
slope = sum((x - mean_x) * (y - mean_y) for x, y in zip(hours, marks)) / sum((x - mean_x) ** 2 for x in hours)
intercept = mean_y - slope * mean_x
print(round(slope, 2))
print(round(intercept, 2))
print(round(slope * 7 + intercept, 1))
Output:
8.54
26.93
86.7
So the model says: each extra hour of study adds about 8.5 marks, starting from about 27. For 7 hours it predicts about 87.
This is a prediction from a pattern, not a promise. Real marks depend on many other things, and the model has only seen six students. Never trust a model beyond the kind of data it saw.
From a line to bigger models
Real models use many inputs (hours, sleep, attendance, past marks) and curved shapes instead of a straight line, but the heart is the same: adjust the numbers inside the model until its predictions match the examples as closely as possible.
Think about it
- Change the marks list to your own made-up data and re-run the code.
- What would the model predict for 0 hours? Does that make sense?
- Name two other things that affect exam marks that the model ignores.
Next lesson: neural networks.

Comments
Loading comments…