sklearn make_scorer函数的类型错误

时间:2018-10-23 03:24:34

标签: python scikit-learn

我试图构建一个自定义评分功能(使用sklearn.metrics.make_scorer)以用于GridSearhCV对象。 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。

1 个答案:

答案 0 :(得分:1)

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

def make_custom_score(self, y_true, y_score):
相关问题