python - F1 小于 Scikit-learn 中的精度和召回率

标签 python machine-learning scikits scikit-learn

我正在进行多类分类,类别不平衡。

我注意到 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 倍简历:

第一次运行:

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.


这是电流 open issue在 Github 上,#83。


以下示例演示了微观、宏观和加权(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 - F1 小于 Scikit-learn 中的精度和召回率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8284456/

相关文章:

python - 基于一个引用数组向数组添加和删除元素

python - 注释是否在运行时在 python 中解析?允许不定义吗?

python - 使用 python 的朴素贝叶斯分类器

python ,scikits-学习 : which learning methods support sparse feature vectors?

python - Sklearn预测函数

python - 两个 DataFrame 的复杂合并

python - 具有 One Hot Encoded Features 的 Auto-Sklearn 中的特征和特征重要性

python - 使用高斯过程回归 (GPR) 预测 radio 信号强度 (RSS)

python - 在 python 中绘制 sklearn 集群

c++ - 如何使用朴素贝叶斯算法对未知记录进行分类