Models are not sensitive to class imbalance


Welcome!

Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, or
method to help you stay sharp in the field. I hope you find it useful!


My latest eBook Imbalanced Data, Myths, Mistakes and Modern Solutions is here!

Most advice about imbalanced data is incomplete, or simply wrong. This book challenges common assumptions and provides practical, evidence-based guidance for tackling class imbalance and building better machine learning models.


Models are not sensitive to class imbalance

Still today, many blogs and articles online, as well as generative AI models, will tell you that machine learning models are not able to cope with imbalanced datasets and will therefore show poor performance. Hence, you need to do something about the imbalance if you want to accurately predict the minority class, which is usually the one you care about.

That might have been true 26 years ago, when we did not have ensemble methods and relied more heavily on individual decision trees and KNNs. But after the introduction of random forests and gradient-boosting machines, that is no longer the world we live in.

In fact, if the classes are linearly separable, their relative proportions will not prevent even a linear model such as logistic regression from separating them properly. For more complex patterns, where the classes are not linearly separable, ensemble models are perfectly capable of discriminating between them, even when the data is highly imbalanced.

Show me the data

To test this, I ran a simulation with imbalance ratios ranging from 10:1 to 5000:1, with a decent class separation.

For each ratio, I trained several commonly used classifiers, including logistic regression, random forests, XGBoost, LightGBM, CatBoost, and two gradient-boosting implementations from scikit-learn.

I trained these models with hyperparameters tuning, which is what we would do in practice anyway.

I then measured their ability to discriminate between the two classes using ROC-AUC with cross-validation, to obtain a measure of performance dispersion.

As we can see in the following image, the performance of the models did not decrease as we increased the imbalance ratio. If the classes can be separated by the features we have in the data, the models will perfectly do so, regardless of the class imbalance.

In the following images we see the performance of the most commonly used ensemble methods (random forest, XGBoost, Catboost, LightGBM) alongside 2 GBM implementations from scikit-learn and logistic regression. The imbalance ratio increases towards the right (x-axis). Performance is measured with the ROC-AUC. As you can see, apart from sklearn's GBM, all models achieve similar performance regardless of the class imbalance degree.

The widening error bands (shown as the shaded area surrounding the curves) reflect the increasing difficulty of assessing performance when the positive class is very rare. With fewer positive examples available for evaluation, the ROC-AUC estimate naturally becomes less precise. This is a limitation of the evaluation sample, not evidence that the models can no longer discriminate between the classes.

From my simulations, only Scikit-learn’s traditional gradient-boosting model seemed to be susceptible to imbalance, and only when the imbalance became really extreme.

Leo Breiman explains that in random forests, each tree is trained on a bootstrap sample of the original data. When the imbalance is extreme, some of those samples may contain very few, or even no minority-class observations. As a result, the corresponding trees have little opportunity to learn the minority class and may perform poorly when predicting it. Hence, we predicted, imbalance ratio may affect random forest performance. But in fact, he went on to test that, and he saw that when using the ROC as evaluation metric, he didn’t see performance degradation, and neither did we, when the hyperparameters are tuned properly.

Check out Leo’s discussion here:

Chen, C., Liaw, A., & Breiman, L. (2004). Using random forest to learn imbalanced data (Technical Report No. 666). Department of Statistics, University of California, Berkeley. https://statistics.berkeley.edu/sites/default/files/tech-reports/666.pdf

Conclusion

In short, when you have imbalanced datasets, reach out for any of these powerful classifiers, tune their hyperparameters, and evaluate them appropriately, using ROC curves and ROC-AUC or scoring functions like log loss and Brier score. With this framework, if your features are able to separate the data, so will your models.

You can find the code for the experiment described here at this link. If you like the repo, please give it a star.

If you liked this article, consider buying my new book: Imbalanced Data: Myths, Mistakes and Modern Solutions.

I hope this information was useful!

Wishing you a successful week ahead - see you next Monday! 👋🏻

Sole


While you are at it, check out our courses:

Stop aimless internet browsing. Start learning today with meticulously crafted courses offering a robust curriculum, fostering skill development with steadfast focus and efficiency.


Hi…I’m Sole
I’m the main instructor at Train in Data. My work as a data scientist includes creating and implementing machine learning models for evaluating insurance claims, managing credit risk, and detecting fraud. In 2018, I was honoured with a Data Science Leaders' award, and in 2019 and again in 2024, I was acknowledged as one of LinkedIn's voices in data science and analytics.

You are receiving this email because you subscribed to our newsletter, signed up on our website, purchased or downloaded any products from us.


Follow us on social media

Copyright (C) 2026 Train in Data. All rights reserved.


Unsubscribe · Preferences

Train in Data

by Soledad Galli, PhD | Data scientist | Python Developer | Best-selling instructor | Book author | 👉 www.trainindata.com

Read more from Train in Data
Class Weights Do Not Improve Model Performance

Welcome! Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, ormethod to help you stay sharp in the field. I hope you find it useful! My New Book is Out! Imbalanced Data, Myths, Mistakes and Modern Solutions Most advice about imbalanced data is incomplete, or simply wrong. This book challenges common assumptions and provides practical, evidence-based guidance for tackling class imbalance and building better machine learning models. Get the book Class...

Welcome! Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, ormethod to help you stay sharp in the field. I hope you find it useful! My New Book is Out! Imbalanced Data, Myths, Mistakes and Modern Solutions Most advice about imbalanced data is incomplete, or simply wrong. This book challenges common assumptions and provides practical, evidence-based guidance for tackling class imbalance and building better machine learning models. Find out more...

Welcome! Every Monday, I’ll drop a no-fluff, straight-to-the-point tip on a data science skill, tool, ormethod to help you stay sharp in the field. I hope you find it useful! The ROC Curve Myth for Imbalanced Datasets One myth I hear far too often is that the ROC curve is not a good metric for evaluating models trained on imbalanced datasets because it tends to produce overly optimistic ROC-AUC values. This is not true. Last Monday we saw that ROC curves are insensitive to class prevalence:...