AI Class Imbalance
Read the count of each class before celebrating accuracy.
How it works
When one label is common, a model can have high accuracy while missing every example of the smaller class. Report counts and task-relevant error types. Resampling and class weights can help in some settings, but resampling must occur inside training folds and does not manufacture independent evidence.
Run the code
Try in playground loads this complete standard-library Python example without executing it. Choose Run when ready. Local review uses Python 3.12.14; the existing browser editor uses Pyodide 0.27.7. These small fictional examples need no account, external data, model-provider key or database service.
Example
actual = [0,0,0,0,0,0,0,0,0,1]
predicted = [0]*10
accuracy = sum(a==p for a,p in zip(actual,predicted,strict=True))/len(actual)
positive_hits = sum(a==1 and p==1 for a,p in zip(actual,predicted,strict=True))
print(f"accuracy: {accuracy:.2f}")
print("positive hits:", positive_hits)
Expected result
accuracy: 0.90 positive hits: 0
The classifier gets nine records right by always predicting the common label. It detects none of the one positive record. Decide which mistakes matter, then inspect precision, recall and uncertainty rather than treating one aggregate number as a verdict.