【发布时间】:2016-02-12 01:10:14
【问题描述】:
我对 cross_val_score 评分指标“roc_auc”和我可以直接导入和调用的 roc_auc_score 之间的区别感到困惑。
文档 (http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter) 表明指定 score='roc_auc' 将使用 sklearn.metrics.roc_auc_score。但是,当我使用 score='roc_auc' 实现 GridSearchCV 或 cross_val_score 时,我收到的数字与我直接调用 roc_auc_score 时非常不同。
这是我的代码,用于帮助演示我所看到的:
# score the model using cross_val_score
rf = RandomForestClassifier(n_estimators=150,
min_samples_leaf=4,
min_samples_split=3,
n_jobs=-1)
scores = cross_val_score(rf, X, y, cv=3, scoring='roc_auc')
print scores
array([ 0.9649023 , 0.96242235, 0.9503313 ])
# do a train_test_split, fit the model, and score with roc_auc_score
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
rf.fit(X_train, y_train)
print roc_auc_score(y_test, rf.predict(X_test))
0.84634039111363313 # quite a bit different than the scores above!
我觉得我在这里遗漏了一些非常简单的东西——很可能是我在实施/解释其中一个评分指标时的错误。
谁能解释这两个评分指标之间存在差异的原因?
【问题讨论】:
-
我也完全被这种差异搞糊涂了。我还尝试使用标准的 make_scorer() 函数将分数函数转换为 cross_val_score 的正确 Scorer 对象,但结果是相同的。 make_scorer() 给出了与我的手动实现相同的结果,而 'roc_auc' 给出了更高的分数。幸运的是,在我的示例中,差异只有百分之几,与您的不同,但仍然:我应该信任哪个函数?
标签: python machine-learning scikit-learn random-forest cross-validation