在 GridSearchCV 中使用精度作为评分时如何指定正标签

How to specify positive label when use precision as scoring in 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中也指定这个?

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

我试过这个:

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 可调用对象。

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

然后你把这个custom_scorer传给GridSearchCV:

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