自定义评分指标sklearn逻辑回归

时间:2019-05-16 17:21:58

标签: python scikit-learn

假设我在sci-kit学习中使用了以下自定义损失函数。在这种情况下,我仅对模型得分高于0.8的观察结果评分。

def customLoss(y_true, y_pred):
    a = pd.DataFrame({'Actuals':y_true, 'Preds': y_pred})
    a = a.query('Preds > 0.8')
    return(precision_score(a['Actuals'], a['Preds']))

param_grid = {'C': [0.001, 0.01, 0.1, 1, 10]}
scorer = make_scorer(mf.customLoss ,greater_is_better = True)
grid = GridSearchCV(LogisticRegression(class_weight = 'balanced'), param_grid = param_grid, scoring = scorer, cv = 5)

但是,假设我要使阈值(0.8)可配置。显然,我需要向我的损失函数添加第三个参数,如下所示:

def customLoss(y_true, y_pred, threshold):
        a = pd.DataFrame({'Actuals':y_true, 'Preds': y_pred})
        a = a.query('Preds > @threshold')
        return(precision_score(a['Actuals'], a['Preds']))

但是,我对将第三个参数放在make_scorer函数中的位置有些困惑?

1 个答案:

答案 0 :(得分:0)

尝试

grid = GridSearchCV(LogisticRegression(class_weight = 'balanced'), param_grid = param_grid, scoring = 'metric_you_want', cv = 5)