
Imbalanced Data Doesn't Automatically Need SMOTE: Fix the Evaluation First
- كاتب موثّق
محتويات المقال
The common misconception
SMOTE is everywhere. The paper behind this article counts 156 Google Scholar papers with "SMOTE" in the title in 2021 alone, and more than 100 published variants. The symptom seems to point there: the model predicts almost no positives, so it looks like it never saw enough of them. Rebalancing really does help in some setups, such as weak classifiers or a cutoff you can't change, so success stories are real.
What actually happens
Accuracy hides the minority
from sklearn.datasets import make_classificationfrom sklearn.dummy import DummyClassifierfrom sklearn.ensemble import HistGradientBoostingClassifierfrom sklearn.metrics import accuracy_score, recall_scorefrom sklearn.model_selection import train_test_splitX, y = make_classification(n_samples=8000, n_features=10, n_informative=4, weights=[0.98], random_state=0) # ~2% positiveX_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)models = {"always negative": DummyClassifier(), "boosting": HistGradientBoostingClassifier(random_state=0)}for name, model in models.items(): pred = model.fit(X_tr, y_tr).predict(X_te) print(f"{name:<15} accuracy {accuracy_score(y_te, pred):.3f}" f" recall {recall_score(y_te, pred):.3f}")# always negative accuracy 0.976 recall 0.000# boosting accuracy 0.979 recall 0.143# "It misses most positives: rebalance the data with SMOTE"from sklearn.datasets import make_classificationfrom sklearn.ensemble import HistGradientBoostingClassifierfrom sklearn.metrics import average_precision_score, f1_score, recall_scorefrom sklearn.model_selection import TunedThresholdClassifierCV, train_test_splitX, y = make_classification(n_samples=8000, n_features=10, n_informative=4, weights=[0.98], random_state=0)X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)hgb = HistGradientBoostingClassifier(random_state=0).fit(X_tr, y_tr)ap = average_precision_score(y_te, hgb.predict_proba(X_te)[:, 1])print(f"average precision {ap:.3f}") # ranking quality, no threshold involvedfor name, model in [ ("threshold 0.5", hgb), ("tuned threshold", TunedThresholdClassifierCV(hgb, scoring="f1")), ("balanced weights", HistGradientBoostingClassifier( class_weight="balanced", random_state=0)),]: pred = model.fit(X_tr, y_tr).predict(X_te) cut = getattr(model, "best_threshold_", 0.5) print(f"{name:<16} cutoff {cut:.3f} F1 {f1_score(y_te, pred):.3f}" f" recall {recall_score(y_te, pred):.3f}")# average precision 0.439# threshold 0.5 cutoff 0.500 F1 0.250 recall 0.143# tuned threshold cutoff 0.029 F1 0.444 recall 0.490# balanced weights cutoff 0.500 F1 0.442 recall 0.347A classifier outputs a score, not a verdict
predict takes the probability from predict_proba and applies a of 0.5. When positives are 2% of the data, a transaction with a 20% fraud probability is ten times riskier than average and still gets labeled "legitimate". Low recall at 0.5 tells you the cutoff is wrong for this problem. It doesn't tell you the model failed to learn.
Measure the ranking first
Then choose the cutoff
TunedThresholdClassifierCV picks the threshold that maximizes a metric, using an internal stratified 5-fold cross-validation by default. Optimizing F1, it lands on 0.029: F1 rises from 0.250 to 0.444 and recall from 0.143 to 0.490, at the price of more false alarms, which is the trade the Crash Course describes for any lower threshold. class_weight="balanced", which reweights the classes instead of resampling them, reaches about the same F1, 0.442, with a different mix of recall and precision.
What the benchmark found
How to apply it in your project
Retire accuracy Report precision, recall and average precision, always next to a such as DummyClassifier.Check the ranking before the data If average precision or ROC AUC is weak, rebalancing won't rescue it. Work on features or the model instead. Choose the threshold from costs Use TunedThresholdClassifierCVwith a metric that matches your costs, or pick a point on the precision-recall curve computed on validation data.Start with a strong model In the benchmark, the strongest gradient-boosted models gained nothing from balancing. HistGradientBoostingClassifieris scikit-learn's model from the same family.Rebalance only with evidence With a weak model or a cutoff you can't change, try class_weightor random oversampling inside cross-validation, and keep it only if it wins on the same folds.
Key takeaways
On imbalanced data, accuracy rewards ignoring the rare class: "always negative" scored 0.976. Low recall at a 0.5 cutoff is usually a threshold problem; the same model went from 0.143 to 0.490 recall. Judge the ranking with a threshold-free metric, then set the threshold from the costs of each mistake. Rebalancing, SMOTE included, didn't help strong classifiers in a large benchmark; use it with evidence, not by default.
📚 المصادر
Datasets: Class-imbalanced datasets — Google Machine Learning Crash Course Classification: Accuracy, recall, precision, and related metrics — Google Machine Learning Crash Course Elor and Averbuch-Elor, To SMOTE, or not to SMOTE? (2022) Tuning the decision threshold for class prediction — scikit-learn User Guide Precision-Recall — scikit-learn examples
التعليقات (0)