问题
我正在尝试使用 scikit-learn 的 LogisticRegressionCV
与 roc_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')
太棒了。很清楚发生了什么: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)
问题
关于如何使用 roc_auc_score
作为 LogisticRegressionCV
的评分指标,以便为评分函数指定参数,有什么想法吗?
我在 scikit-learn 的 GitHub 存储库中找不到关于这个问题的 SO 问题或对这个问题的讨论,但肯定有人以前遇到过这个问题?
最佳答案
你可以使用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_
关于python - 如何在 scikit-learn 的 LogisticRegressionCV 调用中将参数传递给评分函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39044686/