sklearn(七)計算多分類任務中每個類別precision、recall、f1的整合函式precision_recall_fscore_support()

凝眸伏筆發表於2020-12-01

1.precision_recall_fscore_support()使用介紹:

sklearn.metrics.precision_recall_fscore_support(y_true, y_pred, *, beta=1.0, labels=None, pos_label=1, average=None, 
warn_for=('precision', 'recall', 'f-score'), sample_weight=None, zero_division='warn')

輸入引數:

引數同recall()函式用法

輸出:

precision:average為none時,返回一個array, shape = [n_unique_labels],如果average不為none時,返回一個float。

recall:同上

fbeta_score:同上

support:同上

The number of occurrences of each label in y_true.

2.例子:

#average=None,取出每一類的P,R,F1值
p_class, r_class, f_class, support_micro=precision_recall_fscore_support(
                y_true=y_true, y_pred=y_pred, labels=[1, 2, 3, 4], average=None)
print('各類單獨F1:',f_class)
print('各類F1取平均:',f_class.mean())
#>>>各類單獨F1: [ 0.75        0.66666667  0.5         0.5       ]
#>>>各類F1取平均: 0.604166666667
 
#注意這裡,輸出《巨集F》
print(f1_score(y_true,y_pred,labels=[1,2,3,4],average='macro'))
#>>>0.604166666667

 

參考:

1.官方文件:https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_fscore_support.html?highlight=precision_recall_fscore_support

2.例子:https://blog.csdn.net/lyb3b3b/article/details/84819931

 

相關文章