【问题标题】:roc_auc score method with LeaveOneOut in scikit-learnscikit-learn 中带有 LeaveOneOut 的 roc_auc 评分方法
【发布时间】:2016-04-04 12:29:47
【问题描述】:

在 scikit-learn 中,GridSearchCV() 支持 'roc_auc' 作为评分函数。 它适用于 n 折交叉验证,但如果我使用 LeaveOneOut,它就不起作用并生成错误消息。

ValueError: Only one class present in Y. ROC AUC score is not defined in that case.

虽然用 AUC 绘制只有一个样本似乎很自然,但其他语言(例如 R)支持 LeaveOneOut 的 roc_auc。

如何使用 python 和 scikit-learn 进行计算?如果不可能,会不会像这样使用大折交叉验证结果?

【问题讨论】:

  • 您是否尝试为单类模型绘制多类 Roc 曲线?你读过this 吗?
  • 留一个交叉验证的问题是 GridSearchCV 计算每个折叠的分数,然后报告平均值。如果遗漏一个,就不可能为单个样本生成分数。
  • 感谢您的回答。所以 GridSearchCV() 不能用 LeaveOneOut 完成。那么,有没有其他方法可以计算所有样本改变参数而不是GridSearchCV的roc_auc分数?

标签: python scikit-learn


【解决方案1】:

正如David Maust 所指出的,留一交叉验证的问题是 GridSearchCV 计算每个折叠的分数,然后报告平均值。

为了使用 LeaveOneOut 获得有意义的 ROC AUC,您需要计算每个折叠的概率估计值(每个折叠只包含一个观察值),然后在所有这些概率估计值的集合上计算 ROC AUC。

这可以按如下方式完成:

def LeaveOneOut_predict_proba(clf, X, y, i):
    clf.fit(X.drop(i), y.drop(i))
    return clf.predict_proba(X.loc[[i]])[0, 1]

# set clf, param_grid, X, y

for params in ParameterGrid(param_grid):
    print(params)
    clf.set_params(**params)
    y_proba = [LeaveOneOut_predict_proba(clf, X, y, i) for i in X.index]
    print(roc_auc_score(y, y_proba))

样本输出:

{'n_neighbors': 5, 'p': 1, 'weights': 'uniform'}
0.6057986111111112
{'n_neighbors': 5, 'p': 1, 'weights': 'distance'}
0.620625
{'n_neighbors': 5, 'p': 2, 'weights': 'uniform'}
0.5862499999999999

由于这不使用 GridSearchCV 的基础架构,您需要自己实现选择最大分数和并行化(如果需要)。

【讨论】:

  • 对于带有LOO交叉验证的ROC曲线,this有意义吗?
猜你喜欢
  • 2017-09-02
  • 2018-12-30
  • 2018-05-14
  • 2017-03-27
  • 2013-11-24
  • 1970-01-01
  • 2017-10-21
  • 2019-04-20
相关资源
最近更新 更多