SMOTE

SMOTE

最終更新 2026-03-03 読了時間 2 分
まとめ
  • SMOTE(Synthetic Minority Over-sampling Technique)は少数派クラスの近傍点を結ぶ線分上に合成サンプルを生成し、クラス不均衡を補正する。
  • 単純な複製(Random Over-sampling)と異なり、特徴空間を補間するため決定境界の過学習を抑える。
  • imbalanced-learnSMOTE / SMOTENC(カテゴリ特徴量対応)を使って実装する。

直感 #

クレジットカード不正検知では、正常取引が99%・不正が1%というデータが典型的。そのまま学習すると「全部正常」と予測するだけで正解率99%になってしまう。SMOTE は少数派サンプルとその近傍を結ぶ線分上にランダムに新しい点を生成し、少数派の密度を人工的に高めて均衡を取る。

詳細な解説 #

ライブラリとデータ #

1
2
3
4
5
6
7
8
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
from sklearn.ensemble import RandomForestClassifier
from imblearn.over_sampling import SMOTE
from collections import Counter

不均衡データの作成 #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
X, y = make_classification(
    n_samples=2000, n_features=10, n_informative=5,
    weights=[0.95, 0.05], flip_y=0, random_state=42
)
print("元のクラス分布:", Counter(y))
# Counter({0: 1900, 1: 100})

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)

SMOTE の適用 #

1
2
3
smote = SMOTE(sampling_strategy="auto", k_neighbors=5, random_state=42)
X_resampled, y_resampled = smote.fit_resample(X_train, y_train)
print("SMOTE 後:", Counter(y_resampled))

効果の比較 #

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# SMOTE なし
rf_base = RandomForestClassifier(n_estimators=100, random_state=42)
rf_base.fit(X_train, y_train)
print("=== SMOTE なし ===")
print(classification_report(y_test, rf_base.predict(X_test)))

# SMOTE あり
rf_smote = RandomForestClassifier(n_estimators=100, random_state=42)
rf_smote.fit(X_resampled, y_resampled)
print("=== SMOTE あり ===")
print(classification_report(y_test, rf_smote.predict(X_test)))

SMOTE の派生手法 #

手法特徴使いどころ
SMOTEk近傍の線分上に合成基本手法
Borderline-SMOTE境界付近のサンプルのみ補間決定境界の改善
ADASYN学習困難な領域を重点的に補間密度適応
SMOTENCカテゴリ特徴量に対応混合データ
SMOTE + Tomek Linksオーバーサンプリング + ノイズ除去クリーンな境界

注意点 #

  • SMOTE は訓練データにのみ適用する。テストデータには絶対に使わない
  • パイプライン内で imblearn.pipeline.Pipeline を使い、交差検証のリークを防ぐ
  • 特徴量数が多い場合は次元の呪いで合成サンプルの品質が下がる