Last modified: Jun 01, 2025 By Alexander Williams
Install imbalanced-learn in Python Easily
Imbalanced-learn is a Python library for handling imbalanced datasets. It works with scikit-learn and provides tools for resampling and metrics.
This guide will show you how to install imbalanced-learn easily. We'll cover different installation methods and provide examples.
Prerequisites for imbalanced-learn
Before installing imbalanced-learn, you need Python 3.6 or later. You should also have scikit-learn installed as it's a dependency.
If you need to install scikit-learn first, use this command:
pip install scikit-learn
Install imbalanced-learn Using pip
The easiest way to install imbalanced-learn is with pip. Run this command in your terminal:
pip install imbalanced-learn
This will install the latest stable version. It includes all necessary dependencies.
Install imbalanced-learn with Conda
If you use Anaconda or Miniconda, you can install it from conda-forge:
conda install -c conda-forge imbalanced-learn
This method is recommended for Anaconda users as it handles dependencies better.
Verify the Installation
After installation, verify it works by importing it in Python:
import imblearn
print(imblearn.__version__)
0.9.0 # Example output
Basic Usage Example
Here's a simple example using SMOTE for oversampling:
from imblearn.over_sampling import SMOTE
from sklearn.datasets import make_classification
X, y = make_classification(n_classes=2, weights=[0.1, 0.9])
smote = SMOTE()
X_res, y_res = smote.fit_resample(X, y)
This code creates a synthetic dataset and applies SMOTE resampling.
Troubleshooting Common Issues
If you get dependency errors, try upgrading pip first:
pip install --upgrade pip
For scikit-learn conflicts, check our guide on installing XGBoost which covers similar issues.
Alternative Installation Methods
For advanced users, you can install from source:
git clone https://github.com/scikit-learn-contrib/imbalanced-learn.git
cd imbalanced-learn
pip install .
This is useful if you need the latest features not in the stable release.
Integrating with Other Libraries
imbalanced-learn works well with other ML libraries. For example, you can combine it with CatBoost or LightGBM.
Here's an example pipeline:
from imblearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestClassifier
pipeline = Pipeline([
('smote', SMOTE()),
('classifier', RandomForestClassifier())
])
Conclusion
Installing imbalanced-learn is straightforward with pip or conda. It's a powerful tool for handling imbalanced datasets in machine learning.
For more Python installation guides, check our articles on PyCaret NLP and other machine learning tools.