首页 > 解决方案 > 是否使用缩放的测试数据进行预测?

问题描述

我有一个不平衡的分类问题。首先,我想缩放数据,然后通过 SMOTE 重新采样。为了防止数据泄漏,我使用了管道。我的代码是:

X_train, X_test, Y_train, y_test = train_test_split(X, y, test_size = 0.20, random_state = 0, stratify=y)
dict = {0: 0.33421052631578946, 1: 0.6657894736842105}
score={'AUC':'roc_auc', 
           'RECALL':'recall',
           'PRECISION':'precision',
           'F1':'f1',
       'ACC':'accuracy',
        'BACC':'balanced_accuracy',
      }

params = [{'randomforestclassifier__n_estimators': [50,100, 200, 250, 300, 350],
 'randomforestclassifier__max_features': ['sqrt','auto', 'log2', 0.8],
 'randomforestclassifier__max_depth': [1,5,10, 20, 30, 40, 50],
 'randomforestclassifier__min_samples_leaf': [1, 2, 4, 5, 10, 20],
 'randomforestclassifier__min_samples_split': [0.1,0.5,1,5, 10, 12]}
  ]

skfold = StratifiedKFold(n_splits=5, random_state=13)

pipeline = make_pipeline(RobustScaler(), TomekLinks(), RandomForestClassifier(random_state=13, class_weight=dict))

#grid search
gcv_rf2 = GridSearchCV(estimator=pipeline, param_grid=params,
            cv=skfold, scoring=score, n_jobs=12,
            refit='F1', verbose=1,
            return_train_score=True)

gcv_rf2.fit(X_train, y_train)
y_hat = gcv_rf2.predict(X_test)

print(classification_report(y_test, y_hat))

问题是正类的结果不太好,我认为这与使用未缩放的X_test预测版本有关(我知道不对测试数据使用重采样,但我不确定缩放))。我的代码是正确的还是有任何问题导致这个不有趣的结果?

标签: pythonpipelinescalingimbalanced-dataimblearn

解决方案


推荐阅读