ENع
←Linear regression
Lesson 03 · Foundations

Linear regression

12 min9 steps5 questionsBeginner
About this lesson

Linear regression predicts a number with a straight line: fare = w × km + b. Training finds the two numbers w and b from examples. Watch one full round of the training loop on four taxi rides, number by number: predict, measure the errors, average their squares into the loss, follow the gradient downhill and update. Then repeat it 1000 times until the line settles, and use it to price a ride it has never seen.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 3 · Interactive
01 / 09

Four taxi rides

The training data: four taxi rides. For each we know the distance in km, the feature, and the fare in dollars, the label. On the chart, longer rides cost more, roughly along a straight line. We want a rule that predicts fares.

ride   km (feature x)   fare (label y)
  1      1                $3.00
  2      2                $4.00
  3      3                $8.00
  4      4                $9.00

goal: a rule that turns km into a fare
Loading figure…
or ← →
Read the lesson step by step
  1. Four taxi rides

    The training data: four taxi rides. For each we know the distance in km, the feature, and the fare in dollars, the label. On the chart, longer rides cost more, roughly along a straight line. We want a rule that predicts fares.

    ride   km (feature x)   fare (label y)
      1      1                $3.00
      2      2                $4.00
      3      3                $8.00
      4      4                $9.00
    
    goal: a rule that turns km into a fare
  2. A straight-line guess

    Linear regression predicts with a straight line: fare = w × km + b. The weight w sets the slope, dollars per km. The bias b lifts the line, like a starting fee. Training starts from a guess: w = 1, b = 0.

    model:  fare = w * km + b
      w  weight: slope, dollars per km
      b  bias: where the line crosses 0 km
    
    first guess  w = 1.0, b = 0.0
    training will only ever change these two numbers
  3. Predict, then compare

    With w = 1, b = 0 the line predicts $1, $2, $3, $4 for the four rides. The real fares were $3, $4, $8, $9. Each error is prediction minus real fare: −2, −2, −5, −5.

    pred = w * x + b      err = pred - y
      1 km   pred 1   real 3   err −2
      2 km   pred 2   real 4   err −2
      3 km   pred 3   real 8   err −5
      4 km   pred 4   real 9   err −5
    
    the dashed gaps on the chart are the errors
  4. One number: the loss

    To score the whole line, square each error and average: (4 + 4 + 25 + 25) ÷ 4 = 14.5. This is the loss, the mean squared error. Squaring makes every error positive and punishes big misses most.

    loss = np.mean(err**2)
      errors   −2, −2, −5, −5
      squared  4, 4, 25, 25
      mean     (4 + 4 + 25 + 25) / 4 = 14.5
    
    lower is better; 0 would mean every dot is on the line
  5. Which way is downhill?

    The gradient tells how the loss changes if we nudge each number up a little. For w it is −20.5; for b, −7. Negative means the loss falls when that number goes up, so both should grow.

    dw = 2 * np.mean(err * x)
       = 2 * mean(−2×1, −2×2, −5×3, −5×4)
       = −20.5
    db = 2 * np.mean(err)
       = 2 * mean(−2, −2, −5, −5) = −7
    negative: raising w or b lowers the loss
  6. Take a small step

    Gradient descent steps each number against its gradient, scaled by the learning rate, 0.05. w becomes 1 − 0.05 × (−20.5) = 2.025; b becomes 0.35. The line swings toward the dots: the loss drops from 14.5 to 0.83.

    w -= lr * dw    1 - 0.05 * (−20.5) = 2.025
    b -= lr * db    0 - 0.05 * (−7)    = 0.35
    
    loss  14.5  ->  0.83
    lr = 0.05 is the learning rate: the step size
  7. Repeat: the training loop

    That was one round of the training loop: predict, measure the loss, find the gradient, update. Repeat it 1000 times. The steps get smaller as the line gets closer, because the gradient shrinks.

    round      w       b      loss
        0    1.000   0.000   14.500
        1    2.025   0.350    0.833
        2    2.194   0.409    0.461
        5    2.226   0.424    0.451
       20    2.221   0.439    0.451
      100    2.206   0.482    0.450
     1000    2.200   0.500    0.450
  8. The best straight line

    Training settles at w = 2.2 and b = 0.5: $2.20 per km plus a $0.50 start. The loss stops at 0.45, not 0: no straight line passes through all four dots. The least-squares formula gives this same line.

    trained       w = 2.2000   b = 0.5000
    least squares w = 11 / 5 = 2.2
                  b = 6 - 2.2 * 2.5 = 0.5
    loss = 0.45, the smallest any straight line gets
  9. Predict a new ride

    Now use the model on a ride it has never seen: 5 km. It predicts 2.2 × 5 + 0.5 = $11.50. Training learned just two numbers, w and b; predicting is the line's formula.

    fare = w * km + b
         = 2.2 * 5 + 0.5
         = $11.50
    
    the new ride lands on the line, past the data

Quiz Unlocks after the last step.