imbalanced-learn is a Python library that provides a comprehensive suite of resampling techniques to address imbalanced datasets in machine learning, where one class significantly outnumbers another. It offers methods for over-sampling (e.g., SMOTE, ADASYN), under-sampling (e.g., NearMiss, EditedNearestNeighbours), and combined approaches, along with ensemble methods tailored for imbalanced data. It is compatible with scikit-learn and is part of scikit-learn-contrib projects. The current stable version is 0.14.1, with regular maintenance releases to ensure compatibility with scikit-learn and Python versions.
pip install imbalanced-learnVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to use SMOTE (Synthetic Minority Over-sampling Technique) to balance an imbalanced dataset generated with scikit-learn. It first creates a dataset with a 90:10 class distribution and then applies SMOTE to equalize the number of samples in both classes.
Replace `ratio` and `return_indices` with `sampling_strategy` (e.g., `sampling_strategy='auto'`, `'minority'`, `'not majority'`, or a dictionary specifying target counts).
Migrate to alternative ensemble methods or updated API for similar functionality.
Avoid using the `n_jobs` parameter in `ClusterCentroids` and rely on scikit-learn's global joblib configuration or alternative parallelization strategies if needed.
Ensure your pipeline is properly fitted before calling methods like `transform` or `predict` to avoid future errors. This often means calling `.fit()` before any other operations on the pipeline.
Always check the official documentation for the precise `scikit-learn` version compatibility for your `imbalanced-learn` version. Upgrade both packages concurrently if issues arise.
Install the package using pip: `pip install imbalanced-learn` or, if using Anaconda, `conda install -c conda-forge imbalanced-learn`.
Replace `sampler.fit_sample(X, y)` with `sampler.fit_resample(X, y)`.
Reduce the `k_neighbors` parameter to be less than the number of samples in the smallest class, or use a different over-sampling method like `RandomOverSampler` if the minority class is extremely small.
Call `pipeline.fit_resample(X, y)` on the `imblearn` pipeline, as samplers primarily use `fit_resample` for both fitting and transforming. If you need a `transform` method, ensure the sampler is not the very last step, or explicitly separate the resampling and subsequent transformation steps.
from imblearn.over_sampling import SMOTE