The data and the line
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.
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.
One round of training
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.
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.
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.
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.
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.
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.
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.
What happens
;; step 1/9 · data 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
The four rides are invented for the lesson, small enough to check by hand. Real fares also depend on time and traffic; with more features the model becomes w1·x1 + w2·x2 + ... + b (multiple linear regression). The learning rate matters: for these rides, any rate above about 0.12 makes the steps overshoot and the loss explode; a much smaller one works but needs far more rounds.