MODULE 04 · Junior · 2 hours

The first model saved millions — and it was a linear regression

Predicting delivery cost instead of charging a city average. A simple model you can explain to finance coefficient by coefficient, and it beats the baseline threefold.

Open this module in the simulator All 23 lessons

The first two modules are free, no card. The rest are $20/month.

🏢 The situation

Datacore's logistics team is complaining: the customer's delivery cost is computed as the "average city tariff", and the company loses money on long-distance and heavy orders.

Lena:

"Your first model! We have history: 80,000 deliveries with the actual cost. Features — weight, dimensions, distance, city, urgency. Start with linear regression. Yes, everyone wants neural nets right away, but the team rule is: simple baseline first. If a linear model gets MAE below $0.80 — we already save millions. And be ready to explain to Max where the number comes from: finance won't sign off on a 'black box'."

🎯 Your task

  1. Understand how linear regression and gradient descent work.
  2. Train the model in scikit-learn, evaluate it with MAE/RMSE.
  3. Interpret the coefficients for the business.

📚 Theory

Linear regression

The model predicts a number as a weighted sum of features:

Training means finding the weights w that minimize the error on historical data. The classic loss function is MSE (mean squared error).

Gradient descent — how the model "learns"

This is the core of almost all ML, including neural networks (Andrew Ng devotes the first week of his course to it):

  1. Start with random weights.
  2. Compute the error on the data.
  3. Compute the gradient — the direction of the steepest error increase.
  4. Take a step against the gradient: w = w − α·∇L, where α is the learning rate.
  5. Repeat until convergence.
The model: a line through points distance (km) → cost ($) Training: descending the error iterations → loss ↓ step by step minimum
Left — what the model is looking for (the minimum-error line); right — how it looks: gradient descent slides step by step toward the loss minimum.

Feature scaling

Parcel weight: 0.1–50 kg. Distance: 1–9000 km. With such different scales gradient descent converges poorly and the coefficients are incomparable. The fix is standardization: x' = (x − mean) / std (StandardScaler).

Regression metrics

Metric Formula (essence) Property
MAE mean |y − ŷ| same units ($), robust to outliers, business-friendly
RMSE root of mean (y − ŷ)² punishes large errors harder
share of explained variance 1 — ideal, 0 — no better than the mean

If RMSE ≫ MAE — there are individual big misses worth investigating.

Train/test split — the sacred rule

Quality is measured on data the model did not see during training. Otherwise you're measuring memory, not generalization.


What to remember

  • Always start with a simple baseline and record its metric.
  • Gradient descent: a step against the gradient; the learning rate decides everything.
  • Scale features; use a Pipeline to avoid leakage.
  • MAE speaks the language of the business; RMSE detects big misses.

Next in this module: Practice

Open the module →

Nearby lessons

03 Marketing argues: did the discount help? 05 Customers are leaving: a churn model

The whole program — 23 lessons