Machine learningLoading progress…

A line as a model

Fit a linear predictor by hand, implement its loss and gradient, then compare with a library solution.

The simplest model: a straight line

Suppose an ice cream stand records, for twelve days, how much warmer or cooler it was than the seasonal average (x, in degrees) and how many hundreds of cones it sold (y). The points drift upward as x grows. A model is a rule that turns an input into a prediction. The simplest rule that follows a drifting cloud is a straight line:

prediction = w * x + b

Two numbers define it. The slope w (also called the weight) says how much the prediction rises when x grows by 1. The intercept b (the bias) is the prediction when x is 0. With w = 2 and b = 1, a day 3 degrees above average gets the prediction 2 × 3 + 1 = 7. Learning means finding good values for w and b. Those two numbers are the parameters, and everything the model knows lives in them.

-5-4-3-2-1012345-10-8-6-4-2024681012x: temperature vs averagey: sales
prediction = w * x + b = 1 * 3 + 0 = 3
average squared miss (MSE) = 9.433
Slide w and b and watch the line move through the 12 purple days. Ask about different x values and read the prediction as an equation.

A good line passes through the middle of the cloud

Which line is best? For now use your eyes: the line should rise where the cloud rises and pass roughly through the middle. The readout at the bottom, the MSE, is the average squared distance between the line and the points. A smaller MSE means a better line. Section two of this chapter explains it properly. For this data the best any straight line can do is an MSE of 1.88. Notice that even the best line misses every point a little. The model is a simplification: it ignores everything except the temperature.

Predict first. The model is w = 3, b = −2. What does it predict for x = 4?

Predict first. You shift the intercept b from 1 to 3 and leave w alone. What happens to the line?

Linear means linear in the parameters

The word "linear" refers to w and b, which only ever get multiplied by numbers and added. You could feed the model a transformed input such as x squared and it would still be a linear model, because the prediction is still a weighted sum of fixed inputs. The line is the shape you see when there is one input.

Forcing b = 0 (a line through the origin) is a legitimate but weaker model: here its best slope is 2.025, and its MSE is 2.724 instead of 1.881. You try this below.

Chapter context

Linear regression fits a weighted sum of features. The model is simple enough to inspect numerically, making it a useful bridge between geometry, optimization and software implementation.

Chapter reference: math, worked example & code

This section belongs to the whole chapter, so it is the same on every lesson in it and may cover a different lesson's topic. The code for the experiment you just ran is inside the experiment itself.

Math and notation

y^i=xiTw+b,L=1n∑i(y^i−yi)2.\hat y_i=x_i^Tw+b,\quad L=\frac1n\sum_i(\hat y_i-y_i)^2. ∇wL=2nXT(Xw+b1−y),∂bL=2n∑i(y^i−yi).\nabla_w L=\frac2nX^T(Xw+b\mathbf1-y),\quad \partial_bL=\frac2n\sum_i(\hat y_i-y_i).

When invertible, a no-intercept least-squares solution is (XTX)−1XTy(X^TX)^{-1}X^Ty. Use a pseudoinverse or suitable solver otherwise.

Worked example

Use x=[1,2], y=[2,4], w=1 and b fixed at zero. Predictions [1,2] give residuals [-1,-2], so MSE=(1+4)/2=2.5. The w gradient is 2/2×(−1×1−2×2)=−5. With eta=0.1, w becomes 1.5 and MSE falls to (0.25+1)/2=0.625.

Reference code

The following example uses the Python standard library and does not call a remote service.

x, y, w = [1,2], [2,4], 1.0
mse = lambda weight: sum((weight*a-b)**2 for a,b in zip(x,y))/len(x)
grad = 2/len(x)*sum((w*a-b)*a for a,b in zip(x,y))
assert mse(w) == 2.5 and grad == -5
w -= .1*grad
assert w == 1.5 and mse(w) == .625

Python examples are provided for study and local execution; this page does not execute Python.

Common mistakes

A low residual does not show that a weight is causal. The closed form is not always a literal matrix inverse in reliable code. Include or exclude bias consistently in derivations.