Vocabulary for this course

The terms the chapters assume, in plain words. You can open this from any chapter.

accuracy
The share of predictions that are correct, counting every class the same.
additive
Built by summing separate contributions, so each part’s effect can be read on its own.
baseline
A reference score from a rule so plain it needs no learning, such as always naming the most common answer.
features
The input columns for one example — the numbers a model is allowed to read before it predicts.
fitting
Adjusting a model’s numbers so its predictions get closer to the answers in the training data.
generalisation
How well a model performs on examples it never saw while being fitted.
hypothesis class
The family of shapes a model is allowed to use, such as a straight line or a tree.
label
The correct answer for one example — what you want the model to predict.
loss
A single number measuring how wrong a prediction is; smaller means better.
metric
A single number used to judge predictions, such as the share answered correctly.
noise
Random variation in data that carries no real signal, which a model can mistake for a pattern.
optimiser
The procedure that changes a model’s numbers to make its loss smaller.
overfitting
When a model memorises accidental details of its training examples and does worse on new ones.
parameters
The numbers inside a model that fitting adjusts, as opposed to the inputs it reads.
regularisation
Any penalty or limit that discourages a model from chasing noise in its training examples. The aim is better performance on new data.
supervised learning
A setting where each example carries the answer you want the model to predict.
test set
Examples kept out of fitting, used to estimate how the model will do on data it has not seen.
training set
The examples a model is fitted on; a score there is not an estimate of performance on new data.
weights
The numbers a model multiplies each input by before adding the results together.

Classical machine learning

Interpreting a model

A coefficient is a conditional statement about the model, not about the world. Read it in its units, check it against a held-out set, and never mistake importance for cause.

Part 9 of 10

Intermediate16 min readBack to Courses

A coefficient is conditional

A coefficient, the weight on one feature, says how the prediction moves. Hold every other feature fixed. Raise this feature by one unit. The prediction then changes by the coefficient. That clause about the other features is the key part. A coefficient describes the fitted model, not a law about the world.

Units decide the number. A coefficient per year looks small next to one per thousand dollars, even when the two features matter equally. Scaling the features first removes the units. Each coefficient then measures a change of one standard deviation, which puts them on a similar footing without changing the model.

Correlated features make individual weights unstable. Two columns that carry almost the same information can be traded one against the other. The signs can flip between two samples even when both features are useful. The prediction stays stable while the account of it moves. That is a warning about reading a single weight in isolation.

Log-odds and odds ratios

Logistic regression predicts a log-odds, the log of the odds. A coefficient b means that a one-unit rise adds b to the log-odds of the positive class. Turning the log back into odds gives the odds ratio. A ratio of 1.0 means no change. A ratio of 2.0 doubles the odds, and 0.5 halves them. That is a multiplicative statement about odds, not about chance.

Odds and chance move apart as the baseline moves. A coefficient can triple the odds while moving a predicted chance from 0.02 to 0.06. The same coefficient can move a chance from 0.40 to 0.67. Convert to predicted chances at typical feature values before you explain the effect to anyone who will act on it.

The intercept is the log-odds when every feature is zero. That is often a row that cannot exist: zero age, zero income, no prior purchases. Centring the features makes the intercept the log-odds at the mean. That is at least a plausible reference point. It does not change any slope.

Importance on held-out data

from sklearn.inspection import permutation_importance

result = permutation_importance(
    model, X_test, y_test, n_repeats=20, random_state=0, scoring="roc_auc"
)
for name, mean, std in zip(X_test.columns, result.importances_mean, result.importances_std):
    print(f"{name:12} {mean:+.3f} +/- {std:.3f}")

Coefficients in their units

Fit the logistic model, compare the standardised odds ratios with the per-year and per-thousand ones, then add a copy of age and watch the coefficients split.

Output
Click "Run" to execute your code

Global views and their blind spots

Permutation importance shuffles one column and measures the drop in a held-out metric. It repeats that a few times. The method works for any model. It answers a useful question: how much does the fitted model rely on this column for these rows? Correlated columns share the credit. Shuffling can also create rows that never occur in reality.

Impurity importance from a tree is computed on training data. It favours features with many different values, because they offer more places to split. It is cheap, and it is a poor guide to generalisation. Prefer permutation importance on data the model did not fit. Report the spread across repeats, not a single ranking.

A partial dependence plot shows the average prediction as one feature changes. The other features are averaged out. It hides interactions, and it can extrapolate into empty regions of the feature space. Individual conditional expectation curves show one line per row. There a real interaction becomes visible as lines that do not move together.

Importance is not causation. A feature can rank highly because it is a proxy for another column, or because it encodes the label by accident, and neither fact makes it a lever you can pull.

Local explanations and honesty

A local explanation credits one prediction to its features. SHAP values come from a cooperative game and add up to the prediction. LIME fits a small stand-in model around the row. A counterfactual names the nearest point with a different prediction. Each answers a different question. Mixing them in one report produces a story that is hard to check.

Attribution is not causation here either. A feature can be important because it stands in for a protected attribute. It can also matter because the training window happened to link it with the label. The explanation describes the model’s reasoning. That is exactly what you want when the model is wrong. It is exactly what you must not present as a claim about the world.

Report direction, units, uncertainty and the group the numbers describe. Bootstrap the weights or the importance scores, and show the interval. A ranking without a spread is a story. Check the explanation against a known case before trusting it on a new one. Write down the version of the model and the data it was computed on. An explanation of yesterday’s weights is a historical document.

Importance by shuffling

Run the leave-one-out accuracy, compare the importance of x0 before and after the duplicate is added, then replace the duplicate with a column of pure noise.

Output
Click "Run" to execute your code

Check what you learned

One question at a time. A wrong answer explains itself — take another go.

Part 1 of 5

What does a linear coefficient describe?