CHAPTER 02 OF 50

Regression: estimating an amount

Predict a home price, then check how far the estimate missed.

Sachin Gupta4 min read
Portrait of Sachin Gupta rendered in binary

A homeowner asks, “How much might my house sell for?” The answer needs an amount of money. Predicting that amount is a regression task.

In chapter 1, training used examples with known answers. Prediction used the trained model for a new input. Here, we give that kind of amount-predicting task its name: regression.

Why do we need regression?

Often, we have to make a decision before we know the actual amount. A seller needs an asking price before a home sells. Regression can use past examples to estimate that unknown amount.

Here are three practical uses. The example predictions are made up.

  • Home pricing. Use a home’s size, location, and condition to estimate a sale price, such as $330,000. This can help a seller choose an initial asking price.
  • Delivery planning. Use the route, distance, and traffic to estimate a trip time, such as 24 minutes. This helps give the customer an arrival window.
  • Rainfall planning. Use weather measurements to estimate how much rain will fall, such as 8 mm over the next six hours. This can help a farmer plan watering.

The useful part is the estimated amount: it gives us something to plan with while the actual answer is unknown.

Learn from homes that already sold

Training examples pair information about a home, such as its size and location, with its known sale price. The sale price is the answer we want the model to learn to predict, also called the target or label.

For a new home, we provide its information without its future sale price. The trained model gives an estimate. Our illustrative example is $330,000. All prices in this chapter are made up.

How much is this home worth?This is regression: the answer measures an amount. A price such as $330,500 would also make sense.SUPERVISED LEARNING / 02How much is this home worth?Portrait of Sachin GuptaEstimate a priceINPUTA new homeEXAMPLE PREDICTION$330,000An estimated sale priceKnown sale prices in training$240,000$350,000$290,000REGRESSIONAn amountSachin Gupta · guptasachinn.comIllustrative answers · no live model
This is regression: the answer measures an amount. A price such as $330,500 would also make sense.

The answer measures an amount

$330,000 and $330,500 are both possible estimates. The difference between them, $500, has a meaning: it is an amount of money.

Other regression tasks can estimate a travel time, a temperature, or a weight. Always keep the unit with the answer: dollars, minutes, degrees, or grams.

An estimate can be wrong

Suppose the home actually sells for $340,000. Our $330,000 estimate was $10,000 too low. A regression model predicts an amount; it does not guarantee the actual outcome.

How far off was the estimate?

Our illustrative estimate is $330,000. Change the actual sale price and check the gap. This calculator measures the miss; it does not train a price model.

Estimated price$330,000

The estimate missed by $10,000.

Count the distance between the two prices. An estimate below the sale price can miss, and an estimate above it can miss too.

One quick check

A model estimates $330,000. Could the home actually sell for $335,000?

Choose one answer.

Regression in one infographic

Known prices, a new home, an estimated amount, and the actual outcome.

RegressionLearn from examples to estimate an amount. Six illustrated panels explain this task using fictional examples.SUPERVISED LEARNING / CHAPTER 02RegressionLearn from examples to estimate an amount.Portrait of Sachin Gupta1Ask for an amount$330,000How much might this home sell for?The answer is an amount in dollars.2Train on completed salesHome information + known sale priceHome A$240,000Home B$350,000Home C$290,0003Predict for a new home$330,000Provide the new home's information.Its actual sale price is still unknown.4Compare with the outcomeEstimateActual sale$330,000$340,000$10,000The estimate was this far off.5Keep the unit with the answerPrice$330,000Travel time24 minutesWeight165 gramsEach answer measures an amount.6An estimate is not a guaranteeThe model estimates a price.The actual sale can differ.Check predictions againstactual outcomes.This is how you see the misses.Regression estimates an amount.Illustrative examples and predictions · no live trained model on this pageSachin Gupta · guptasachinn.com

Code and sources

Run the price-gap example

Optional: Node.js 20 or newer, no extra packages. Copy the full source into regression-example.mjs. It checks an illustrative estimate against an actual price; it does not train a price predictor.

node regression-example.mjs
Estimated price: $330,000
Actual sale price: $340,000
Gap: $10,000

Change actualSalePrice to $320,000 or $330,000 and run again.

Full source
// Fictional home prices for an introductory regression lesson.
export const HOME_EXAMPLE = Object.freeze({ estimatedPrice: 330000, actualPrice: 340000 });
export const KNOWN_PRICES = Object.freeze(["$240,000", "$350,000", "$290,000"]);
export function formatPrice(value) {
  return "$" + value.toLocaleString("en-US", { maximumFractionDigits: 0 });
}
export function priceGap(estimate, actual) {
  if (![estimate, actual].every(value => Number.isFinite(value) && value >= 0)) {
    throw new RangeError("Use nonnegative, finite prices.");
  }
  return Math.abs(actual - estimate);
}



// The estimate is illustrative, not the result of a trained price model here.
// Change the actual price to see how far the estimate missed.
const actualSalePrice = 340000;
console.log("Estimated price: " + formatPrice(HOME_EXAMPLE.estimatedPrice));
console.log("Actual sale price: " + formatPrice(actualSalePrice));
console.log("Gap: " + formatPrice(priceGap(HOME_EXAMPLE.estimatedPrice, actualSalePrice)));
Sources and teaching notes

The definition follows Google’s regression definition. Google’s supervised-learning introduction explains known labels, training, and prediction. The home drawings and prices are illustrative. A trained home-price model is not running on this page.

For a real travel-time application, see Google’s explanation of travel-time prediction. The practical examples above use made-up predictions.

← Chapter 1: Learning from examples
Chapter 3: Classification →

Related

Tagged