Class imbalance and baseline prevalence
Quick answer
Class imbalance means some labels occur much less often than others. Start by measuring prevalence and comparing against a trivial baseline before judging a classifier. Static examples tested locally with Python 3.13.5, scikit-learn 1.8.0 SciPy 1.17.0 and NumPy 2.3.5 where imported. ByteMind package execution is unverified.
Compare with a constant score
from sklearn.metrics import average_precision_score, accuracy_scorey=[0]*95+[1]*5print(f"{accuracy_score(y,[0]*100):.2f} {average_precision_score(y,[.05]*100):.2f}")Observed local output:
0.95 0.05High accuracy coexists with zero positive recall; constant-score AP equals prevalence.
How it works
For binary labels 0 and 1, prevalence pi = number of positives / number of observations. An always-negative classifier has accuracy 1-pi and recall zero when positives exist; precision is undefined because no positive predictions exist. A constant score gives average precision equal to prevalence when both classes exist. A random ranking approaches that reference as sample size grows, but its finite-sample AP is not guaranteed to equal pi. Precision depends on prevalence: for true-positive rate r and false-positive rate f, precision = pir / (pir + (1-pi)*f) when the denominator is nonzero. Even small false-positive rates may create a large review queue for rare events. Report prevalence, confusion counts, PR performance and workload on representative held-out data. Stratify independent rows where appropriate; for repeated entities or temporal data, respect groups and time even if proportions differ. A change in prevalence may change precision and calibration; do not assume unchanged conditional score distributions.
Precision under two base rates
for positives in [100,1000]: tp=positives*.8;fp=(10000-positives)*.02 print(f"{tp/(tp+fp):.3f}")Observed local output:
0.2880.816Holding conditional rates fixed is a hypothetical assumption, not a drift guarantee.
Common mistakes
Never infer useful detection from accuracy alone. Do not compare AP across populations with different prevalence without context. Do not choose a test split to manufacture a convenient class ratio.
Practice and next steps
For 10,000 synthetic items at 1% prevalence, calculate precision at 80% recall and 2% false-positive rate. Compare with prevalence 10%. Complete the knowledge check and staged exercise. Next: Class weights and fold-only resampling.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Class imbalance and baseline prevalence — knowledge check
Apply class imbalance and baseline prevalence to new scenarios.
At 2% prevalence, always-negative accuracy is?
Constant-score AP with both classes equals?
Why can precision fall under lower prevalence?
Repeated customers appear in data. What split matters?
What is recall for the always-negative baseline when positives exist?