Gradient LabStart the lesson

Lessons › Chapter 3: Regression and optimization

Linear regression

Each bar is one house: size along the floor, price as height. Bigger houses tend to cost more, but not perfectly. There is noise.

Watch it move. This lesson is interactive on Gradient Lab: every step is an animated scene you can replay.

Start the interactive lesson

Step by step

Story

The first real project

Their first project: predict how much a customer will want to borrow, from their income.

Concept

Points from real data

Each bar is one house: size along the floor, price as height. Bigger houses tend to cost more, but not perfectly. There is noise.

Build

Guess a line

We describe the trend with a straight line: ŷ = w·x + b. Think of w as “how much the price rises per extra m²” and b as the starting price. Our first guess is w = 0.1 and b = 1.8.

Build

Predictions sit on the line

For each house, the line gives a prediction ŷ (the small white balls). Some are too high, some too low.

Key idea

Residuals: how far off

The gap between the real price and the line’s guess is the residual: how much the line missed by. A good line keeps these gaps small.

Build

Find the best line

Now tilt and shift the line until the residuals are as small as possible. The best fit here is w = 0.39, b = 0.65. The next lessons show exactly how “as small as possible” is measured and found.

Insight

Predict something new

A new house of size 9 was never in the data. Read the price off the line: ŷ ≈ 4.17. That is the whole point of fitting a model.

Real life

In real life: a quick price estimate

Property sites and estate agents use trend lines like this all the time: bigger homes, higher prices. The stacks of coins are past sale prices. The line through them gives an instant estimate for a new listing.

Quick check: In ŷ = w·x + b, what does w control?

w is the slope: how much ŷ changes for each unit of x. b sets the starting height.

Pseudocode

# x = house size, y = price
# goal: one straight line through the points
w, b = 0.1, 1.8              # a first guess

guess(x):
    return w × x + b         # the line

miss = y − guess(x)          # how far off each house is
w, b = the best line         # smallest misses
guess(9)                     # price for a new house

Key terms

residual
How far off a guess was: the true value minus the guess.

Build it: Gradient descent from scratch (30 min, runs in your browser)

See the project

Build it: House price predictor (30 min, runs in your browser)

See the project