RyanL
RyanL

Reputation: 156

Type Error with sklearn make_scorer Function

I am trying to build a custom scoring function (using sklearn.metrics.make_scorer) to be used in a GridSearhCV object. The documentation for make_scorer says:

score_func : callable, Score function (or loss function) with signature score_func(y, y_pred, **kwargs).

Here is the code I am using:

    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

The error I get is:

TypeError: make_custom_score() takes 2 positional arguments but 3 were given.

I am planning on adding more to the scoring function using the **kwargs in the future so I would like to use make_scorer if I can.

Upvotes: 1

Views: 721

Answers (1)

LeKhan9
LeKhan9

Reputation: 1350

I believe 3 positional arguments are being passed since you called the method on an instance. Try adding self as the first param to that method.

def make_custom_score(self, y_true, y_score):

Upvotes: 1

Related Questions