---
# System prepended metadata

title: '[Learn Note] [Machine Learning] Confusion Matrix'
tags: [Machine Learning, Learn Note]

---

# 二元分類（Binary Classification）
![image](https://hackmd.io/_uploads/BymiJdrDyx.png)
* True Positive(TP)
* False Positive(FP)
* True Negative(TN)
* False Negative(FN)

---

# 多元分類（Multi-Class Classification）
![image](https://hackmd.io/_uploads/ryexlurvJx.png)

---
## Setosa 類別
### TP (True Positive)
![image](https://hackmd.io/_uploads/HJin-_BPJg.png)

實際值與預測值皆為 Setosa。
值: Cell 1 = 16
### FN (False Negative)
![image](https://hackmd.io/_uploads/Sy9JMuHwyx.png)

該列中非 TP 的其他值總和。
計算: Cell 2 + Cell 3 = 0 + 0 = 0
### FP (False Positive)
![image](https://hackmd.io/_uploads/S1YBfuHvJg.png)

該欄中非 TP 的其他值總和。
計算: Cell 4 + Cell 7 = 0 + 0 = 0
### TN (True Negative)
![image](https://hackmd.io/_uploads/S1EKGurDJl.png)

除了該行與該列的其他值總和。
計算: Cell 5 + Cell 6 + Cell 8 + Cell 9 = 17 + 1 + 0 + 11 = 29


---
### Accuracy（準確率）
$$ Accuracy = \frac{TP + TN}{TP + TN + FP + FN} = \frac{16 + 29}{16 + 29 + 0 + 0} = 0 $$

## Versicolor 類別
### TP (True Positive)
![image](https://hackmd.io/_uploads/ryMzmuHwJe.png)

實際值與預測值皆為 Versicolor。
值: Cell 5 = 17
### FN (False Negative)
![image](https://hackmd.io/_uploads/SkTImdHDJx.png)

該列中非 TP 的其他值總和。
計算: Cell 4 + Cell 6 = 0 + 1 = 1
### FP (False Positive)
![image](https://hackmd.io/_uploads/BkS27OHPJe.png)

該欄中非 TP 的其他值總和。
計算: Cell 2 + Cell 8 = 0 + 0 = 0
### TN (True Negative)
![image](https://hackmd.io/_uploads/rkfm4_rD1g.png)

除了該行與該列的其他值總和。
計算: Cell 1 + Cell 3 + Cell 7 + Cell 9 = 16 + 0 + 0 + 11 = 27

---

## Virginica 類別
### TP (True Positive)
![image](https://hackmd.io/_uploads/r1laV_HP1l.png)

實際值與預測值皆為 Virginica。
值: Cell 9 = 11
### FN (False Negative)
![image](https://hackmd.io/_uploads/BJffSuHPyl.png)

該列中非 TP 的其他值總和。
計算: Cell 7 + Cell 8 = 0 + 0 = 0
### FP (False Positive)
![image](https://hackmd.io/_uploads/rJU4rOHv1x.png)

該欄中非 TP 的其他值總和。
計算: Cell 3 + Cell 6 = 0 + 1 = 1
### TN (True Negative)
![image](https://hackmd.io/_uploads/Hk7ISuHwkl.png)

除了該行與該列的其他值總和。
計算: Cell 1 + Cell 2 + Cell 4 + Cell 5 = 16 + 0 + 0 + 17 = 33


---

# 基於 Confusion Matrix 數據的指標
Accuracy（準確率）:
準確率衡量模型在所有類別中預測的整體正確性。
$$ Accuracy = \frac{TP + TN}{TP + TN + FP + FN} $$

Precision (精確度):
精確度聚焦於正類預測的質量，表示預測為正類中實際為正類的比例。
$$ Precision = \frac{TP}{TP + FP} $$

Recall (召回率，也稱為敏感度):
衡量模型識別所有實際正類實例的能力。
$$ Recall = \frac{TP}{TP + FN} $$

F1-score:
F1分數平衡了精確度（Precision）和召回率（Recall），提供了一個綜合的模型表現指標。
$$ F1-score = 2 \times \frac{Precision \times Recall}{Precision + Recall} $$

Specificity（特異度）:
特異度，也稱為真陰性率，衡量模型正確識別負類實例的能力。
$$ Specificity = \frac{TN}{TN + FP} $$

---

# TPR, FPR, TNR, FNR, AUC
## TPR (True Positive Rate)（Recall）
表示正類預測正確的比例
$$ TPR = \frac{TP}{TP + FN} $$

## FPR (False Positive Rate)
反類被錯誤預測為正類的比例
$$ FPR = \frac{FP}{FP + TN} $$

## TNR (True Negative Rate)
反類預測正確的比例
$$ TNR = \frac{TN}{TN + FP} $$

## FNR (False Negative Rate)
正類被錯誤預測為反類的比例
$$ FNR = \frac{FN}{TP + FN} $$

## ROC曲線:
以FPR為橫軸，TPR為縱軸繪製的曲線。曲線越接近左上角，模型效果越好。
![image](https://hackmd.io/_uploads/B1XoxKBPkg.png)

AUC (Area Under Curve):
ROC曲線下的面積，數值越大表示模型效果越好。

---
# 選擇適當的評估指標
評估指標的選擇取決於具體問題和不同類型錯誤的相對重要性：
- 對於不平衡數據集，精確度、召回率和F1分數通常比單純的準確率更具參考價值。
- 當假陽性代價高昂時，應著重關注精確度。
- 當假陰性後果嚴重時，應優先考慮召回率。
- 使用F1分數可以平衡地看待精確度和召回率。
- 當正確識別負類案例很重要時，應考慮特異度。

－--
# 參考
* [Analytics Vidhya - Latest Guide on Confusion Matrix for Multi-Class Classification](https://www.analyticsvidhya.com/blog/2021/06/confusion-matrix-for-multi-class-classification/)
* [shona cnblog - 針對二分類的結果，對模型進行評估，通常有以下幾種方法](https://www.cnblogs.com/shona/p/10764445.html)
* [GeeksforGeeks - Understanding the Confusion Matrix in Machine Learning](https://www.geeksforgeeks.org/confusion-matrix-machine-learning/)