0% found this document useful (0 votes)
76 views12 pages

UNIT4 Confusion Matrix

The document discusses confusion matrices, which are used to evaluate the performance of classification models. A confusion matrix displays the number of correct and incorrect predictions made by a model compared to actual outcomes. It is structured as a table with predictions along one axis and actual outcomes along the other, allowing for calculation of important metrics like accuracy, precision, recall, and F1 score. An example confusion matrix for a medical diagnosis model is provided to demonstrate how values are populated in the table and interpreted.

Uploaded by

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

UNIT4 Confusion Matrix

The document discusses confusion matrices, which are used to evaluate the performance of classification models. A confusion matrix displays the number of correct and incorrect predictions made by a model compared to actual outcomes. It is structured as a table with predictions along one axis and actual outcomes along the other, allowing for calculation of important metrics like accuracy, precision, recall, and F1 score. An example confusion matrix for a medical diagnosis model is provided to demonstrate how values are populated in the table and interpreted.

Uploaded by

Jaya Sankar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON PROGRAMMING & DATA SCIENCE

Confusion Matrix
Confusion Matrix
The confusion matrix is a matrix used to determine the performance of the
classification models for a given set of test data.
 It can only be determined if the true values for test data are known.
The matrix itself can be easily understood, but the related terminologies may
be confusing.
It shows the errors in the model performance in the form of a matrix, hence
also known as an error matrix.
 Some features of Confusion matrix are :
1. For the 2 prediction classes of classifiers, the matrix is of 2*2 table, for 3
classes, it is 3*3 table, and so on.
2. The matrix is divided into two dimensions, that are predicted
values and actual values along with the total number of predictions.
Confusion Matrix
3. Predicted values are those values, which are predicted by the model, and
actual values are the true values for the given observations.
 It looks like the table:

True Negative: Model has given


prediction No, and the real or
actual value was also No.
True Positive: The model has predicted yes, and the actual value was also true.
False Negative: The model has predicted no, but the actual value was Yes, it is
also called as Type-II error.
False Positive: The model has predicted Yes, but the actual value was No. It is
also called a Type-I error.
Confusion Matrix
Need for Confusion Matrix in Machine learning
1. It evaluates the performance of the classification models, when they make
predictions on test data, and tells how good our classification model is.
2. It not only tells the error made by the classifiers but also the type of errors
such as it is either type-I or type-II error.
3. With the help of the confusion matrix, we can calculate the different
parameters for the model, such as accuracy, precision, etc.
Confusion Matrix
Example:
Suppose we are trying to create a model that can predict the result for the
disease that is either a person has that disease or not. So, the confusion matrix
for this is given as

From the above example, we can conclude that:


1. The table is given for the two-class classifier, which has two predictions
"Yes" and "NO." Here, Yes defines that patient has the disease, and No
defines that patient does not has that disease.
Confusion Matrix
Example:

2. The classifier has made a total of 100 predictions. Out of 100


predictions, 89 are true predictions, and 11 are incorrect predictions.
3. The model has given prediction "yes" for 32 times, and "No" for 68 times.
Whereas the actual "Yes" was 27, and actual "No" was 73 times.
Confusion Matrix
Calculations using Confusion Matrix:
We can perform various calculations for the model, such as the model's
accuracy, using this matrix. These calculations are given below:
1. Classification Accuracy
2. Misclassification rate
3. Precision
4. Recall
5. F-measure
Confusion Matrix
Classification Accuracy: 
It is one of the important parameters to determine the accuracy of the
classification problems.
It defines how often the model predicts the correct output.
It can be calculated as the ratio of the number of correct predictions made
by the classifier to all number of predictions made by the classifiers.
The formula is given below:
Confusion Matrix
Misclassification rate: 
It is also termed as Error rate, and it defines how often the model gives the
wrong predictions.
The value of error rate can be calculated as the number of incorrect
predictions to all number of the predictions made by the classifier.
The formula is given below:
Confusion Matrix
Precision:
 It can be defined as the number of correct outputs provided by the model or
out of all positive classes that have predicted correctly by the model, how
many of them were actually true.
It can be calculated using the below formula:
Confusion Matrix
Recall:
 It is defined as the out of total positive classes, how our model predicted
correctly. The recall must be as high as possible.

F-measure:
 If two models have low precision and high recall or vice versa, it is difficult to
compare these models. So, for this purpose, we can use F-score.
 This score helps us to evaluate the recall and precision at the same time. The
F-score is maximum if the recall is equal to the precision. It can be calculated
using the below formula:
Confusion Matrix
Confusion Matrix using scikit-learn in Python
# confusion matrix in sklearn
from sklearn.metrics import confusion_matrix
from sklearn.metrics import classification_report
# actual values
actual = [1,0,0,1,0,0,1,0,0,1]
# predicted values
predicted = [1,0,0,1,0,0,0,1,0,0]
# confusion matrix
matrix = confusion_matrix(actual,predicted, labels=[1,0])
print('Confusion matrix : \n',matrix)
# outcome values order in sklear
ntp, fn, fp, tn = confusion_matrix(actual,predicted,labels=[1,0]).reshape(-1)
print('Outcome values : \n', tp, fn, fp, tn)
# classification report for precision, recall f1-score and accuracy
matrix = classification_report(actual,predicted,labels=[1,0])
print('Classification report : \n',matrix)

You might also like