Fit your first predictive model
A dataset of tiny, invented house sales lets you inspect every operation. Fit a price per square metre, test it on another size and inspect what the result means.
Open this chapter with experiments and audio
What model will fit these three sales?
A 50 m² house sells for €150,000, a 75 m² house for €225,000 and a 100 m² house for €300,000. These are constructed examples. All three have price divided by area equal to €3,000 per m². A model with one weight can represent this pattern exactly by multiplying area by price per square metre. Choosing that model already encodes a relationship that passes through the origin.
| Area | Price (€ thousands) | Input in hundreds of m² |
|---|---|---|
| 50 m² | 150 | 0.50 |
| 75 m² | 225 | 0.75 |
| 100 m² | 300 | 1.00 |
The last column rescales the input so that a straightforward learning rate works well. In these units the desired weight is 300. A weight of 300 means €300,000 per 100 m², equivalent to €3,000 per m². Unit changes alter the numerical weight while leaving the physical relationship unchanged.
Move the weight up by €50 per square metre. The predicted price increases by €2,500 on the smallest home and by €5,000 on the largest. The same adjustment therefore has different effects across the dataset. Move the weight to €3,000 to recover all three training prices.
How does one update use all three examples?
Start the weight at zero. The model predicts zero for every sale. Each example asks for a change in the same direction. For one row, the gradient of squared error is 2 × input × (prediction − target). For the three rows these gradients are −150, −337.5 and −600. Their average is −362.5. A gradient step with learning rate 0.1 increases the weight by 36.25.
On the next pass the predictions are larger and the training errors smaller. Repeating this calculation approaches the common slope. Since the loss is the average over examples, this is batch gradient descent. A batch containing only a sample of rows would yield a cheaper, noisier estimate of the gradient on a larger dataset.
Train and test it in Python
sales = [(0.50, 150), (0.75, 225), (1.00, 300)]
weight = 0.0
for step in range(200):
gradient = sum(2 * area * (weight * area - price)
for area, price in sales) / len(sales)
weight -= 0.1 * gradient
print(round(weight, 3))
print(round(weight * 1.10 * 1_000), "EUR")The printed weight rounds to 300 and a 110 m² house gives €330,000. The new size lies beyond the largest training home. This is extrapolation. Here it works because we generated the sales with the same proportional rule. Real transactions vary with location, condition, year and many other factors, so this exercise measures whether the algorithm can recover a deliberately simple pattern.
What would a bias add?
A bias lets the line move up or down independently of the area. If synthetic prices followed “€40,000 plus €2,000 per m²”, a model forced through zero could not fit every size exactly. A slope and bias together could. Extra flexibility becomes useful when supported by evidence. A larger model can also fit chance fluctuations, which makes a separate evaluation set important.
Why would using 110 directly in the final line produce a wrong price?
The model expects hundreds of square metres. Input 110 would mean 11,000 m². Preprocessing is part of the model’s contract with its caller.
Two sales determine a line. What did you learn?
In a made-up housing market, a 50 m² flat sells for €140,000 and an 80 m² flat for €200,000. Express price in thousands of euros and model it as price = slope × area + offset. Subtract the two equations. The offset cancels and you obtain 60 = slope × 30. The slope is 2 thousand euros per square metre. Substituting the first sale gives offset = 140 − 2 × 50 = 40 thousand euros.
Before seeing a third sale, predict the price at 65 m². It is halfway between the two observations, so the line gives 170, or €170,000. Suppose the flat then sells for €185,000. Its residual, using actual minus predicted, is €15,000. The two training residuals were both zero. A new observation can reveal variation that two points, fitted by two adjustable numbers, could never expose.
Did the fit discover a fixed land price of €40,000?
The 40 is the line’s prediction at zero square metres. Zero lies outside the observed range, and many mechanisms can have produced the two sales. To study a land-price explanation you would need additional measurements, a comparison of alternatives and evidence supporting that interpretation. Extending the line to 200 m² yields €440,000 under the same assumption of constant slope.
Fit the original sales model and inspect its residuals →
Further reading · scikit-learn · LinearRegression