sklearn,更改cross_val_score中的默认参数

时间:2017-04-20 10:08:48

标签: python function parameters scikit-learn evaluation

我正在使用 cross_val_score。评估文本分类预测 我需要使用 recall_score函数评估我的预测,但参数 average =' macro' 。 cross_val_score将其设置为默认参数 binary ,这对我的代码不起作用。有没有办法用不同的参数调用recall_score,或者将默认参数更改为宏。

results = model_selection.cross_val_score(estimator, X, Y, cv= kfold, scoring= 'recall')

1 个答案:

答案 0 :(得分:3)

你可以使用" recall_macro"就像这样:

results = model_selection.cross_val_score(estimator, X, Y, cv= kfold, scoring= 'recall_macro')

根据documentation of metrics

‘f1’                metrics.f1_score            for binary targets
‘f1_micro’          metrics.f1_score            micro-averaged
‘f1_macro’          metrics.f1_score            macro-averaged
‘f1_weighted’       metrics.f1_score            weighted average
‘f1_samples’        metrics.f1_score            by multilabel sample
‘neg_log_loss’      metrics.log_loss            requires predict_proba support
‘precision’ etc.    metrics.precision_score     suffixes apply as with ‘f1’
‘recall’ etc.       metrics.recall_score        suffixes apply as with ‘f1’

如您所见,它指定所有后缀适用于"召回"。

或者,您也可以像这样使用make_scorer

# average can take values from 'macro', 'micro', 'weighted' etc as specified above
scorer = make_scorer(recall_score, pos_label=None, average='macro')
results = model_selection.cross_val_score(estimator, X, Y, cv= kfold,
                                          scoring= scorer)