imbalanced-learn-extra

imbalanced-learn-extra extends imbalanced-learn with oversamplers it leaves out, usually because they are newer or less cited. It has two main parts.
The first is a general interface for clustering-based oversampling. SMOTE and its variants only look at the imbalance between classes, but a class can also be imbalanced within itself. Clustering the data first helps with that. You can combine any clusterer with any oversampler, so here is k-means with SMOTE:
from collections import Counter
from imblearn.over_sampling import SMOTE
from imblearn_extra.clover.over_sampling import ClusterOverSampler
from sklearn.cluster import KMeans
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
print(Counter(y.tolist()))
oversampler = ClusterOverSampler(
oversampler=SMOTE(random_state=0), clusterer=KMeans(n_clusters=10, random_state=0, n_init=10)
)
_, y_resampled = oversampler.fit_resample(X, y)
print(Counter(y_resampled.tolist()))Counter({1: 357, 0: 212})
Counter({0: 357, 1: 357})
The second is Geometric SMOTE. SMOTE creates new samples by interpolating on a line between existing ones. Geometric SMOTE expands that area, and it works as a drop-in replacement:
from collections import Counter
from imblearn_extra.gsmote import GeometricSMOTE
from sklearn.datasets import load_breast_cancer
X, y = load_breast_cancer(return_X_y=True)
_, y_resampled = GeometricSMOTE(random_state=0).fit_resample(X, y)
print(Counter(y_resampled.tolist()))Counter({0: 357, 1: 357})
Both follow the imbalanced-learn API, so they fit into scikit-learn style pipelines. Install it with pip install imbalanced-learn-extra.