本文介绍了在 GridSearchCV 中使用精度作为评分时如何指定正标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

model = sklearn.model_selection.GridSearchCV(
        estimator = est,
        param_grid = param_grid,
        scoring = 'precision',
        verbose = 1,
        n_jobs = 1,
        iid = True,
        cv = 3)

sklearn.metrics.precision_score(y, y_pred,pos_label=[0])中,我可以指定正标签,如何在GridSearchCV中也指定?

In sklearn.metrics.precision_score(y, y_pred,pos_label=[0]), I can specify the positive label, how can I specify this in GridSearchCV too?

如果无法指定,使用自定义评分时,如何定义?

If there is no way to specify, when using custom scoring, how can I define?

我已经试过了:

custom_score = make_scorer(precision_score(y, y_pred,pos_label=[0]),
                          greater_is_better=True)

但我有错误:

NameError: name 'y_pred' is not defined

推荐答案

阅读docs,您可以将任何 kwargs 传递到 make_scorer 中,它们将自动传递到 score_func 可调用对象中.

Reading the docs, you can pass any kwargs into make_scorer and they will be automatically passed into the score_func callable.

from sklearn.metrics import precision_score, make_scorer
custom_scorer = make_scorer(precision_score, greater_is_better=True,  pos_label=0)

然后你把这个 custom_scorer 传递给 GridSearchCV:

Then you pass this custom_scorer to GridSearchCV:

gs = GridSearchCV(est, ..., scoring=custom_scorer)

这篇关于在 GridSearchCV 中使用精度作为评分时如何指定正标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 03:11