Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning

Guillaume LemaitreFernando NogueiraChristos K. Aridas

article2017JMLR2,621 citations

Presents imbalanced-learn, a Python library integrated with scikit-learn that provides standardized implementations of over-sampling, under-sampling, and ensemble algorithms to effectively train machine learning models on skewed class distributions.

Listen

Real-world datasets in fields such as fraud detection, medical diagnosis, and bioinformatics often contain far fewer examples of one class than others. This imbalance disrupts standard machine learning algorithms that assume roughly equal class sizes or costs, leading to poor performance on the minority class. The problem ranks among the top ten challenges in data mining and pattern recognition.

The article set out to introduce and describe an open-source Python toolbox that supplies a range of established methods for correcting class imbalance. The toolbox aims to fill a gap in the Python ecosystem by offering tools that integrate directly with widely used libraries.

The authors built the imbalanced-learn package around four categories of techniques: under-sampling, over-sampling, combined sampling, and ensemble approaches. Development followed scikit-learn conventions, relied solely on numpy, scipy, and scikit-learn, and incorporated unit tests achieving 99 percent coverage, continuous integration, and community review processes. At the time of writing, the repository attracted roughly 2,000 visits and 300 unique visitors each week.

The toolbox supplies fixed and cleaning under-sampling routines, random and SMOTE-based over-sampling with several variants, combinations that pair SMOTE with cleaning steps, and two ensemble methods that reuse majority-class samples across multiple balanced subsets. All samplers expose a consistent fit, sample, and fit-sample interface, and a Pipeline class allows seamless chaining with scikit-learn transformers and estimators.

These capabilities give practitioners a single, maintained Python resource for balancing data before model training, reducing the need to switch languages or implement methods from scratch. Integration with scikit-learn lowers adoption barriers and supports reproducible workflows in production environments.

The authors plan to add further prototype-selection and generation methods along with additional user guides. Users should monitor the GitHub repository for these extensions and test the current release on their own datasets before large-scale deployment.

The description focuses on implementation and design rather than new empirical benchmarks, so performance gains will vary by data set and downstream classifier. Readers should verify results on representative samples and consider the MIT license and scikit-learn-contrib status when evaluating long-term maintenance.

Cover for Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning

Abstract

Imbalanced-learn is an open-source python toolbox aiming at providing a wide range of methods to cope with the problem of imbalanced dataset frequently encountered in machine learning and pattern recognition. The implemented state-of-the-art methods can be categorized into 4 groups: (i) under-sampling, (ii) over-sampling, (iii) combination of over- and under-sampling, and (iv) ensemble learning methods. The proposed toolbox only depends on numpy, scipy, and scikit-learn and is distributed under MIT license. Furthermore, it is fully compatible with scikit-learn and is part of the scikit-learn-contrib supported project. Documentation, unit tests as well as integration tests are provided to ease usage and contribution. The toolbox is publicly available in GitHub: this https URL.

Table of Contents

  • 1 Introduction
  • 2 Project management
  • 3 Implementation design
  • 4 Implemented methods
  • 4.1 Notation and background
  • 4.2 Under-sampling
  • 4.3 Over-sampling
  • 4.4 Combination of over- and under-sampling
  • 4.5 Ensemble learning
  • 5 Future plans and conclusion
  • References

Knowls

  1. Knowl 1 — API Architecture and Sampler Interface of imbalanced-learn

    model/method

    The imbalanced-learn Python library provides a modular interface for class imbalance mitigation designed to integrate with the scikit-learn ecosystem. Resampling algorithms derive from dedicated sampler classes that expose three primary methods:

    • fit(X, y): Analyzes the training feature matrix XX and target label vector yy to compute internal parameters and decision statistics necessary for resampling.
    • sample(X, y): Applies the computed resampling strategy to XX and yy, returning balanced feature and target arrays according to a target balancing ratio.
    • fit_sample(X, y): A convenience method that executes fit(X, y) followed immediately by sample(X, y) on the input dataset.

    To allow integration with standard learning workflows, the library provides a custom Pipeline class subclassed from scikit-learn that allows samplers to be chained sequentially with feature transformers and estimators.

  2. Knowl 2 — Balancing Ratio for Imbalanced Datasets

    definition

    For a binary classification dataset χ\chi partitioned into a minority class subset χmin\chi_{\text{min}} and a majority class subset χmaj\chi_{\text{maj}}, the dataset balancing ratio rχr_\chi is defined as:

    rχ=χminχmajr_\chi = \frac{|\chi_{\text{min}}|}{|\chi_{\text{maj}}|}

    where |\cdot| denotes set cardinality. A data balancing procedure transforms χ\chi into a resampled dataset χres\chi_{\text{res}} with a targeted balancing ratio rχresr_{\chi_{\text{res}}} by adjusting the relative sizes of the minority and majority classes.

  3. Knowl 3 — Fixed vs. Cleaning Under-Sampling Taxonomy

    model/method

    Under-sampling reduces the majority class sample size χmaj|\chi_{\text{maj}}| to balance class distributions. Implemented under-sampling approaches are categorized into two paradigms:

    • Fixed under-sampling: Directly downsamples the majority class to satisfy a specified balancing ratio rχresr_{\chi_{\text{res}}}. Selection criteria include uniform random sampling, cluster centroid substitution, nearest neighbour rules (e.g., NearMiss variants), and classification accuracy ranking via instance hardness thresholds.
    • Cleaning under-sampling: Does not target a predetermined balancing ratio rχresr_{\chi_{\text{res}}}, but instead refines the feature space by removing noisy, ambiguous, or borderline majority samples based on local neighborhood criteria. Methods include Condensed Nearest Neighbours (CNN), Edited Nearest Neighbours (ENN), One-Sided Selection (OSS), Neighbourhood Cleaning Rule (NCR), and Tomek links.
  4. Knowl 4 — Over-Sampling Techniques for Minority Class Augmentation

    model/method

    Over-sampling increases the number of minority samples in χmin\chi_{\text{min}} until a desired balancing ratio rχresr_{\chi_{\text{res}}} is reached. The toolbox implements two main classes of over-sampling:

    • Random over-sampling: Randomly duplicates existing instances from χmin\chi_{\text{min}} with replacement until the target class balance is achieved.
    • SMOTE (Synthetic Minority Over-sampling Technique): Synthesizes new feature vectors by interpolating along the line segments joining minority class instances and their nearest neighbours within χmin\chi_{\text{min}}.
    • SMOTE variants: Specialized heuristics including Borderline-SMOTE (Borderline 1 and Borderline 2, which selectively synthesize points near decision boundaries) and SVM-SMOTE (which generates synthetic instances using support vectors established by a Support Vector Machine).
  5. Knowl 5 — Hybrid Sampling Combining Over-Sampling and Cleaning Under-Sampling

    model/method

    Synthetic over-sampling via SMOTE can induce overfitting and produce noisy samples within majority class regions. Hybrid methods mitigate this by applying a cleaning under-sampling stage immediately following SMOTE generation:

    • SMOTE + Tomek links: First expands the minority class using SMOTE, then detects and removes all Tomek links (pairs of nearest neighbor instances belonging to opposing classes) to clarify decision boundaries.
    • SMOTE + Edited Nearest Neighbours (ENN): First expands the minority class using SMOTE, then applies ENN to eliminate any instance whose class label disagrees with the majority class of its nearest neighbours.
  6. Knowl 6 — Ensemble Under-Sampling via EasyEnsemble and BalanceCascade

    model/method

    To prevent information loss inherent to discarding majority instances during under-sampling, ensemble under-sampling creates multiple balanced subsets to train a classifier collection:

    • EasyEnsemble: Repeatedly draws independent random under-sampled subsets of the majority class χmaj\chi_{\text{maj}}, pairs each with the entire minority class χmin\chi_{\text{min}} to construct multiple balanced subsets with ratio rχresr_{\chi_{\text{res}}}, and trains an ensemble of learners.
    • BalanceCascade: Constructs balanced training subsets sequentially using feedback from intermediate classifiers. At each stage, a classifier is trained on a balanced subset; majority class samples correctly classified by the current ensemble are removed from future consideration, while misclassified majority samples are retained for subsequent sampling stages.

Coverage note — No substantial contributed material was omitted. The software's project infrastructure details (e.g., Travis CI, test coverage percentage, and documentation tools) were omitted as standard non-methodological engineering metadata.

References

  1. 1.G. E. Batista, A. L. Bazzan, and M. C. Monard. Balancing training data for automated annotation of keywords: a case study. In WOB, pages 10–18, 2003.
  2. 2.N. V. Chawla, K. W. Bowyer, L. O. Hall, and W. P. Kegelmeyer. SMOTE: synthetic minority over-sampling technique. Journal of artificial intelligence research, pages 321–357, 2002.
  3. 3.A. Dal Pozzolo, O. Caelen, S. Waterschoot, and G. Bontempi. Racing for unbalanced methods selection. In International Conference on Intelligent Data Engineering and Automated Learning, pages 24–31. Springer, 2013.
  4. 4.H. Han, W.-Y. Wang, and B.-H. Mao. Borderline-smote: a new over-sampling method in imbalanced data sets learning. In International Conference on Intelligent Computing, pages 878–887. Springer, 2005.
  5. 5.P. Hart. The condensed nearest neighbor rule. Information Theory, IEEE Transactions on, 14(3):515–516, May 1968.
  6. 6.H. He and E. Garcia. Learning from imbalanced data. Knowledge and Data Engineering, IEEE Transactions on, 21(9):1263–1284, 2009.
  7. 7.M. Kubat, S. Matwin, et al. Addressing the curse of imbalanced training sets: one-sided selection. In International Conference in Machine Learning, volume 97, pages 179–186. Nashville, USA, 1997.
  8. 8.M. Kuhn. Caret: classification and regression training. Astrophysics Source Code Library, 1:05003, 2015.
  9. 9.J. Laurikkala. Improving identification of difficult small classes by balancing class distribution. Springer, 2001.
  10. 10.X.-Y. Liu, J. Wu, and Z.-H. Zhou. Exploratory undersampling for class-imbalance learning. IEEE Transactions on Systems, Man, and Cybernetics, Part B (Cybernetics), 39(2):539–550, 2009.
  11. 11.I. Mani and I. Zhang. knn approach to unbalanced data distributions: a case study involving information extraction. In Proceedings of Workshop on Learning from Imbalanced Datasets, 2003.
  12. 12.H. M. Nguyen, E. W. Cooper, and K. Kamei. Borderline over-sampling for imbalanced data classification. International Journal of Knowledge Engineering and Soft Data Paradigms, 3(1):4–21, 2011.
  13. 13.F. Pedregosa, G. Varoquaux, A. Gramfort, V. Michel, B. Thirion, O. Grisel, M. Blondel, P. Prettenhofer, R. Weiss, V. Dubourg, et al. Scikit-learn: Machine learning in python. Journal of Machine Learning Research, 12(Oct):2825–2830, 2011.
  14. 14.R. C. Prati, G. E. Batista, and M. C. Monard. Data mining with imbalanced class distributions: concepts and methods. In Indian International Conference Artificial Intelligence, pages 359–376, 2009.
  15. 15.M. Rastgoo, G. Lemaitre, J. Massich, O. Morel, F. Marzani, R. Garcia, and F. Meriaudeau. Tackling the problem of data imbalancing for melanoma classification. In Bioimaging, 2016.
  16. 16.M. R. Smith, T. Martinez, and C. Giraud-Carrier. An instance level analysis of data complexity. Machine learning, 95(2):225–256, 2014.
  17. 17.S. C. Sonnenburg, S. Henschel, C. Widmer, J. Behr, A. Zien, F. de Bona, A. Binder, C. Gehl, V. Franc, et al. The SHOGUN machine learning toolbox. Journal of Machine Learning Research, 11(Jun):1799–1802, 2010.
  18. 18.I. Tomek. Two modifications of CNN. Systems, Man, and Cybernetics, IEEE Transactions on, 6:769–772, 1976.
  19. 19.L. Torgo. Data mining with R: learning with case studies. Chapman & Hall/CRC, 2010.
  20. 20.D. L. Wilson. Asymptotic properties of nearest neighbor rules using edited data. Systems, Man and Cybernetics, IEEE Transactions on, (3):408–421, 1972.
  21. 21.Q. Yang and X. Wu. 10 challenging problems in data mining research. International Journal of Information Technology & Decision Making, 5(04):597–604, 2006.

Citation

MLA
Lemaître, G., et al. “Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning”. Journal of Machine Learning Research, vol. 18, no. 17, 2017, pp. 1–5, https://www.jmlr.org/papers/v18/16-365.html.
APA
Lemaître, G., Nogueira, F., & Aridas, C. K. (2017). Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning. Journal of Machine Learning Research, 18(17), 1–5. https://www.jmlr.org/papers/v18/16-365.html
Chicago
Lemaître, G., F. Nogueira, and C. K. Aridas. 2017. “Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning”. Journal of Machine Learning Research 18 (17): 1–5. https://www.jmlr.org/papers/v18/16-365.html.
Harvard
Lemaître, G., Nogueira, F. and Aridas, C.K. (2017) “Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning”, Journal of Machine Learning Research, 18(17), pp. 1–5. Available at: https://www.jmlr.org/papers/v18/16-365.html.
Vancouver
1. Lemaître G, Nogueira F, Aridas CK (2017) Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning. Journal of Machine Learning Research 18:1–5

BibTeX

@article{JMLR:v18:16-365,
  author  = {Guillaume  Lema{{\^i}}tre and Fernando Nogueira and Christos K. Aridas},
  title   = {Imbalanced-learn: A Python Toolbox to Tackle the Curse of Imbalanced Datasets in Machine Learning},
  journal = {Journal of Machine Learning Research},
  year    = {2017},
  volume  = {18},
  number  = {17},
  pages   = {1--5},
  url     = {http://jmlr.org/papers/v18/16-365.html}
}
Metadata:DOI registry

Source Code

This paper has an official code repository available. Click below to access the source code.

View Repository

Access the Paper

This paper is available from its original source. Click below to access the PDF.

Open PDF

License: https://creativecommons.org/licenses/by/4.0/