我对cross_val_score评分指标'roc_auc‘和我可以直接导入和调用的roc_auc_score之间的区别感到困惑。
文档(http://scikit-learn.org/stable/modules/model_evaluation.html#scoring-parameter)指出,指定scoring='roc_auc‘将使用sklearn.metrics.roc_auc_score。但是,当我使用scoring='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!我觉得我在这里遗漏了一些非常简单的东西--很可能是我在如何实现/解释其中一个评分指标时的错误。
有没有人能解释一下这两个评分标准之间存在差异的原因?
发布于 2016-04-29 23:42:37
这是因为您提供了预测y,而不是roc_auc_score中的概率。此函数接受分数,而不是分类标签。试着这样做:
print roc_auc_score(y_test, rf.predict_proba(X_test)[:,1])它应该会给出与cross_val_score之前的结果类似的结果。Refer to this post for more info。
发布于 2015-11-11 09:04:22
我刚刚遇到了一个类似的问题here。关键的要点是,cross_val_score使用KFold策略和默认参数进行训练测试拆分,这意味着拆分成连续的块,而不是混洗。另一方面,train_test_split进行了随机拆分。
解决方案是使拆分策略显式并指定shuffling,如下所示:
shuffle = cross_validation.KFold(len(X), n_folds=3, shuffle=True)
scores = cross_val_score(rf, X, y, cv=shuffle, scoring='roc_auc')发布于 2016-10-26 00:05:14
我自己也遇到了这个问题,在挖掘了一下之后,我找到了答案。为爱而分享。
实际上有两个半的问题。
你需要使用相同的train/test);
roc_auc_score中(使用predict_proba()方法)。但是,一些估计器(如SVC)没有predict_proba()方法,您可以使用decision_function()方法。下面是一个完整的示例:
# Let's use the Digit dataset
digits = load_digits(n_class=4)
X,y = digits.data, digits.target
y[y==2] = 0 # Increase problem dificulty
y[y==3] = 1 # even more使用两个估计器
LR = LogisticRegression()
SVM = LinearSVC()拆分训练/测试集。但是要把它保存在一个我们可以重复使用的变量中。
fourfold = StratifiedKFold(n_splits=4, random_state=4)将其提供给GridSearchCV并保存分数。请注意,我们传递的是fourfold。
gs = GridSearchCV(LR, param_grid={}, cv=fourfold, scoring='roc_auc', return_train_score=True)
gs.fit(X,y)
gs_scores = np.array([gs.cv_results_[k][0] for k in gskeys])将其提供给cross_val_score并保存分数。
cv_scores = cross_val_score(LR, X, y, cv=fourfold, scoring='roc_auc')有时,您希望循环并计算几个不同的分数,所以这就是您要使用的。
loop_scores = list()
for idx_train, idx_test in fourfold.split(X, y):
X_train, y_train, X_test, y_test = X[idx_train], y[idx_train], X[idx_test], y[idx_test]
LR.fit(X_train, y_train)
y_prob = LR.predict_proba(X_test)
auc = roc_auc_score(y_test, y_prob[:,1])
loop_scores.append(auc)我们在所有方面的分数都一样吗?
print [((a==b) and (b==c)) for a,b,c in zip(gs_scores,cv_scores,loop_scores)]
>>> [True, True, True, True]
但是,有时我们的估计器没有predict_proba()方法。因此,根据这个example,我们这样做:
for idx_train, idx_test in fourfold.split(X, y):
X_train, y_train, X_test, y_test = X[idx_train], y[idx_train], X[idx_test], y[idx_test]
SVM.fit(X_train, y_train)
y_prob = SVM.decision_function(X_test)
prob_pos = (y_prob - y_prob.min()) / (y_prob.max() - y_prob.min())
auc = roc_auc_score(y_test, prob_pos)https://stackoverflow.com/questions/33642158
复制相似问题