# [筆記] macro, micro 與 binary 不同版本的 acc., precision, recall, fscore 計算方法 ###### tags: `AI/ML` 使用套件: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.precision_recall_fscore_support.html 這篇舉例說明頗清楚: https://blog.csdn.net/jmh1996/article/details/114935105 我也來舉個例子: ![](https://i.imgur.com/PhurzsV.png) **Notations** - gt: ground truth - pred: predict result - 檢測陽性,正樣本 + : label 為 1 - 檢測陰性,負樣本 - : label 為 0 看表格解釋 | | d1 | d2 | d3 | d4 | d5 | d6 | d7 | d8 | d9 | d10| | - | - | - | - | - | - | - | - | - | - | - | | gt | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 | 1 | | pred | 0 | 1 | 0 | 1 | 0 | 0 | 0 | 1 | 0 | 1 | | 對於+ | TN | FP | TN | TP | FN | TN | TN | FP | TN | TP | | 對於- | TP | FN | TP | TN | FP | TP | TP | FN | TP | TN | 統計一下 對於+ 當中 $TP: 2, FP: 2, TN: 5, FN: 1$ 對於- 當中 $TP:5, FP:1, TN:2, FN:2$ 根據定義 $$precision=\frac{TP}{TP+FP} \hspace{0.5cm} recall=\frac{TP}{TP+TN}$$ $$F1\_score=2\frac{precision\times recall}{precision+recall}$$ ## binary avg. of precision, recall 在 sklearn.metrics 的 precision_recall_fscore_support 裡所謂的 binary average 模式 就是只看**對於+(陽性樣本)的** precision, recall, fscore 值 計算出來得到 $$bin\_precision(+)=\frac{2}{2+2}=0.5 \hspace{0.5cm} bin\_recall(+)=\frac{2}{2+1}=0.6666$$ F1 值為這兩個的平均,在此就不贅述 ## macro avg. of precision, recall macro 就是把對於正樣本(+) 的結果與對於負樣本(-) 的結果分別算出來後,做平均 正樣本的結果已經在上一段落算完,因此我們還需要計算 **對於-(陰性樣本)的**precision, recall, fscore 值計算如下 $$bin\_precision(-)=\frac{5}{5+1}=0.8333 \hspace{0.5cm} bin\_recall(-)=\frac{5}{5+2}=0.7142$$ 接著就可以計算 macro average 版本的 precision, recall, fscroe 值計算如下 $$macro\_precision=\frac{0.5+0.8333}{2}=0.6666 \hspace{0.5cm} macro\_recall=\frac{0.6666+0.7142}{2}=0.6904$$ ## micro avg. of precision, recall micro avg 是把上述的 - 對於+ 當中 $TP: 2, FP: 2, TN: 5, FN: 1$ - 對於- 當中 $TP:5, FP:1, TN:2, FN:2$ 把以上兩個分類觀點觀察到的 TP, FP, TN, FN 都做加總之後再做計算: $sum\_TP:2+5=7$ , $sum\_FP:2+1=3$ , $sum\_TN:5+2=7$ , $sum\_FN:2+1=3$ 再根據定義計算出 micro average 版本的 precision, recall, fscore 值計算如下 $$micro\_precision=\frac{7}{7+3}=0.7 \hspace{0.5cm} micro\_recall=\frac{7}{7+3}=0.7$$ 報告完畢