首页 文章

如何在scikit-learn的LogisticRegressionCV调用中将参数传递给评分函数

提问于
浏览
7

Problem

我正在尝试使用scikit-learn的LogisticRegressionCVroc_auc_score作为评分指标 .

from sklearn.linear_model import LogisticRegression
from sklearn.metrics import roc_auc_score

clf = LogisticRegressionCV(scoring=roc_auc_score)

但是当我尝试拟合模型( clf.fit(X, y) )时,它会抛出错误 .

ValueError: average has to be one of (None, 'micro', 'macro', 'weighted', 'samples')

那个's cool. It'清楚发生了什么: roc_auc_score 需要使用指定的 average 参数调用,每个its documentation和上面的错误 . 所以我试过了 .

clf = LogisticRegressionCV(scoring=roc_auc_score(average='weighted'))

但事实证明,单独使用可选参数不能调用 roc_auc_score ,因为这会引发另一个错误 .

TypeError: roc_auc_score() takes at least 2 arguments (1 given)

Question

关于我如何使用 roc_auc_score 作为 LogisticRegressionCV 的评分指标的任何想法,我可以指定评分函数的参数?

我在scikit-learn的GitHub回购中找不到关于这个问题的SO问题或者对这个问题的讨论,但是肯定有人之前遇到过这个问题吗?

2 回答

  • 5

    我找到了解决这个问题的方法!

    scikit-learn在其 metrics 模块中提供 make_scorer 函数,允许用户从其本机评分函数 with arguments specified to non-default values 创建评分对象(有关此函数的更多信息,请参阅here来自scikit-learn文档) .

    所以,我创建了一个评分对象,指定了 average 参数 .

    roc_auc_weighted = sk.metrics.make_scorer(sk.metrics.roc_auc_score, average='weighted')
    

    然后,我在调用 LogisticRegressionCV 时传递了该对象,它没有任何问题!

    clf = LogisticRegressionCV(scoring=roc_auc_weighted)
    
  • 5

    您可以使用 make_scorer ,例如

    from sklearn.linear_model import LogisticRegressionCV
    from sklearn.metrics import roc_auc_score, make_scorer
    from sklearn.datasets import make_classification
    
    # some example data
    X, y = make_classification()
    
    # little hack to filter out Proba(y==1)
    def roc_auc_score_proba(y_true, proba):
        return roc_auc_score(y_true, proba[:, 1])
    
    # define your scorer
    auc = make_scorer(roc_auc_score_proba, needs_proba=True)
    
    # define your classifier
    clf = LogisticRegressionCV(scoring=auc)
    
    # train
    clf.fit(X, y)
    
    # have look at the scores
    print clf.scores_
    

相关问题