Linear Regression: Predicting Numbers with a Line
Imagine you have a handful of dots scattered on a graph — each dot is a real apartment, its area on the bottom axis and its price up the side. You can see a rough trend: bigger apartments cost more. Linear regression is simply the art of drawing the one straight line that runs through the middle of those dots as well as possible, and then using that line to guess the price of an apartment you have never seen before.
That is the whole idea. A machine "learns" a line from examples, and afterwards the line becomes a prediction machine: give it an area, it hands you back a price. In this lesson we will build that line by hand with real numbers, learn exactly what "best" means, and see how a computer nudges the line into place step by step.
What you will be able to do after this lesson
- Explain what a straight-line model $y = w\,x + b$ means, in plain words, and what the slope $w$ and intercept $b$ each control.
- Use a fitted line to predict a number (like an apartment price) from an input (like area).
- Measure how good a line is using residuals, squaring, and Mean Squared Error (MSE) — computed by hand.
- Describe how gradient descent "walks downhill" to find the best line, and what a learning rate does.
- Extend the line to many features (a weighted sum) and read the weights — carefully — as feature importance.
- Know where linear regression works and where it fails, and read R-squared as a percentage of variation explained.
- Fit a real model in a few lines of Python with
scikit-learn.
The big idea: a line that turns an input into a prediction
A line is the simplest possible relationship between two numbers. Every straight line can be written with one small formula:
$$y = w\,x + b$$
Let us decode every symbol, because these five characters carry the whole lesson: