Skip to content

What a model learns

A model turns features into a prediction by lowering a loss on rows it has seen. The score that matters comes from rows it hasn't.

Introductory ~10 min8 steps

What runs in CodeSplash

You will be able to

  • Name the target, the features and the loss of a prediction problem
  • Explain generalization as the goal of training
  • Say which task Predict infers from a target

Read first

01

Examples in, a rule out

Read

Supervised learning starts with rows where you already know the answer. The answer is the target. The other columns you may use are the features.

A model is a rule that maps features to a prediction, y^=f(x)\hat{y} = f(x). Training picks one rule from a family by lowering a lossLoss functionThe penalty for wrong predictions that training lowers, such as squared error or log loss.: the average penalty for wrong predictions on the training rows.

Three choices define the whole exercise: the family of rules, the loss, and the rows you learn from.

New York air quality, 1973
02

Which task is this?

Predict

Predict first

You pick species in the penguins data as the target. It has three values. What task will the Setup step detect?

RegressionBinary classificationMulticlass classification
Reveal

Multiclass classification. Setup reads the target: categories or yes/no give classification, numbers give regression. Add an event column and a time target becomes survival. You can override the detected Task, and the task decides which metrics are on offer.

03

The goal isn't the training rows

Read

A flexible enough model can drive its training loss to almost nothing by memorizing rows. That says little about tomorrow.

The goal is generalizationGeneralizationHow well a model does on new rows drawn the same way as its training rows, and not on the rows it saw.: low error on rows drawn the same way but not used in training. So machine learning's central habit is to score on held-back rows. Every other idea in this track protects that score from being fooled.

04

How far do features get you?

Predict

Predict first

Air quality: predict ozone from solar radiation, wind, temperature and month. A rule that ignores all four has a cross-validated RMSE of 34.6 ppb. How much of that error will a straight-line model remove?

Halfway or more towards zero errorAbout a thirdBarely any change
Reveal

About a third. Linear regression reaches 22.9, and a random forest 19.0. All three were scored on rows they didn't train on, with 93 training rows in play.

05

See rules take shape

Explore

Each model family can only draw certain shapes. Switch Model and watch the boundary change, then compare the training and new-data accuracy. Try Logistic regression on Two moons, then Random forest.

DExplorable · Decision boundary zoo

What shapes of boundary can each kind of model draw?

Which model will score highest on new data from this shape?

How sure?

Data

Model

5

k for neighbors, depth for trees, γ × 2 for the kernel, hidden units for the network.

0.20
150
Simulating…

Commit your prediction above to see the result

Make your prediction first; the figure appears when you commit.

Make your prediction to see the result.

Finally, use memorize the noise. Training accuracy goes to the top while new-data accuracy falls. The gap between them is what this track is about.

06

Check your understanding

Checkpoint

Check yourself

You train a model to predict a house's sale price from its size and location, minimizing squared error. What is the loss?

Show the answer

Answer: c

The price is the target, size and location are features. The loss is the penalty training lowers, which is a different thing from the metric you report on new rows.

Check yourself

Model A has lower training error than model B. Model B has lower error on rows neither saw. Which should you prefer for predicting new cases?

Show the answer

Answer: b

Training error rewards memorizing. Held-out error is the estimate of generalization.

Check yourself

A baseline has an RMSE of 34.6 and a model 22.9. What share of the baseline's RMSE did the model remove? Answer as a proportion.

Show the answer

Answer: 0.34

1−22.9/34.6≈0.341 - 22.9/34.6 \approx 0.34. Compare with a baseline before calling any score good.

07

Do it in the app

In the app

In Predict, an experiment is one target on one dataset. Setup asks for the target, {{target}} in the walkthrough, and detects the task; the other columns become features, except identifiers and likely leaks, which are left out with a reason.

Python
import pandas as pd
from sklearn.impute import SimpleImputer
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_val_score
from sklearn.pipeline import make_pipeline

df = pd.read_csv("airquality.csv").dropna(subset=["Ozone"])
X, y = df[["Solar.R", "Wind", "Temp", "Month"]], df["Ozone"]
model = make_pipeline(SimpleImputer(strategy="median"), LinearRegression())
rmse = -cross_val_score(model, X, y, cv=5, scoring="neg_root_mean_squared_error")
print(rmse.mean())
R
df <- airquality[!is.na(airquality$Ozone), ]
df$Solar.R[is.na(df$Solar.R)] <- median(df$Solar.R, na.rm = TRUE)
set.seed(1)
fold <- sample(rep(1:5, length.out = nrow(df)))
rmse <- sapply(1:5, function(k) {
  fit <- lm(Ozone ~ Solar.R + Wind + Temp + Month, df[fold != k, ])
  sqrt(mean((df$Ozone[fold == k] - predict(fit, df[fold == k, ]))^2))
})
mean(rmse)

These scripts draw their own folds, so their numbers differ a little from the app's. Python and R in the app read one shared plan.

08

Recap

Recap
  • A model is a rule from features to a target, chosen by lowering a loss on training rows.
  • The target's type sets the task.
  • The goal is generalization, so the score that counts comes from rows the model never saw.

Concepts in this lesson