Introduction to AI: How Machines Learn · Lesson 3 of 6

Lesson 3: Predicting Numbers: Fitting a Line

How a model learns a relationship between two things, using a straight line and a few lines of Python.

Lesson 3: Predicting Numbers: Fitting a Line

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

  1. Change the marks list to your own made-up data and re-run the code.
  2. What would the model predict for 0 hours? Does that make sense?
  3. Name two other things that affect exam marks that the model ignores.

Next lesson: neural networks.

Finished this lesson?Log in to save your progress and pick up where you left off.
Next lesson →

Comments

Loading comments…

MeroSir Team
A free MeroSir course. Run the examples yourself and try the exercises: you learn programming by doing. Spotted a mistake? Use the Report button above the lesson.
Lesson 3: Predicting Numbers: Fitting a Line | MeroSir