الانتقال إلى المحتوى
Machine Learningتقييم الموديلات· 7 دقائق للقراءة

99% Accuracy Can Mean a Useless Model: Accuracy on Imbalanced Data

When one class is rare, accuracy mostly measures the common one. Check the baseline, read the confusion matrix, then pick a fairer number.

ReadIn

مقال كل يوم يفرق معاك

21 نوفمبر 2026
  • كاتب موثّق
محتويات المقال

A quality team wants a model that flags defective parts on an assembly line, where about 1 part in 100 is bad. Three candidates reach the review with an accuracy of 0.990, 0.990 and 0.976. The third one is dropped first. It's also the only one that catches the defects: 48 of the 50 in the test set. One of the two "best" candidates is a rule that calls every part fine.

The common misconception

Many developers read accuracy as a universal grade: higher is better and 99% is excellent, whatever the data looks like. It's easy to see why:

  • It's the default. score on 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.

The half-truth: accuracy is exact about what it measures. When one class is rare, what it measures stops being what you care about.

What actually happens

Accuracy is the number of correct predictions, true positives plus true negatives, divided by all predictions. That formula hides a weighting.

Accuracy is an average weighted by class size

Split accuracy by class and it becomes the hit rate on positives times their share of the data, plus the hit rate on negatives times theirs. With at 1% positives, 99% of the weight sits on the negatives. A model that finds 14 of 50 defects has a hit rate of 0.28 on them, which adds 0.0028 to its accuracy. Catching every single defect could add at most one point.

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.

The floor moves with the data. The same always-negative rule scores 0.5 on a balanced test set, 0.99 here and 0.999 when positives are 1 in 1,000. Accuracy describes the test set's class mix as much as the model.

Count the four outcomes

Here are the three candidates from the review: the dummy rule, a logistic regression (model A), and the same model trained with class_weight="balanced" so that missing a rare positive costs more (model B).

Python
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"

The splits predictions into four counts: true positives (defects caught), false negatives (defects missed), false positives (false alarms) and true negatives. In scikit-learn, rows are the true classes and columns the predicted ones. Now the models separate. Model A misses 36 of the 50 defects and still ties the dummy on accuracy, because 0.990 rewards the 4,950 good parts far more than the 50 bad ones. Model B catches 48 and pays with 117 false alarms. Accuracy merged all of that into one number and ranked the only useful model last.

One number that respects both classes

averages the recall of each class, the share of each class the model got right, so the 50 defects weigh as much as the 4,950 good parts. scikit-learn's guide gives its two key properties: on balanced data it equals ordinary accuracy, and when accuracy beats chance only by exploiting the imbalance, it drops to 1 divided by the number of classes, 0.5 here. The dummy gets exactly 0.500, model A 0.638 and model B 0.968. The ranking finally matches what the models do. With adjusted=True, random guessing scores 0 instead.

MythReality
99% accuracy means the model worksAlways predicting "fine" scores 0.990 here, with no model at all
Higher accuracy means a better modelModel B catches 48 of 50 defects and has the lowest accuracy, 0.976
A model that beats 0.99 has learned somethingModel A missed 36 of 50 defects and tied the dummy
Accuracy describes the modelIt also describes the class mix: the same rule scores 0.5 or 0.99

How to apply it in your project

  1. Check the class mix firstPrint the share of each class, for example with value_counts(normalize=True) in pandas. If one class dominates, accuracy alone is out.
  2. Put a dummy in every comparisonDummyClassifier gives the floor. A model is only as good as its distance above it.
  3. Read the confusion matrixUse confusion_matrix or classification_report before any single number, and look at the minority class row.
  4. Pick the summary for the questionBalanced 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.
  5. Keep the test set realisticSplit with stratify=y so 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.

📚 المصادر

  1. Classification: Accuracy, recall, precision, and related metrics — Google Machine Learning Crash Course
  2. Metrics and scoring: quantifying the quality of predictions — scikit-learn User Guide
  3. DummyClassifier — scikit-learn API Reference

التعليقات (0)