Linear regression: your first model

Fit a straight line to house prices, learn the notation every ML paper uses, and make your first prediction ŷ.

Lesson · 15 min

In this lesson you will

  • Use the standard notation for a training set (x, y, m and the i-th example)
  • Write the linear model f(x) = wx + b and explain what w and b do
  • Use a model to make a prediction ŷ for a new input

Time to build your first machine learning model. The task: a friend is selling a house of 1,500 square feet and asks what it might sell for. You have a list of recent sales in the neighbourhood. Can the data give a sensible answer?

The training set and its notation

The data a model learns from is its training set. Every machine learning text uses the same short names, so it’s worth learning them once:

  • xx is the input (also called a feature): here, the size of a house in thousands of square feet.
  • yy is the output or target: the price it sold for, in hundreds of thousands of dollars.
  • mm is the number of training examples.
  • (x(i),y(i))(x^{(i)}, y^{(i)}) is the ii-th training example. The (i)(i) is a label, not a power: x(2)x^{(2)} just means “the size of the second house”.

So if the third house is 1,500 sq ft and sold for $310k, then x(3)=1.5x^{(3)} = 1.5 and y(3)=3.1y^{(3)} = 3.1.

The model: a straight line

The learning algorithm’s job is to produce a function ff, the model, that takes a new xx and outputs a prediction. We write the prediction as y^\hat{y} (“y hat”): our estimate of the true yy.

The simplest model that can capture “bigger houses cost more” is a straight line:

fw,b(x)=wx+bf_{w,b}(x) = wx + b

It has two numbers we get to choose, called the model’s parameters:

  • ww, the weight or slope: how much the predicted price rises for each extra 1,000 sq ft.
  • bb, the bias or intercept: where the line crosses the vertical axis, the price it predicts for a size of zero (a baseline, not a real house).

Because there is a single input feature, this is called linear regression with one variable (or univariate linear regression).

Drag the line yourself

The dots are 24 real-looking sales. Move ww and bb with the sliders, or grab the line itself: drag its middle to shift it up and down, drag the round handle to tilt it. Then use the House size probe to read off the model’s prediction y^\hat{y} for your friend’s house.

Loading the interactive graphic…

Each dot is a house that sold recently. Size runs along the bottom, and price runs up the side.

Some things worth noticing:

  • Different values of ww and bb give different models and different predictions for the same house. Training means picking good ones.
  • Even the best line doesn’t pass through every dot. Real prices depend on more than size, so a good model is one whose misses are small overall.
  • With w≈1.9w \approx 1.9 and b≈0.5b \approx 0.5 the line hugs the data, and for 1,500 sq ft it predicts y^≈3.3\hat{y} \approx 3.3, about $330k.

You found good values by eye. But “looks about right” is not something a computer can do. In the next lesson we turn “how good is this line?” into a single number the computer can make smaller.

Check yourself

Self-check · 4 questions

Linear regression

1. In the notation of this course, what does x(3)x^{(3)} mean?
2. In the model fw,b(x)=wx+bf_{w,b}(x) = wx + b, what does ww control?
3. With w=2w = 2 and b=0.5b = 0.5 (size in thousands of square feet, price in hundreds of thousands of dollars), what does the model predict for a 1,500 sq ft house?
4. What does mm stand for?