python – 在Scikit-learn中小于精度和召回的F1
作者:互联网
我正在进行多级分类,具有不平衡的类别.
我注意到f1总是小于精度和召回的直接调和平均值,在某些情况下,f1甚至小于精度和召回率.
仅供参考,我将metrics.precision_score(y,pred)称为精度等等.
我知道微观/宏观平均值的差异,并通过使用precision_recall_fscore_support()的类别结果测试它们不是微观的.
不确定这是由于使用了宏观平均值还是其他一些原因?
更新了详细结果如下:
n_samples:75,n_features:250
MultinomialNB(alpha = 0.01,fit_prior = True)
2倍CV:
第一轮:
F1: 0.706029106029
Precision: 0.731531531532
Recall: 0.702702702703
precision recall f1-score support
0 0.44 0.67 0.53 6
1 0.80 0.50 0.62 8
2 0.78 0.78 0.78 23
avg / total 0.73 0.70 0.71 37
第二轮:
F1: 0.787944219523
Precision: 0.841165413534
Recall: 0.815789473684
precision recall f1-score support
0 1.00 0.29 0.44 7
1 0.75 0.86 0.80 7
2 0.82 0.96 0.88 24
avg / total 0.84 0.82 0.79 38
总体:
Overall f1-score: 0.74699 (+/- 0.02)
Overall precision: 0.78635 (+/- 0.03)
Overall recall: 0.75925 (+/- 0.03)
从Scholarpedia开始的微/平均值定义:
In multi-label classification, the simplest method for computing an
aggregate score across categories is to average the scores of all
binary task. The resulted scores are called macro-averaged recall,
precision, F1, etc. Another way of averaging is to sum over TP, FP,
TN, FN and N over all the categories first, and then compute each of
the above metrics. The resulted scores are called micro-averaged.
Macro-averaging gives an equal weight to each category, and is often
dominated by the system’s performance on rare categories (the
majority) in a power-law like distribution. Micro-averaging gives an
equal weight to each document, and is often dominated by the system’s
performance on most common categories.
目前在Github的#open issue,#83.
以下示例演示了Micro,Macro和加权(Scikit-learn中的当前)平均值可能有何不同:
y = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2]
pred = [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 2, 0, 1, 2, 2, 2, 2]
混淆矩阵:
[[9 3 0]
[3 5 1]
[1 1 4]]
Wei Pre: 0.670655270655
Wei Rec: 0.666666666667
Wei F1 : 0.666801346801
Wei F5 : 0.668625356125
Mic Pre: 0.666666666667
Mic Rec: 0.666666666667
Mic F1 : 0.666666666667
Mic F5 : 0.666666666667
Mac Pre: 0.682621082621
Mac Rec: 0.657407407407
Mac F1 : 0.669777037588
Mac F5 : 0.677424801371
上面的F5是F0.5的简写…
解决方法:
你可以用以下输出更新你的问题:
>>> from sklearn.metrics import classification_report
>>> print classification_report(y_true, y_predicted)
这将显示每个类别的精确度和召回以及支持,从而帮助我们理解平均值如何工作并决定这是否是适当的行为.
标签:python,machine-learning,scikit-learn,scikits 来源: https://codeday.me/bug/20190704/1378466.html