Sub topic 2
Why a Model That Predicts the Majority Class Scores Well on Accuracy
This chapter assumes you can train a scikit-learn classifier, split data into train and test sets, and read basic metric names. It shows how accuracy can look excellent for a model that has learned nothing useful.
Accuracy is a weighted average of per-class results
Accuracy is the share of predictions that are correct:
accuracy = (TP + TN) / (TP + TN + FP + FN)
Every example counts equally. If 95% of the examples belong to the negative class, the negatives make up 95% of the score. Getting them all right gives 0.95, whatever happens to the positives.
Consider a fraud screen with 2,500 test transactions, 125 of them fraudulent. A model that labels every transaction "legitimate" is right 2,375 times:
- True negatives: 2,375
- False negatives: 125
- True positives: 0
- False positives: 0
Accuracy is 2,375 / 2,500 = 0.95. Recall on the fraud class is 0 / 125 = 0. The model has never found a single case, yet the headline number looks strong.
Measuring the baseline
A useful habit is to compute this "do nothing" baseline before training any real model. DummyClassifier with strategy="most_frequent" does it. The code below builds a synthetic dataset with about 5% positives. It uses a stratified split, which keeps the class ratio the same in the train and test sets.
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.dummy import DummyClassifier
from sklearn.metrics import accuracy_score, recall_score
X, y = make_classification(
n_samples=10000,
n_features=20,
n_informative=5,
weights=[0.95, 0.05],
flip_y=0.01,
random_state=42,
)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=42
)
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
pred = baseline.predict(X_test)
print("accuracy:", accuracy_score(y_test, pred))
print("minority recall:", recall_score(y_test, pred))Expect an accuracy of roughly 0.95 and a minority recall of exactly 0.0. The exact accuracy depends on the real positive share, which flip_y shifts slightly. The model ignores all 20 features, and accuracy still reports 95%.
What this means in practice
The baseline accuracy equals the majority-class share. That number is the floor your real model must beat, and it is much higher than the 50% floor people assume for a binary problem.
- A model at 0.94 accuracy on this data is worse than doing nothing, even though 94% sounds good.
- A model at 0.96 accuracy might be a real improvement or might have found only a handful of positives. Accuracy alone cannot tell you which.
- Small accuracy gains can hide large changes in how many rare cases you catch, because each positive is only a small fraction of the total.
Common errors
- Judging a model by accuracy alone. Check the per-class results with
recall_score, or look at a full confusion matrix. The next chapter covers these. - Forgetting the baseline. If you never run a
DummyClassifier, you cannot tell whether a score is good. - Not stratifying the split. A random split on rare data can leave very few positives in the test set. Recall then becomes noisy, or undefined if no positives land there.
- Assuming the metric matches the goal. Accuracy treats a missed fraud case and a falsely flagged legitimate transaction as equally costly. In most real problems they are not.
Exercise
A test set has 4,000 rows and 80 positives. A model predicts "negative" for every row. What are its accuracy and its recall on the positive class?
Show answer
There are 3,920 negatives, all predicted correctly, so accuracy is 3,920 / 4,000 = 0.98. No positive is found, so recall on the positive class is 0 / 80 = 0.
Exercise
Your new classifier scores 0.93 accuracy on data that is 96% negative. Is it better than the majority-class baseline? Explain.
Show answer
No. The baseline scores 0.96, so 0.93 is worse than always predicting the majority class. You would also need recall and precision on the positive class to see what the model does find.