Imbalanced classes in Python
When one class is rare, accuracy lies and the weights need saying.
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import recall_score
from sklearn.utils.class_weight import compute_class_weight
rng = np.random.default_rng(0)
X = rng.normal(size=(200, 2))
y = (rng.random(200) < 0.1).astype(int)
weights = compute_class_weight("balanced", classes=np.array([0, 1]), y=y)
print(weights.round(2))
plain = LogisticRegression().fit(X, y)
weighted = LogisticRegression(class_weight="balanced").fit(X, y)
print(f"plain recall {recall_score(y, plain.predict(X)):.3f}")
print(f"weighted recall {recall_score(y, weighted.predict(X)):.3f}")
How it works
- Setting
class_weightto balanced scales the loss by frequency. compute_class_weightshows the numbers it uses.- Score such a model on recall or AUC, not accuracy.
Keywords and builtins used here
asintprint
The run, in numbers
- Lines
- 16
- Characters to type
- 612
- Tokens
- 172
- Three-star pace
- 115 tpm
At the three-star pace of 115 tokens a minute, this run takes about 90 seconds.
Step 2 of 3 in Beyond the basics, step 23 of 25 in Machine learning with scikit-learn.