Understanding confusion matrix & Basic metrics for ML

YOKK m.
4 min readJan 29, 2020

From previous blog about What is confusion matrix, this will explain the math behind it and how to get Accuracy, precision and recall rate is the easiest way

Predicted value as Positive-Negative. Actual Value denoted as True -False

Classification Model

Recall, Precision, Accuracy, Error Rate, F1 score

Example

Recall

Recall : ทำนายถูก/ทำนายทั้งหมด

คือการแสดงผลว่า ณ class หนึ่งๆ การทำนายนั้นถูกมากน้อยขนาดไหน — ในที่นี้ classes : อากาศเป็นพิษ อากาศไม่เป็นพิษ, ยิ่ง recall สูง(ใกล้ 1มากเท่าไหร่ — ยิ่งดี)

High Recall indicates the class is correctly recognised (small number of FN)

  1. อากาศเป็นพิษ
Recall for อากาศเป็นพิษ
  • ทายถูก ว่าอากาศเป็นพิษและอากาศเป็นพิษจริงๆ (TP) — 42
  • ทายผิด ว่าอากาศไม่เป็นพิษ แต่อากาศจริงๆเป็นพิษ (FN) — 8

→เพราะฉะนั้น ข้อมูลที่อากาศเป็นพิษจริงๆคือ 42 + 8 =50
Recall toxicity = 0.84

2. อากาศไม่เป็นพิษ

Recall for อากาศไม่เป็นพิษ
  • ทายถูก ว่าอากาศ ไม่เป็นพิษและอากาศไม่เป็นพิษจริงๆ (TN) — 48
  • ทายผิด ว่าอากาศเป็นพิษ แต่อากาศจริงๆไม่เป็นพิษ (FN) — 2

→เพราะฉะนั้น ข้อมูลที่อากาศเป็นพิษจริงๆคือ 48+ 2=50
Recall Not toxic = 0.96

Precision

precision: ทายถูกและเกิดขึ้นจริง / จำนวนที่เราทายว่ามันจะเกิดขึ้นทั้งหมด

คือการแสดงผลว่า การทายของเรา ถูกมากน้อยขนากไหน — ในที่นี้ classes : อากาศเป็นพิษ อากาศไม่เป็นพิษ

  1. ทายว่าอากาศเป็นพิษ
Precision ของทายว่าอากาศเป็นพิษ
  • ทายถูก ว่าอากาศ เป็นพิษและอากาศเป็นพิษจริงๆ (TP) — 42
  • ทายผิด ว่าอากาศเป็นพิษ แต่อากาศจริงๆเป็นไม่พิษ (FP) — 2

→เพราะฉะนั้นจำนวนที่ทายอากาศเป็นพิษคือ 42+2 =44
ที่ทายถูก จริงๆ 42 ทายผิด 2
Precision = 0.95

2. ทายว่าอากาศไม่เป็นพิษ

Precision ของทายว่าอากาศไม่เป็นพิษ
  • ทายถูก ว่าอากาศ เป็นไม่พิษและอากาศเป็นไม่พิษจริงๆ (TP) — 48
  • ทายผิด ว่าอากาศไม่เป็นพิษ แต่อากาศจริงๆเป็นพิษ (FP) — 8

→เพราะฉะนั้นจำนวนที่ทายอากาศเป็นพิษคือ 48+8 =56
ที่ทายถูก จริงๆ 48 ทายผิด 8
Precision = 0.85

Relationship between Precision- Recall

High Recall, High Precision

High recall relates to a low false negative rate — การที่ค่า recall สูง, หมายความว่า เราทายผิดน้อย ในเเง่ของ การที่เราทำนาย(ผิด)ว่า เราpredictedว่ามันจะไม่เกิด แต่จริงๆมันเกิด(ในที่นี้ เราpredicted ว่าอากาศจะไม่เป้นพิษ แต่จริงๆอากาศจริงๆเป็นพิษ)
High Precision relates to a low false positive rate — การที่ค่า precision สูง, หมายความว่า เราทายผิดน้อย ในเเง่ของ การที่เราpredicted(ผิด)ว่า มันจะเกิด แต่จริงๆมันไม่เกิด(ในที่นี้ เราpredicted ว่าอากาศจะเป็นพิษ แต่จริงๆอากาศจริงๆไม่เป็นพิษ)

High Recall, High Precision: shows that classifier is returning accurate results and returning a majority of all positive results

High Recall, Low Precision

(REPEAT ABOVE)High recall relates to a low false negative rate — การที่ค่า recall สูง, หมายความว่า เราทายผิดน้อย ในเเง่ของ การที่เราทำนาย(ผิด)ว่า เราpredictedว่ามันจะไม่เกิด แต่จริงๆมันเกิด(ในที่นี้ เราpredicted ว่าอากาศจะไม่เป้นพิษ แต่จริงๆอากาศจริงๆเป็นพิษ)
Low Precision relates to a high false positive rate — การที่ค่า precision ต่ำมากๆ, หมายความว่า ข้อมูลที่เราpredictedมานั้นแทบจะไม่ตรงกับข้อมูลจริงๆเลย

High recall, low precision: shows that predicted labels are incorrect when compared to the training labels.

F1 score

the weighted average of Precision and Recall , where an F1 score reaches its best value at 1 (perfect precision and recall) and worst at 0.

F1 is usually more useful than accuracy, especially if you have an uneven class distribution. Accuracy works best if false positives and false negatives have similar cost. If the cost of false positives and false negatives are very different, it’s better to look at both Precision and Recall.

Accuracy and Error Rate

Accuracy หมายความว่า ความแม่นยำของการทำนาย Model หนึ่งๆ มีความแม่นยำมากแค่ไหน โดยคำนวนว่าโมเดลเราทำนายถูกทั้งหมดเท่าไหร่

→Accuracy = 0.9

Accuracy and F1-Score

  • Accuracy ส่วนมากใช้ในกรณี ที่ให้ความสำคัญกับ TP&TN (ผลที่เราทายถูก — ทายว่า เกิดขึ้นจริง,และactualจริง-ทายว่าไม่จริงและactualคือไม่จริง)
  • F1-score ส่วนมากใช้ในกรณี ที่ให้ความสำคัญกับ FP&FN (ผลที่เราทายผิด — ทายว่า เกิดขึ้นจริง,และactualไม่จริง-ทายว่าไม่จริงและactualคือจริง)
  • F1-score is a better metric (more flexible) when there are imbalanced classification เช่น y {อากาศเป็นพิษ,อากาศไม่เป็นพิษ} มีสัดส่วนไม่เท่ากับ 50:50

AUC (Area Under ROC Curve)

a graph showing the performance of a classification model at all classification thresholds. This curve plots two parameters:
- True Positive Rate = TP/TP+FN
- False Positive Rate=FP/FP+TN

AUCมีค่าระหว่าง 0–1
- AUC ≤ 0.5 — model predictedได้แย่มากๆ
- AUC ≥ 0.7— average performance
- AUC ≥ 0.8 — good performance
- AUC ≥ 0.9— Excellent performance

Regression Model

R2 (R-squared/ Explained Variance)

will give some information about the goodness of fit of a model. An R2 of 1 indicates that the regression predictions perfectly fit the data

R-squared

R2 มีค่าระหว่าง0–1, โดยที่ y = target value, y_hat = y_pred/ predicted value, y_bar = average of y

--

--

YOKK m.

a data scientist — Master of Data science is the degree, master of none is also me