【问题标题】:Type Error with sklearn make_scorer Functionsklearn make_scorer 函数的类型错误
【发布时间】:2018-10-23 13:51:28
【问题描述】:

我正在尝试构建要在 GridSearhCV 对象中使用的自定义评分函数(使用 sklearn.metrics.make_scorer)。 make_scorer 的文档说:

score_func :可调用,评分函数(或损失函数) 签名score_func(y, y_pred, **kwargs)

这是我正在使用的代码:

    class model(object):
        def __init__(self):
            pass

        def fit(self, X, y):
            score_func = make_scorer(self.make_custom_score)

            clf = GradientBoostingClassifier()

            model = GridSearchCV(estimator=clf, 
                                 param_grid=grid, 
                                 scoring=score_func,
                                 cv=3)

            model.fit(X, y)               
            return self


        def make_custom_score(y_true, y_score):
            df_out = pd.DataFrame()

            df = pd.DataFrame({'true': y_true.tolist(), 'probability':
                                y_score.tolist()})

            for threshold in np.arange(0.01, 1.0, 0.01):   

                above_thresh = df[df['probability'] > threshold].groupby('true').count().reset_index()   


                tp = above_thresh.loc[[1.0]]['probability'].sum()


                df_threshold = pd.DataFrame({'threshold': [threshold], 'tp': tp})
                df_out = df_out.append(df_threshold)

            df_out = df_out.sort_values(by = ['threshold'], ascending = False)

            tp_score = tp[5]


            return tp_score

我得到的错误是:

TypeError: make_custom_score() 接受 2 个位置参数,但给出了 3 个。

我计划将来使用 **kwargs 为评分功能添加更多功能,因此如果可以的话,我想使用 make_scorer。

【问题讨论】:

标签: python scikit-learn


【解决方案1】:

我相信自从您在实例上调用该方法以来,正在传递 3 个位置参数。尝试将 self 作为该方法的第一个参数添加。

def make_custom_score(self, y_true, y_score):

【讨论】:

    猜你喜欢
    • 2020-06-17
    • 2020-07-26
    • 2016-12-18
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2017-12-16
    • 2015-10-26
    • 2015-10-15
    相关资源
    最近更新 更多