Sub topic 12
Reading coefficients as log-odds and odds ratios
This chapter assumes you know the sigmoid model and the link between probability, odds, and log-odds. It also assumes you can fit a scikit-learn pipeline. Here you learn to say what a fitted coefficient means.
The model is linear in log-odds
Logistic regression computes a score z = b + w₁x₁ + … + wdxd. The score is the log-odds of the positive class:
log(p / (1 − p)) = b + w·x
A coefficient wⱼ is therefore the change in log-odds when xⱼ rises by one unit and the other features stay fixed. The effect is additive on the log-odds scale. It is not additive on the probability scale.
Exponentiate to get a multiplicative effect on the odds:
odds after / odds before = exp(wⱼ)
This ratio is the odds ratio.
| Coefficient w | Odds ratio exp(w) | Effect of +1 unit |
|---|---|---|
| 0 | 1.0 | No change in odds |
| 0.69 | about 2.0 | Odds double |
| −0.69 | about 0.5 | Odds halve |
Example with standardized features
Scale first, so one unit means one standard deviation. Coefficients are then comparable across features.
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
data = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(
data.data, data.target, test_size=0.25, stratify=data.target, random_state=0
)
model = make_pipeline(StandardScaler(), LogisticRegression(C=1.0, max_iter=1000))
model.fit(X_train, y_train)
clf = model.named_steps["logisticregression"]
coef = clf.coef_[0]
for i in np.argsort(np.abs(coef))[::-1][:3]:
print(data.feature_names[i], round(coef[i], 2), round(np.exp(coef[i]), 2))In this dataset, class 1 is "benign". A negative coefficient means that a higher value of the feature lowers the odds of benign. The odds ratio is then below 1.
Common errors
- Reading the coefficient as a probability change. A coefficient of 0.69 does not add 0.69 to p. The change in p depends on where you start. Near p = 0.5 the slope of the sigmoid is steepest. Near 0 or 1 it is flat.
- Ignoring units. On raw features, a coefficient is per original unit, such as per millimetre. On standardized features, it is per standard deviation. State which one you use.
- Comparing raw coefficients across features. Features on different scales give coefficients that cannot be compared. Standardize first.
- Forgetting regularization. An L2 or L1 penalty shrinks coefficients toward zero. Treat them as biased estimates, not as unbiased effect sizes.
- Causal claims. An odds ratio describes association in your data. Correlated features also split or distort credit between them.
Exercise
A standardized feature has coefficient 0.69, so exp(0.69) is about 2. A patient's predicted probability is 0.20. What is the predicted probability after the feature rises by one standard deviation, with the other features fixed?
Show answer
The starting odds are 0.20 / 0.80 = 0.25. Multiplying by 2 gives odds of 0.5. The probability is 0.5 / (1 + 0.5), which is about 0.33.