Lecture5b - Understanding Confusion Matrix (Multi-Class Classification)
Lecture5b - Understanding Confusion Matrix (Multi-Class Classification)
Intelligence is the capacity for logic, understanding, self-awareness, learning, emotional knowledge, reasoning,
planning, creativity, critical thinking, and problem-solving. More generally, it can be described as the ability to
perceive or infer information, and to retain it as knowledge to be applied towards adaptive behaviors within an
environment or context.
Binary Classification
BINARY CLASSIFICATION
A Confusion matrix is an N × N matrix used for evaluating the performance of a classification model, where N is the number
of target classes. The matrix compares the actual target values with those predicted by the machine learning model. This
gives the holistic view of how well the classification model is performing and what kinds of errors it is making.
BINARY CLASSIFICATION
A binary classification
problem has only two
classes to classify,
preferably a positive
and a negative class.
BINARY CLASSIFICATION
[1] Prediksi Pasien Menderita Cancer atau Tidak
Diketahui:
Total 20 pasien dengan 9 pasien positif cancer (label 1) dan 11 pasien negatif cancer (label 0).
Penyelesaian:
6 6
TP = 6 𝑺𝒆𝒏𝒔𝒊𝒕𝒊𝒇𝒊𝒕𝒚 = = = 0.66667 ∗ 100% = 66.67%
6+3 9
TN = 9 9 9
𝑺𝒑𝒆𝒄𝒊𝒇𝒊𝒄𝒊𝒕𝒚 = = = 0.8182 ∗ 100% = 81.82%
FN = 3 9 + 2 11
FP = 2 6+9 15
𝑶𝒗𝒆𝒓𝒂𝒍𝒍 𝑨𝒄𝒄𝒖𝒓𝒂𝒄𝒚 = = = 0.75 ∗ 100% = 75.00%
6 + 2 + 9 + 3 20
6 × 9 − (2 × 3) 54 − (6) 48 48
𝑴𝑪𝑪 = = = = = 0.49
6 + 2 ∗ 6 + 3 ∗ 9 + 2 ∗ (9 + 3) 8 ∗ 9 ∗ 11 ∗ (12) 9504 97.49
Multi-Class
Classification Problem
MULTI-CLASS CLASSIFICATION
1) Unlike binary classification, there are
no positive or negative classes in
multi-class classification.
1) Typical metrics used in multiclass are the same as the metrics used in the binary classification case.
2) The metric is calculated for each class by treating it as a binary classification problem after grouping all the
other classes as belonging to the second class.
3) The binary metric is averaged over all the classes to get either a macro average (treat each class equally)
or weighted average (weighted by class frequency) metric.
MULTI-CLASS CLASSIFICATION
Example: a confusion matrix for a multiclass problem where we have
to predict whether a person loves Facebook, Instagram or Snapchat.
The confusion matrix is shown in a 3 x 3 matrix:
Diketahui:
Total 50 pengguna social media dengan 15 users Facebook (label 0), 15 users Instagram (label 1), dan 20 users Snapchat (label 2).
Penyelesaian:
Actual Class
9 2 1
Predicted Class
4 10 5
2 3 14
MULTI-CLASS CLASSIFICATION
[2] Sweet Fruit (Apple, Orange and Mango) Classification
Diketahui:
Total 36 buah yang akan di klasifikasikan dengan 11 buah apple (label 0), 12 buah orange (label 1), dan 13 buah mango (label 2).
? ? ?
Predicted Class
Orange
? ? ?
Mango
? ? ?
MULTI-CLASS CLASSIFICATION
[2] Sweet Fruit (Apple, Orange and Mango) Classification (Jawaban)
Diketahui:
Total 36 buah yang akan di klasifikasikan dengan 11 buah apple (label 0), 12 buah orange (label 1), dan 13 buah mango (label 2).
Actual Class
Apple Orange Mango
Apple
7 8 9
Predicted Class
Orange
1 2 3
Mango
3 2 1
CONFUSION MATRIX ONLINE CALCULATOR
https://confusionmatrixonline.com/
exercises for students
Python Library for
Binary & Multi-Class Classifications
MACHINE LEARNING LIBRARY & NOTEBOOKS
Scikit-learn (formerly scikits. learn and also Colab notebooks are Jupyter notebooks that run in
known as sklearn) is a free software machine the cloud and are highly integrated with Google
learning library for the Python programming Drive, making them easy to set up, access, and
language. share.
# Total 20 pasien dengan 9 pasien positif cancer (label 1) dan 11 pasien negatif cancer (label 0)
lst_classes = [0, 1]
# Class = apple (label 0), orange (label 1), dan mango (label 2)
lst_classes = [0, 1, 2]
Output
# Compute multi-class confusion matrix
arr_out_matrix = multilabel_confusion_matrix(lst_actual_class, lst_predicted_class, labels=lst_classes)
tp = arr_data[1][1]
fp = arr_data[0][1]
tn = arr_data[0][0]
fn = arr_data[1][0]
sensitivity = round(tp/(tp+fn), 3);
specificity = round(tn/(tn+fp), 3);
accuracy = round((tp+tn)/(tp+fp+tn+fn), 3);
balanced_accuracy = round((sensitivity+specificity)/2, 3);
2) Everything you Should Know about Confusion Matrix for Machine Learning
https://www.analyticsvidhya.com/blog/2020/04/confusion-matrix-machine-learning/
6) Multiclass Classification
https://docs.aws.amazon.com/machine-learning/latest/dg/multiclass-classification.html
11) sklearn.metrics.multilabel_confusion_matrix
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.multilabel_confusion_matrix.html
12) sklearn.metrics.confusion_matrix
https://scikit-learn.org/stable/modules/generated/sklearn.metrics.confusion_matrix.html
END PRESENTATION
Thank you for your attention
S–W–T