【问题标题】:how to score precision, recall and f1-score in a multi-class dataset using cross-validate?如何使用交叉验证在多类数据集中对精度、召回率和 f1-score 进行评分?
【发布时间】:2020-05-19 06:54:46
【问题描述】:

此代码适用于具有 2 个类但不适用于多类的数据集

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 时它不起作用

【问题讨论】:

    标签: python scikit-learn cross-validation


    【解决方案1】:

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

    在计算精度或召回率时,定义正类很重要,但在多类数据集中,很难定义。

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

    编辑。

    尝试使用以下代码(微平均精度、召回率、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')}
    

    【讨论】:

    • 谢谢,但是名称'f1_macro'没有定义,等等。你能告诉我如何定义评分函数吗?
    • 我添加了一个代码,并参考它。参考scikit-learn.org/stable/modules/generated/…的sklearn.metrics.make_score。
    • 我已经尝试过这种方式......仍然无法正常工作错误仍然显示目标是多类但平均='二进制'。请选择另一个平均设置,[None, 'micro', 'macro', 'weighted'] 之一。
    • accuracy = cross_val_score(gnb,x,y, cv=5, score='accuracy') precision = cross_val_score(gnb,x,y, cv=5, score='precision_macro') 召回率 = cross_val_score(gnb,x,y, cv=5, score='recall_macro') f1 = cross_val_score(gnb,x,y, cv=5, score='f1_macro') print(accuracy) print(precision) print(recall)打印(f1)此代码正在运行..:D
    猜你喜欢
    • 2017-11-18
    • 1970-01-01
    • 2018-03-17
    • 2019-11-14
    • 2021-11-02
    • 2017-03-14
    • 2017-06-21
    • 2021-09-01
    • 2017-05-28
    相关资源
    最近更新 更多