Privacy note: We capture your IP address to provide customised and focused services. See our Privacy Policy.

Machine Learning Foundations — Alpha School

Lesson 5: Linear Regression from Scratch

Linear regression is the simplest model in machine learning, and it is the best place to start — not because it is a toy, but because almost every idea in the field appears in it in its clearest form. The task is simple: predict a number from other numbers. Given a house's size, predict its price. Given hours studied, predict an exam score. Given the temperature, predict ice-cream sales. In each case the model draws the straight line that best fits the data, and then reads new predictions off that line.

Imagine a worked example. A small tutoring centre records, for ten students, how many hours each studied and the score each got on a test. Plotted on a graph, the points rise from the bottom left to the top right: more study, higher score. Linear regression finds the straight line that sits closest to all the points at once. The line has an equation you may remember from school: y equals m times x plus b. Here x is the hours studied, y is the predicted score, m is the slope — how many extra marks each extra hour tends to bring — and b is the intercept, the score the line predicts for zero hours of study.

Fitting the best line

But what does closest mean? For each student, the line makes a prediction, and the difference between that prediction and the real score is called a residual — the error for that point. Some residuals are positive, some negative, so simply adding them up would let errors cancel out. Instead, linear regression squares each residual and adds those squares together. Squaring makes every error positive and punishes large errors much more than small ones. The best line is the one that makes this total — the sum of squared residuals — as small as possible. That rule is called least squares, and it is what the model is really doing when it fits.

Once the line is fitted, using it is easy. A new student studies for six hours, and the model reads the score off the line. But the line also tells you something about the relationship itself. A steep slope means each extra hour matters a lot; a flat slope means hours barely matter. An intercept far from zero hints at what happens with no study at all — though you should be careful here, because predicting far outside the range of your data is risky. A line fitted on students who studied between two and ten hours may be nonsense for someone who studied for a hundred hours.

More than one feature

Real predictions rarely depend on a single feature. A house price depends on size, bedrooms, age and location all at once, and linear regression handles that by fitting not a line but a flat surface in many dimensions — one slope for each feature. The equation grows to match: the prediction is the intercept plus each feature multiplied by its own slope. Each slope then has a plain reading: holding all the other features fixed, how much does the prediction change when this one feature rises by one unit? That phrase, holding the others fixed, is doing real work: the slope for bedrooms measures the effect of an extra bedroom for houses of the same size, age and location, not for houses in general. Multiple regression is still linear regression — the same least-squares fitting, the same residuals — just with more slopes to learn, and the same care needed about outliers and about predicting far outside the data.

Linear regression has real limits, and knowing them is part of using it well. It assumes the relationship is roughly a straight line, and many real relationships are not: a plant grows fast, then slows; a dose of medicine helps up to a point, then harms. It is also sensitive to outliers — one wild point can drag the whole line towards it, because squaring makes its error huge. And it can only use the features you give it: if house prices depend on the school district and you never record it, no line can capture that. Even so, linear regression remains a working tool in its own right and the foundation for everything more complex that follows, including the neural networks of Lesson 14.

A straight line through scattered points

Hours studied Test score residual (error) The fitted line sits closest to all the points at once

Each dot is one student. The green line is the model's prediction; the dashed segment shows one residual — the gap between the line's prediction and the student's real score.

Key Terms

Linear regression
A model that predicts a number by fitting a straight line to the data and reading new predictions off that line.
Slope
In the line y = mx + b, the number m: how much the prediction changes for each one-unit increase in the feature.
Intercept
In the line y = mx + b, the number b: the prediction when the feature is zero.
Residual
The difference between a real value and the model's prediction for one example.
Least squares
The rule for fitting a line: choose the line that makes the total of the squared residuals as small as possible.
Outlier
An example far from the rest of the data, which can pull a fitted line strongly towards it.
Feature
One measurement used to make a prediction, such as hours studied.

Discussion Questions

  1. The line predicts a score for zero hours of study. Why might that number be meaningless even if the maths is right?
  2. A single student who studied 40 hours and scored badly would drag the line. What could you do about such a point before trusting the model?
  3. Where in everyday life do you think a straight-line relationship is a fair description, and where would it fail?

Practice Questions

  1. In y = mx + b, what do m and b mean for the study-hours example?
  2. What is a residual?
  3. Why does least squares square the residuals instead of just adding them?
  4. Why is it risky to predict far outside the range of the training data?
  5. Give one limit of linear regression.
Show answers
  1. m is the extra marks per extra hour studied; b is the predicted score for zero hours. The slope measures the effect of one more hour, and the intercept is where the line crosses the score axis.
  2. The difference between a real value and the model's prediction for one example. If a student really scored 70 and the line predicted 65, the residual is 5.
  3. So positive and negative errors cannot cancel out, and large errors count more. Squaring makes every error positive and punishes big mistakes much more than small ones.
  4. Because the straight-line pattern may not continue outside that range. A line fitted on 2 to 10 hours of study says nothing reliable about 100 hours.
  5. It assumes the relationship is roughly a straight line. Many real relationships curve, level off or reverse, and a straight line cannot capture that.
Keep reading to complete this lesson