【问题标题】:Scikit-learn: which values are provided to a scorer by default when needs_threshold=True?Scikit-learn:当 needs_threshold=True 时,哪些值默认提供给记分员?
【发布时间】:2018-08-18 12:15:54
【问题描述】:

在二元分类设置中,根据 ROC 下的面积调整模型需要可以阈值化的模型输出。

但是,在 scikit-learn 中,支持向量分类器默认不会生成类别概率。

因此,例如,使用 GridSearchCV 和 scoring=make_scorer(roc_auc_score, needs_threshold=False) 来调整 SVC 模型是不正确的,因为 AUC 分数将根据每个 CV 折叠中的预测类来计算。无论我们使用SVC(probability=True) 还是SVC(probability=False),都会发生这种情况。另一方面,scoring=make_scorer(roc_auc_score, needs_threshold=True) 会正确调整。

因此SVC 必须将一些“阈值”输出传递给GridSearchCV 中的评分函数。 我们如何知道给定模型的这个阈值输出是什么?

对于SVC,我假设调用了decision_function() 方法。 (我假设它没有计算类概率,因为在使用 SVC(probability=False) 时,您无法在拟合的 GridSearchCV 对象上运行 predict_proba())。但从文档中并不清楚(至少对我而言)这绝对是正在发生的事情。

【问题讨论】:

    标签: scikit-learn


    【解决方案1】:

    是的,你是对的。

    来自source-code of make_scorer:

    ....
    elif needs_threshold:
        cls = _ThresholdScorer
    ....
    

    所以当needs_threshold = True 时,使用_ThresholdScorer 记分员。现在查看source code of _ThresholdScorer,我们看到:

    ....
    ....
            try:
                y_pred = clf.decision_function(X)
    
                # For multi-output multi-class estimator
                if isinstance(y_pred, list):
                    y_pred = np.vstack(p for p in y_pred).T
    
            except (NotImplementedError, AttributeError):
                y_pred = clf.predict_proba(X)
    

    因此,这将首先调用估计器的decision_function() 来查找阈值。

    【讨论】:

    • 完美!谢谢
    猜你喜欢
    • 2013-11-27
    • 2017-02-27
    • 1970-01-01
    • 2018-02-03
    • 2019-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多