我正在使用GridSearchCV来识别随机森林分类器的最佳参数集 .
PARAMS = {
'max_depth': [8,None],
'n_estimators': [500,1000]
}
rf = RandomForestClassifier()
clf = grid_search.GridSearchCV(estimator=rf, param_grid=PARAMS, scoring='roc_auc', cv=5, n_jobs=4)
clf.fit(data, labels)
其中数据和标签分别是完整数据集和相应的标签 .
现在,我将GridSearchCV返回的性能(来自 clf.grid_scores_
)与"manual" AUC估算进行了比较:
aucs = []
for fold in range (0,n_folds):
probabilities = []
train_data,train_label = read_data(train_file_fold)
test_data,test_labels = read_data(test_file_fold)
clf = RandomForestClassifier(n_estimators = 1000,max_depth=8)
clf = clf.fit(train_data,train_labels)
predicted_probs = clf.predict_proba(test_data)
for value in predicted_probs:
for k, pr in enumerate(value):
if k == 1:
probabilities.append(pr)
fpr, tpr, thresholds = metrics.roc_curve(test_labels, probabilities, pos_label=1)
fold_auc = metrics.auc(fpr, tpr)
aucs.append(fold_auc)
performance = np.mean(aucs)
我手动将数据预分割成训练和测试集(同样的5 CV方法) .
当使用 RandomForest
的相同参数时, GridSearchCV
返回的AUC值是 always higher ,而不是手动计算的值(例如0.62对0.70) . 我知道不同的训练和测试分裂可能会给你不同的性能但是在测试100次重复的GridSearchCV时这种情况不断发生 . 有趣的是,如果我使用 accuarcy
而不是 roc_auc
作为评分指标,性能差异很小,并且可能与我使用不同的训练和测试集的事实相关联 . 这是否发生是因为 GridSearchCV
的AUC值是以与使用 metrics.roc_curve
不同的方式估算的?