Confusion Matrix in Machine Learning
Confusion Matrix in Machine Learning
Custom Search
COURSES Login
HIRE WITH US
Confusion Matrix in Machine Learning
In the eld of machine learning and speci cally the problem of statistical classi cation, a
confusion matrix, also known as an error matrix.
A confusion matrix is a table that is often used to describe the performance of a
classi cation model (or “classi er”) on a set of test data for which the true values are
known. It allows the visualization of the performance of an algorithm.
It allows easy identi cation of confusion between classes e.g. one class is commonly
mislabeled as the other. Most performance measures are computed from the confusion
matrix.
Confusion Matrix:
A confusion matrix is a summary of prediction results on a classi cation problem.
The number of correct and incorrect predictions are summarized with count values and
broken down by each class. This is the key to the confusion matrix.
The confusion matrix shows the ways in which your classi cation model is confused
when it makes predictions.
It gives us insight not only into the errors being made by a classi er but more importantly
the types of errors that are being made.
https://www.geeksforgeeks.org/confusion-matrix-machine-learning/ 1/8
7/10/2019 Confusion Matrix in Machine Learning - GeeksforGeeks
Here,
• Class 1 : Positive
• Class 2 : Negative
However, there are problems with accuracy. It assumes equal costs for both kinds of
errors. A 99% accuracy can be excellent, good, mediocre, poor or terrible depending upon
the problem.
Recall:
Recall can be de ned as the ratio of the total number of correctly classi ed positive
examples divide to the total number of positive examples. High Recall indicates the class
is correctly recognized (small number of FN).
https://www.geeksforgeeks.org/confusion-matrix-machine-learning/ 2/8
7/10/2019 Confusion Matrix in Machine Learning - GeeksforGeeks
Precision:
To get the value of precision we divide the total number of correctly classi ed positive
examples by the total number of predicted positive examples. High Precision indicates an
example labeled as positive is indeed positive (small number of FP).
Precision is given by the relation:
High recall, low precision:This means that most of the positive examples are correctly
recognized (low FN) but there are a lot of false positives.
Low recall, high precision:This shows that we miss a lot of positive examples (high FN)
but those we predict as positive are indeed positive (low FP)
F-measure:
Since we have two measures (Precision and Recall) it helps to have a measurement that
represents both of them. We calculate an F-measure which uses Harmonic Mean in place
of Arithmetic Mean as it punishes the extreme values more.
The F-Measure will always be nearer to the smaller value of Precision or Recall.
Let’s consider an example now, in which we have in nite data elements of class B and a
single element of class A and the model is predicting class A against all the instances in
the test data.
Here,
Precision : 0.0
Recall : 1.0
https://www.geeksforgeeks.org/confusion-matrix-machine-learning/ 3/8
7/10/2019 Confusion Matrix in Machine Learning - GeeksforGeeks
Now:
Arithmetic mean: 0.5
Harmonic mean: 0.0
When taking the arithmetic mean, it would have 50% correct. Despite being the worst
possible outcome! While taking the harmonic mean, the F-measure is 0.
For the simpli cation of the above confusion matrix i have added all the terms like
TP,FP,etc and the row and column totals in the following image:
Now,
Classi cation Rate/Accuracy:
Accuracy = (TP + TN) / (TP + TN + FP + FN)= (100+50) /(100+5+10+50)= 0.90
https://www.geeksforgeeks.org/confusion-matrix-machine-learning/ 4/8
7/10/2019 Confusion Matrix in Machine Learning - GeeksforGeeks
Recall: Recall gives us an idea about when it’s actually yes, how often does it predict yes.
Recall=TP / (TP + FN)=100/(100+5)=0.95
Precision: Precsion tells us about when it predicts yes, how often is it correct.
Precision = TP / (TP + FP)=100/ (100+10)=0.91
F-measure:
Fmeasure=(2*Recall*Precision)/(Recall+Presision)=(2*0.95*0.91)/(0.91+0.95)=0.92
OUTPUT ->
Confusion Matrix :
[[4 2]
[1 3]]
Report :
https://www.geeksforgeeks.org/confusion-matrix-machine-learning/ 5/8
7/10/2019 Confusion Matrix in Machine Learning - GeeksforGeeks
References :
https://machinelearningmastery.com/confusion-matrix-machine-
learning/http://www.dataschool.io/simple-guide-to-confusion-matrix-terminology/
This article is contributed by Abhishek Sharma. If you like GeeksforGeeks and would like
to contribute, you can also write an article using contribute.geeksforgeeks.org or mail
your article to [email protected]. See your article appearing on the
GeeksforGeeks main page and help other Geeks.
Please write comments if you nd anything incorrect, or you want to share more
information about the topic discussed above.
Recommended Posts:
Learning Model Building in Scikit-learn : A Python Machine Learning Library
Arti cial intelligence vs Machine Learning vs Deep Learning
ML | What is Machine Learning ?
Machine Learning in C++
Getting started with Machine Learning
Stacking in Machine Learning
Demystifying Machine Learning
30 minutes to machine learning
Regularization in Machine Learning
Machine Learning - Applications
Machine Learning | Outlier
An introduction to Machine Learning
https://www.geeksforgeeks.org/confusion-matrix-machine-learning/ 6/8