g↗genai/ learner
How Machines Learn / Lesson 02

Regression: predicting numbers

When a supervised model predicts a number—like minutes, temperature, or price—it fits a trend to past examples. That prediction is an estimate, not a guarantee.

Concept + interactive labBeginner friendly

Predicting a number, not a label.

In Lesson 4.1, you saw that supervised learning trains on past examples that pair an input with a known target. When the target is a number along a scale, the task is called regression.

Regression differs from classification—choosing a category such as “spam” or “not spam,” which you will explore in Lesson 4.3. Predicting a car trip’s arrival time from distance or a home’s monthly electricity bill from outdoor temperature are everyday regression tasks.

Running example: Sam’s bike deliveries

Sam (an invented bike courier for this lesson) logged 6 past local deliveries: distances from 1 km to 6 km (the input) and delivery times from 9 to 27 minutes (the target).

In this lesson, you will learn how to read a trend line to make an estimate, see why real results often differ from that line, test how moving a single data point shifts the trend, and check why guessing outside your data range can mislead you.

How to read a fitted line.

Plot Sam’s 6 past deliveries as dots on a chart, with distance along the bottom and minutes up the side. A simple regression model draws one straight fitted line through the cloud of dots to capture the overall trend.

The method used in this lesson—ordinary least squares—chooses the straight line with the smallest total of the squared vertical gaps between the dots and the line (squaring multiplies each gap by itself, so a 2-minute gap counts as 4 and larger misses carry extra weight). Any number you read off that line for a new input is an estimate: a best guess based on past examples, not a promise.

Think of a kitchen chart that gives cooking time by weight: it gives you a solid starting estimate for a new roast, even though oven heat still varies slightly.

  1. Find the new input—say, a 3.5 km delivery—on the horizontal Distance (km) axis.
  2. Move straight up until you meet the fitted line.
  3. Read straight across to the vertical Time (min) axis to get the estimate: 18 min.
Schematic infographic titled Reading a fitted line showing blue past delivery dots near an indigo straight fitted line. A dashed green arrow rises from 3.5 km on the Distance (km) axis to a green diamond on the line and points left to 18 min on the Time (min) axis, while a separate black arrow points right from the plot to a green chip labelled Estimate: 18 min, above an Analogy card comparing it to a cooking-time chart and a Key takeaways card.
Schematic view of reading a fitted line: start at the new distance, move up to the line, and read across for the estimated time (see the table and interactive chart below for Sam’s exact 6 deliveries).

Why every prediction is an estimate.

A common misconception is that a regression prediction is exact. Look closely at Sam’s 6 past deliveries below: not a single dot sits directly on the fitted line.

The signed difference—actual minus predicted—is called a residual (often called an error). A positive residual means the ride took longer than predicted; a negative residual means it arrived faster.

DistanceActualEstimateResidual
1 km9 min8 min+1 min
2 km11 min12 min−1 min
3 km14 min16 min−2 min
4 km22 min20 min+2 min
5 km25 min24 min+1 min
6 km27 min28 min−1 min

Across these 6 deliveries, the average gap—the average size of those vertical gaps, ignoring plus or minus signs so they do not cancel out—is 1.3 min. Real deliveries vary because distance alone cannot capture red lights, hills, wind, or stairs at drop-off.

The same holds outside delivery times: two apartments of the exact same size on the same street can sell for different prices because of floor level, sunlight, or recent renovations.

Schematic infographic titled Predicted vs. actual showing a straight fitted line with blue actual dots above and below it, a vertical rose dashed line and black bracket labelled Gap (error) between Predicted on the line and Actual above it, and a Possible causes card listing Traffic, Hills, and Wind, with a weather-forecast Analogy card and a Key takeaways card.
Schematic view of prediction gaps: real results can land on, above, or below the line because unmeasured factors create variation (see the table above for Sam’s exact 6 deliveries).

Sometimes one example sits far away from the rest of the pattern—for instance, a ride delayed by a flat tire. That extreme point is called an outlier, and you will test its effect next.

Drag the data points and watch the line move.

Notice two numbers above the chart: the Estimate at 3.5 km and the slope—how much the fitted line rises (or falls) for each extra kilometer. Clicking any preset button replaces the current points with that scenario. Try these steps in order:

  1. On Sam’s 6 deliveries, trace up from 3.5 km to the line and across to the time axis, then compare your reading with the Estimate at 3.5 km card (18.0 min).
  2. Drag any dot up or down on the chart (or pick a delivery below the chart and tap −2 min / +2 min) and watch the fitted line tilt.
  3. Click Delay outlier (replaces your edits with Sam’s 6 deliveries, changing the existing 6 km ride from 27 min to 45 min), trace 3.5 km up to the moved line, and check the 21.0 min estimate (up from 18.0 min on Sam’s original data).
  4. Click Reset to restore Sam’s original 6 deliveries.

Illustrative toy model: uses ordinary least squares on 6 invented bike deliveries to show how a straight fitted line responds to data.

Estimate at 3.5 km18.0 min
Slope (min per km)4.0 min/km
Average gap1.3 min
0102030405060123456Distance (km)Time (min)9m11m14m22m25m27m
Adjust a delivery with buttons or keyboard:6 km delivery = 27 min
Range: 4–48 min

Starting with Sam's 6 deliveries. Trace 3.5 km up to the line to see the 18.0 min estimate.

What a fitted line can and cannot tell you.

A straight fitted line is a useful summary, not a crystal ball. Two everyday traps can make its estimates unreliable:

1. Outliers pull the line

Because ordinary least squares squares every vertical gap, one huge delay can tilt or flatten the whole straight line—for example, a 45-minute 6 km ride makes Sam’s line steeper and raises the estimate even for normal 3.5 km trips.

2. Extrapolation beyond the data

Predicting anywhere outside the observed input range (below 1 km or above 6 km) is called extrapolation. Using Sam’s line for a 25 km ride assumes each extra kilometer adds the same extra minutes even when fatigue, hills, or highways change everything.

  • If a new delivery misses the line by 2 minutes, is the model broken?

    No. A regression line predicts a trend, not an exact outcome, so small gaps around the line are expected when other factors vary.

  • Does regression always use a straight line?

    No. Simple linear regression fits one straight line from a single input, while nonlinear regression fits curves. Both linear and nonlinear models can also combine several inputs—such as distance, weather, and time of day—at once.

  • How is regression different from classification?

    Regression predicts a number along a scale (such as 18 minutes), whereas classification picks a category (such as “on time” or “late”), which you will practice in Lesson 4.3.

✎ Try it yourselfKnowledge check · Practice only

Check your understanding.

Sam’s fitted line—built from past bike deliveries between 1 km and 6 km—estimates 18 minutes for a 3.5 km trip. A new 3.5 km delivery arrives in 21 minutes. What is the most accurate conclusion?

Take this with you.

Regression predicts a number as an estimate. A fitted line summarizes the trend in past examples so you can trace from a new input to an estimated output. Real results often fall above or below the line—and outliers or extrapolation beyond the observed data can make a simple line misleading.

Ready to continue? Return to How Machines Learn.