
99% Accuracy Can Mean a Useless Model: Accuracy on Imbalanced Data
- كاتب موثّق
محتويات المقال
The common misconception
It's the default. scoreon every scikit-learn classifier returns accuracy, and it's the first number most tutorials print.It's intuitive: the share of predictions that were right. Nobody needs a definition. On balanced data it does its job. Google's Machine Learning Crash Course calls it a coarse-grained measure of model quality when both classes have similar numbers of examples.
What actually happens
Accuracy is an average weighted by class size
The floor isn't zero
DummyClassifier makes predictions that ignore the input features, and scikit-learn presents it as a simple to compare against. Its default strategy always predicts the most frequent class, so it scores exactly the majority's share: 0.990 on this data. The useful range for accuracy here runs from 0.990 to 1.000, not from 0 to 1. scikit-learn's guide adds that when a classifier's accuracy is too close to random, something probably went wrong, and lists class imbalance among the usual suspects.
Count the four outcomes
class_weight="balanced" so that missing a rare positive costs more (model B).
from sklearn.datasets import make_classificationfrom sklearn.dummy import DummyClassifierfrom sklearn.linear_model import LogisticRegressionfrom sklearn.model_selection import train_test_splitX, y = make_classification(n_samples=20_000, n_features=10, n_informative=4, weights=[0.99], flip_y=0, class_sep=2.0, random_state=1) # 1% positiveX_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)models = { "always negative": DummyClassifier(), "model A": LogisticRegression(), "model B": LogisticRegression(class_weight="balanced"),}for name, model in models.items(): accuracy = model.fit(X_tr, y_tr).score(X_te, y_te) print(f"{name:<15} accuracy {accuracy:.3f}")# always negative accuracy 0.990# model A accuracy 0.990# model B accuracy 0.976# "Model B is the worst of the three"from sklearn.datasets import make_classificationfrom sklearn.dummy import DummyClassifierfrom sklearn.linear_model import LogisticRegressionfrom sklearn.metrics import balanced_accuracy_score, confusion_matrixfrom sklearn.model_selection import train_test_splitX, y = make_classification(n_samples=20_000, n_features=10, n_informative=4, weights=[0.99], flip_y=0, class_sep=2.0, random_state=1)X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)models = { "always negative": DummyClassifier(), "model A": LogisticRegression(), "model B": LogisticRegression(class_weight="balanced"),}for name, model in models.items(): pred = model.fit(X_tr, y_tr).predict(X_te) tn, fp, fn, tp = confusion_matrix(y_te, pred).ravel() bal = balanced_accuracy_score(y_te, pred) print(f"{name:<15} TP {tp:>2} FN {fn:>2} FP {fp:>3} balanced {bal:.3f}")# always negative TP 0 FN 50 FP 0 balanced 0.500# model A TP 14 FN 36 FP 15 balanced 0.638# model B TP 48 FN 2 FP 117 balanced 0.968One number that respects both classes
adjusted=True, random guessing scores 0 instead.
How to apply it in your project
Check the class mix first Print the share of each class, for example with value_counts(normalize=True)in pandas. If one class dominates, accuracy alone is out.Put a dummy in every comparison DummyClassifiergives the floor. A model is only as good as its distance above it.Read the confusion matrix Use confusion_matrixorclassification_reportbefore any single number, and look at the minority class row.Pick the summary for the question Balanced accuracy for one fair number, recall when misses are expensive, precision when false alarms are. The Crash Course gives the same guidance for recall and precision. Keep the test set realistic Split with stratify=yso training and test sets keep the class proportions you'll see in production.
Key takeaways
Accuracy weights each class by its size; with 1% positives, the positives barely move it. The floor is the majority's share, not zero: always predicting "fine" scored 0.990, the same as a model that missed 36 of 50 defects. Read the confusion matrix: accuracy ranked the only model that caught 48 of 50 defects last. Balanced accuracy gives each class equal weight, but it ignores false alarms, so check precision too.
التعليقات (0)