← retour aux snippets

imblearn SMOTETomek: suréchantillonnage + nettoyage

combiner SMOTE avec TomekLinks pour nettoyer les frontières

imblearn SMOTETomek: suréchantillonnage + nettoyage

objectif

Expliquer et montrer comment combiner SMOTE avec TomekLinks pour nettoyer les frontières.

code minimal

from imblearn.combine import SMOTETomek
from sklearn.datasets import make_classification
X, y = make_classification(n_samples=3000, weights=[0.9, 0.1], n_features=20, random_state=0)
X_res, y_res = SMOTETomek(random_state=0).fit_resample(X, y)
X_res.shape, y_res.mean()

utilisation

# pipeline avec évaluation
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from imblearn.pipeline import Pipeline
pipe = Pipeline([("smotomek", SMOTETomek(random_state=0)), ("lr", LogisticRegression(max_iter=1000))])
scores = cross_val_score(pipe, X, y, cv=5, scoring="roc_auc")
float(scores.mean())

variante(s) utile(s)

# alternative: SMOTEENN
from imblearn.combine import SMOTEENN
SMOTEENN(random_state=0)

notes

  • TomekLinks retire des couples ambigus après suréchantillonnage.
  • Toujours encapsuler dans un pipeline pour CV correcte.