如何在sklearn中使用交叉验证执行SMOTE

EmJ*_*EmJ 2 python classification machine-learning scikit-learn cross-validation

我有一个高度不平衡的数据集,并希望执行SMOTE来平衡数据集并进行交叉验证以测量准确性。但是,大多数现有教程仅利用单次trainingtesting迭代来执行SMOTE。

因此,我想知道使用交叉验证执行SMOTE的正确过程。

我当前的代码如下。但是,如上所述,它仅使用一次迭代。

from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
sm = SMOTE(random_state=2)
X_train_res, y_train_res = sm.fit_sample(X_train, y_train.ravel())
clf_rf = RandomForestClassifier(n_estimators=25, random_state=12)
clf_rf.fit(x_train_res, y_train_res)
Run Code Online (Sandbox Code Playgroud)

如果需要,我很乐意提供更多详细信息。

gmd*_*mds 8

您需要每个折叠中执行SMOTE 。因此,您需要避免train_test_split赞成KFold

from sklearn.model_selection import KFold
from imblearn.over_sampling import SMOTE
from sklearn.metrics import f1_score

kf = KFold(n_splits=5)

for fold, (train_index, test_index) in enumerate(kf.split(X), 1):
    X_train = X[train_index]
    y_train = y[train_index]  # Based on your code, you might need a ravel call here, but I would look into how you're generating your y
    X_test = X[test_index]
    y_test = y[test_index]  # See comment on ravel and  y_train
    sm = SMOTE()
    X_train_oversampled, y_train_oversampled = sm.fit_sample(X_train, y_train)
    model = ...  # Choose a model here
    model.fit(X_train_oversampled, y_train_oversampled )  
    y_pred = model.predict(X_test)
    print(f'For fold {fold}:')
    print(f'Accuracy: {model.score(X_test, y_test)}')
    print(f'f-score: {f1_score(y_test, y_pred)}')
Run Code Online (Sandbox Code Playgroud)

例如,您也可以将分数附加到list定义的外部。

  • 注意:您可能希望使用`StratifiedKFold`来代替其他答案,因为您可能遇到类不平衡的问题。 (2认同)

小智 5

from sklearn.model_selection import StratifiedKFold
from imblearn.over_sampling import SMOTE

cv = StratifiedKFold(n_splits=5)
for train_idx, test_idx, in cv.split(X, y):
    X_train, y_train = X[train_idx], y[train_idx]
    X_test, y_test = X[test_idx], y[test_idx]
    X_train, y_train = SMOTE().fit_sample(X_train, y_train)
    ....
Run Code Online (Sandbox Code Playgroud)