Registry / ai-ml / imblearn

imblearn

JSON →
library0.0pypypi✓ verified 21d ago

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-learn
INSTALL
IMPORT
SIG · IMBLEARN
I
imblearn
ai-mlpythonv0.0
Install
10.1s avg
Import
4376ms
Disk
467MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v? · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
build_error
glibc
py 3.103.95 runs
installs and imports cleanly · install 10.1s · import 4.376s · 454MB
467MB installed
● package 467MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

imblearn
import imblearn
SMOTE
from imblearn.over_sampling import SMOTE
RandomOverSampler
from imblearn.over_sampling import RandomOverSampler
RandomUnderSampler
from imblearn.under_sampling import RandomUnderSampler
Pipeline
from imblearn.pipeline import Pipeline
imblearn
import imblearn
A common mistake is trying to 'pip install imblearn' directly, which is an old, empty PyPI package. The correct package name for installation is 'imbalanced-learn', though it's imported as 'imblearn'.

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.

from sklearn.datasets import make_classification from collections import Counter from imblearn.over_sampling import SMOTE # Generate an imbalanced dataset X, y = make_classification( n_samples=1000, n_features=2, n_informative=2, n_redundant=0, n_repeated=0, n_classes=2, n_clusters_per_class=1, weights=[0.9, 0.1], flip_y=0, random_state=42 ) print(f"Original dataset shape: {Counter(y)}") # Apply SMOTE to oversample the minority class smote = SMOTE(random_state=42) X_resampled, y_resampled = smote.fit_resample(X, y) print(f"Resampled dataset shape: {Counter(y_resampled)}")
Debug
Known issues
breakingThe `ratio` and `return_indices` parameters have been removed from all samplers. Users should now use the `sampling_strategy` parameter instead. This can break code written for older versions.
fix
Replace `ratio` and `return_indices` with `sampling_strategy` (e.g., `sampling_strategy='auto'`, `'minority'`, `'not majority'`, or a dictionary specifying target counts).
affects: < 0.6.0
breakingThe `imblearn.ensemble.BalanceCascade` and `imblearn.ensemble.EasyEnsemble` classes were removed after two deprecation cycles. Code relying on these will fail.
fix
Migrate to alternative ensemble methods or updated API for similar functionality.
affects: >= 0.7.0
deprecatedThe `n_jobs` parameter in `imblearn.under_sampling.ClusterCentroids` is deprecated due to its deprecation in `sklearn.cluster.KMeans` (which it relies on).
fix
Avoid using the `n_jobs` parameter in `ClusterCentroids` and rely on scikit-learn's global joblib configuration or alternative parallelization strategies if needed.
affects: >= 0.9.0
gotchaThe `imblearn.pipeline.Pipeline`'s internal `check_is_fitted` mechanism for checking if the pipeline is fitted will change from a warning to an error in version 0.15. This change aligns with scikit-learn's behavior.
fix
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.
affects: 0.14.x
gotchaimbalanced-learn has strong compatibility requirements with specific `scikit-learn` versions. Installing incompatible versions can lead to `FutureWarning`s or runtime errors.
fix
Always check the official documentation for the precise `scikit-learn` version compatibility for your `imbalanced-learn` version. Upgrade both packages concurrently if issues arise.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'imblearn'
The `imbalanced-learn` package has not been installed in the current Python environment, or the environment where it was installed is not active.
fix
Install the package using pip: `pip install imbalanced-learn` or, if using Anaconda, `conda install -c conda-forge imbalanced-learn`.
AttributeError: 'SMOTE' object has no attribute 'fit_sample'
The `fit_sample` method has been deprecated and removed in newer versions of `imbalanced-learn` (and scikit-learn) in favor of `fit_resample`.
fix
Replace `sampler.fit_sample(X, y)` with `sampler.fit_resample(X, y)`.
ValueError: Expected n_neighbors <= n_samples, but n_samples = X and n_neighbors = Y
This error occurs in algorithms like SMOTE or NearMiss when the number of samples in the minority class (`n_samples`) is less than or equal to the `k_neighbors` parameter, making it impossible to find enough neighbors to generate synthetic samples.
fix
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.
AttributeError: This 'Pipeline' has no attribute 'transform'
When using `imblearn.pipeline.Pipeline`, if the last step is a sampler (like SMOTE), it typically implements `fit_resample` but not `transform`. Standard scikit-learn pipelines expect all but the last step to have `transform`.
fix
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.
ImportError: cannot import name 'SMOTE' from 'imblearn'
The 'SMOTE' class is located within the 'imblearn.over_sampling' submodule, not directly under 'imblearn'.
fix
from imblearn.over_sampling import SMOTE
Upgrade
Version history
0.0latest on PyPI · released Jan 19, 2017
Audit
Dependencies
PythonrequiredRuntime environment
NumPyrequiredNumerical operations
SciPyrequiredScientific computing
Scikit-learnrequiredCore machine learning algorithms and API compatibility
PandasoptionalFor DataFrame input/output handling
TensorflowoptionalFor Keras/TensorFlow integratio
KerasoptionalFor Keras/TensorFlow integration
Agent activity
12 hits · last 30 days
node
10
Perplexity
1
Resources