0% found this document useful (0 votes)
15 views5 pages

Confusion Matrix Implementation Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Confusion Matrix Implementation Guide

Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Department of Computer Engineering

EXPERIMENT NO. 4
Aim:- To study and implement confusion matrix.
Theory:-
Confusion matrix:-
Confusion matrix is a simple table used to measure how well a classification model is
performing. It compares the predictions made by the model with the actual results and
shows where the model was right or wrong. This helps you understand where the model is
making mistakes so you can improve it. It breaks down the predictions into four categories:
 True Positive (TP): The model correctly predicted a positive outcome i.e the actual
outcome was positive.
 True Negative (TN): The model correctly predicted a negative outcome i.e the actual
outcome was negative.
 False Positive (FP): The model incorrectly predicted a positive outcome i.e the actual
outcome was negative. It is also known as a Type I error.
 False Negative (FN): The model incorrectly predicted a negative outcome i.e the actual
outcome was positive. It is also known as a Type II error.

Confusion Matrix
It also helps calculate key measures like accuracy, precision and recall which give a better
idea of performance especially when the data is imbalanced.
Metrics based on Confusion Matrix Data

1. Accuracy
Accuracy shows how many predictions the model got right out of all the predictions. It
gives idea of overall performance but it can be misleading when one class is more dominant
over the other. For example a model that predicts the majority class correctly most of the
time might have high accuracy but still fail to capture important details about other classes.
It can be calculated using the below formula:
Accuracy=TP+TNTP+TN+FP+FNAccuracy=TP+TN+FP+FNTP+TN
Department of Computer Engineering

2. Precision
Precision focus on the quality of the model’s positive predictions. It tells us how many of
the "positive" predictions were actually correct. It is important in situations where false
positives need to be minimized such as detecting spam emails or fraud. The formula of
precision is:
Precision=TPTP+FPPrecision=TP+FPTP

3. Recall
Recall measures how how good the model is at predicting positives. It shows the proportion
of true positives detected out of all the actual positive instances. High recall is essential
when missing positive cases has significant consequences like in medical tests.
Recall=TPTP+FNRecall=TP+FNTP

4. F1-Score
F1-score combines precision and recall into a single metric to balance their trade-off. It
provides a better sense of a model’s overall performance particularly for imbalanced
datasets. It is helpful when both false positives and false negatives are important though it
assumes precision and recall are equally important but in some situations one might matter
more than the other.
F1-Score=2⋅Precision⋅RecallPrecision+RecallF1-
Score=Precision+Recall2⋅Precision⋅Recall

5. Specificity
Specificity is another important metric in the evaluation of classification models
particularly in binary classification. It measures the ability of a model to correctly identify
negative instances. Specificity is also known as the True Negative Rate Formula is given
by:
Specificity=TNTN+FPSpecificity=TN+FPTN

6. Type 1 and Type 2 error


Type 1 and Type 2 error are:
 Type 1 error: It occurs when the model incorrectly predicts a positive instance but the
actual instance is negative. This is also known as a false positive. Type 1 Errors affect
the precision of a model which measures the accuracy of positive predictions.
Type 1 Error=FPFP+TNType 1 Error=FP+TNFP
 Type 2 error: This occurs when the model fails to predict a positive instance even
though it is actually positive. This is also known as a false negative. Type 2 Errors
impact the recall of a model which measures how well the model identifies all actual
positive cases.
Type 2 Error=FNTP+FNType 2 Error=TP+FNFN
Department of Computer Engineering

Code:-

import numpy as np
from [Link] import confusion_matrix,classification_report
import seaborn as sns
import [Link] as plt
actual = [Link](
['Dog','Dog','Dog','Not Dog','Dog','Not Dog','Dog','Dog','Not Dog','Not Dog'])
predicted = [Link](
['Dog','Not Dog','Dog','Not Dog','Dog','Dog','Dog','Dog','Not Dog','Not Dog'])
[Link](cm,
annot=True,
fmt='g',
xticklabels=['Dog','Not Dog'],
yticklabels=['Dog','Not Dog'])
[Link]('Actual', fontsize=13)
[Link]('Confusion Matrix', fontsize=17, pad=20)
[Link]().xaxis.set_label_position('top')
[Link]('Prediction', fontsize=13)
[Link]().xaxis.tick_top()

[Link]().figure.subplots_adjust(bottom=0.2)
[Link]().[Link](0.5, 0.05, 'Prediction', ha='center', fontsize=13)
[Link]()
print(classification_report(actual, predicted))
Department of Computer Engineering
Department of Computer Engineering

Output:-

Conclusion:- We successfully studied and implemented confusion matrix using ML.

You might also like