通过 sklearn.metrics.make_scorer 将估算器传递给自定义评分函数 [英] Pass estimator to custom score function via sklearn.metrics.make_scorer

查看:140
本文介绍了通过 sklearn.metrics.make_scorer 将估算器传递给自定义评分函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个涉及分类概率的自定义评分函数,如下所示:

I'd like to make a custom scoring function involving classification probabilities as follows:

def custom_score(y_true, y_pred_proba):
    error = ...
    return error

my_scorer = make_scorer(custom_score, needs_proba=True)

gs = GridSearchCV(estimator=KNeighborsClassifier(),
                  param_grid=[{'n_neighbors': [6]}],
                  cv=5,
                  scoring=my_scorer)

有什么方法可以将 GridSearch 与给定数据和参数相匹配的估算器传递给我的自定义评分函数?然后我可以使用 estimator.classes_

Is there any way to pass the estimator, as fit by GridSearch with the given data and parameters, to my custom scoring function? Then I could interpret the probabilities using estimator.classes_

例如:

def custom_score(y_true, y_pred_proba, clf):
    class_labels = clf.classes_
    error = ...
    return error

推荐答案

有一种替代方法可以制作得分手 在文档中提到.使用此方法,我可以执行以下操作:

There is an alternative way to make a scorer mentioned in the documentation. Using this method I can do the following:

def my_scorer(clf, X, y_true):
    class_labels = clf.classes_
    y_pred_proba = clf.predict_proba(X)
    error = ...
    return error

gs = GridSearchCV(estimator=KNeighborsClassifier(),
                  param_grid=[{'n_neighbors': [6]}],
                  cv=5,
                  scoring=my_scorer)

这避免了使用 sklearn.metrics.make_scorer.

这篇关于通过 sklearn.metrics.make_scorer 将估算器传递给自定义评分函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆