用扩展的测试数据进行预测?

时间:2020-03-10 13:34:19

标签: python pipeline scaling imbalanced-data imblearn

我的分类问题不平衡。首先,我想缩放数据,然后通过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的未缩放版本进行预测有关(我知道不对测试数据使用重采样,但是我不确定缩放比例))。我的代码正确吗,或者有任何问题导致这种有趣的结果?

0 个答案:

没有答案