1. # -*- coding:utf-8 -*-
    2. # @auth ivan
    3. # @time 20190131
    4. # @goal Test_F1-micro&F1-macro
    5. """
    6. https://www.cnblogs.com/techengin/p/8962024.html
    7. F1-score = 2*(P*R)/(P+R)
    8. 准确率(P) = TP/(TP+FP)
    9. 召回率(R) = TP/(TP+FN)
    10. 对于数据测试结果有下面4种情况:
    11. 真阳性(TP): 预测为正, 实际为正;
    12. 假阳性(FP): 预测为正, 实际为负;
    13. 假阴性(FN): 预测为负, 实际为正;
    14. 真阴性(TN): 预测为负, 实际为负;
    15. """
    16. """
    17. from sklearn.metrics import f1_score
    18. average : string, [None, 'binary' (default), 'micro', 'macro', 'samples', \
    19. 'weighted']
    20. This parameter is required for multiclass/multilabel targets.
    21. If ``None``, the scores for each class are returned. Otherwise, this
    22. determines the type of averaging performed on the data:
    23. ``'binary'``:
    24. Only report results for the class specified by ``pos_label``.
    25. This is applicable only if targets (``y_{true,pred}``) are binary.
    26. ``'micro'``:
    27. Calculate metrics globally by counting the total true positives,
    28. false negatives and false positives.
    29. ``'macro'``:
    30. Calculate metrics for each label, and find their unweighted
    31. mean. This does not take label imbalance into account.
    32. ``'weighted'``:
    33. Calculate metrics for each label, and find their average, weighted
    34. by support (the number of true instances for each label). This
    35. alters 'macro' to account for label imbalance; it can result in an
    36. F-score that is not between precision and recall.
    37. ``'samples'``:
    38. Calculate metrics for each instance, and find their average (only
    39. meaningful for multilabel classification where this differs from
    40. :func:`accuracy_score`).
    41. """
    42. from sklearn.metrics import f1_score, precision_recall_fscore_support
    43. y_true = [1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4]
    44. y_pred = [1, 1, 1, 0, 0, 2, 2, 3, 3, 3, 4, 3, 4, 3]
    45. print(f1_score(y_true, y_pred, labels=[1, 2, 3, 4], average='micro'))
    46. # 0.6153846153846153
    47. p_class, r_class, f_class, support_micro = precision_recall_fscore_support(
    48. y_true=y_true, y_pred=y_pred, labels=[1, 2, 3, 4], average=None)
    49. print(f_class.mean(), f_class)
    50. # 0.6041666666666666 [0.75 0.66666667 0.5 0.5 ]
    51. print(f1_score(y_true, y_pred, labels=[1, 2, 3, 4], average='macro'))
    52. # 0.6041666666666666