如何使用交叉验证在多 class 数据集中对精度、召回率和 f1 分数进行评分?

how to score precision, recall and f1-score in a multi-class dataset using cross-validate?

此代码适用于具有 2 个 class 的数据集,但不适用于 multi-class

scoring = {'accuracy' : make_scorer(accuracy_score), 
       'precision' : make_scorer(precision_score),
       'recall' : make_scorer(recall_score), 
       'f1_score' : make_scorer(f1_score)}
scores = cross_val_score(gnb,x,y, cv=5, scoring=scoring)
print(scores)

错误显示

ValueError: For evaluating multiple scores, use sklearn.model_selection.cross_validate instead. {'accuracy': make_scorer(accuracy_score), 'precision': make_scorer(precision_score, average=None), 'recall': make_scorer(recall_score), 'f1_score': make_scorer(f1_score)} was passed

当我检查代码时将它更改为这样

scores = cross_val_score(gnb,x,y, cv=5, scoring='precision')

错误显示

ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].

当我在 make_scorer

中设置 average 时它不起作用

对 f1 使用评分函数“f1_macro”或“f1_micro”。
同样,'recall_macro' 或 'recall_micro' 用于召回。

计算准确率或召回率时,定义正数class很重要,但在多class数据集中,很难定义。

因此,应计算每个 class 值(即迭代每个值并将其视为正数 class)的平均精度(召回率)。

编辑。

尝试使用以下代码(微平均精度、召回率、f1)。

scoring = {'accuracy' : make_scorer(accuracy_score), 
       'precision' : make_scorer(precision_score, average = 'micro'),
       'recall' : make_scorer(recall_score, average = 'micro'), 
       'f1_score' : make_scorer(f1_score, average = 'micro')}