Confusion Matrix: Fundamentals

Confusion Matrix is one of the fundamentals of Machine Learning. In this post, we try and simplify it for you.

Say, you have some clinical measurements like chest pain, blood circulation, blocked arteries, and weight. You wish to use a machine-learning algorithm to predict whether or not someone will develop heart disease. To do this you could use Logistic Regression, K-NN, Decision Tree, or some other ML algorithms to classify each of the cases as positive or negative.

First up, you divide the data into training and testing sets. Next, you train all the relevant algorithms using training data. Subsequently, you test each method. Finally, you evaluate how each method performed on the testing data.

To evaluate the performance, you find the Classification Accuracy of each model. Classification accuracy is the ratio of correct predictions to total predictions made, i.e.,

Classification accuracy (CA) = Correct predictions (CP) / Total predictions (TP)

It is often presented as a percentage by multiplying the result by 100.

Error rate = (1 - (CP / TP)) * 100

While classification accuracy is a great place to start, it often encounters problems in practice. The main problem with it is that it does not provide a detailed view of the performance of your classification model. 

 It’s here that the confusion matrix comes into the picture.

So, what is a Confusion Matrix?

Confusion Matrix is a table that is used to measure the performance of the classification Model. The general idea is to count the number of times instances of class A are classified as class B. 

Let’s understand this with an example of spam email prediction. 

The simplest confusion matrix looks like the following table


For python the labels are slightly different:

Actual0TN FP
Actual1FN TP


  • TP -> True Positive (Predicting spams as spam).
  • FN ->False Negative (Predicting spammers as non-spam).
  • FP -> False Positive (predicting Non-spammers as spam).
  • TN -> True Negative (Predicting Non-spammers as non-spam).

By considering the above email spam example, FN is the riskiest because our model is predicting spam as non-spam. By this, the user may undergo any fraud. So, let’s call it a Type-1 error. While adding the cost function our main aim is to reduce the Type-1 error.  

FP is the type-2 error because when the regular emails are getting marked spam. To increase accuracy, we need to work on both the errors.

The error types will differ from one problem to another. To evaluate the performance, we mainly focus on the parameters explained below.

Parameters to evaluate the performance of a model

For most models, the performance is evaluated on the basis of the following four parameters:

  1. Sensitivity (True Positive Rate) (TPR)

When the actual value is positive, how often is the positive prediction is correct?


FP -> If mail is not spam and our model is detecting it as spam

FN -> If mail is spam but our model is detecting it as non-spam

  1. Specificity (True Negative Rate): When the actual value is negative, how frequently is the negative prediction correct?
Alternatively, Specificity= 1 - false positive rate
  1. False Positive Rate: How often is the positive prediction incorrect, when the actual value is negative?
False positive Rate= FP/(TN+FP)

OR, False positive Rate=1-Specificity

  1. Precision: When a positive value is predicted, how often is the prediction correct?

The rate of success can be calculated as

r = (TN+TP)/(FN+FP)

If that’s too many equations to digest, here’s a perfect illustration explaining the confusion matrix:

Notably, the confusion matrix is not limited to binary classifiers. The size of the confusion matrix is determined by the number of things we want to predict. 

In summary, the Confusion Matrix tells you what your machine learning algorithm did right and what it did wrong. Here’s an interesting course which may help you explore it further: <link>

Recent Articles


Related Stories


Comments are closed.

Stay on op - Ge the daily news in your inbox

[tdn_block_newsletter_subscribe input_placeholder=”Email address” btn_text=”Subscribe” tds_newsletter2-image=”730″ tds_newsletter2-image_bg_color=”#c3ecff” tds_newsletter3-input_bar_display=”” tds_newsletter4-image=”731″ tds_newsletter4-image_bg_color=”#fffbcf” tds_newsletter4-btn_bg_color=”#f3b700″ tds_newsletter4-check_accent=”#f3b700″ tds_newsletter5-tdicon=”tdc-font-fa tdc-font-fa-envelope-o” tds_newsletter5-btn_bg_color=”#000000″ tds_newsletter5-btn_bg_color_hover=”#4db2ec” tds_newsletter5-check_accent=”#000000″ tds_newsletter6-input_bar_display=”row” tds_newsletter6-btn_bg_color=”#da1414″ tds_newsletter6-check_accent=”#da1414″ tds_newsletter7-image=”732″ tds_newsletter7-btn_bg_color=”#1c69ad” tds_newsletter7-check_accent=”#1c69ad” tds_newsletter7-f_title_font_size=”20″ tds_newsletter7-f_title_font_line_height=”28px” tds_newsletter8-input_bar_display=”row” tds_newsletter8-btn_bg_color=”#00649e” tds_newsletter8-btn_bg_color_hover=”#21709e” tds_newsletter8-check_accent=”#00649e” embedded_form_code=”YWN0aW9uJTNEJTIybGlzdC1tYW5hZ2UuY29tJTJGc3Vic2NyaWJlJTIy” tds_newsletter=”tds_newsletter1″ tds_newsletter3-all_border_width=”2″ tds_newsletter3-all_border_color=”#e6e6e6″ tdc_css=”eyJhbGwiOnsibWFyZ2luLWJvdHRvbSI6IjAiLCJib3JkZXItY29sb3IiOiIjZTZlNmU2IiwiZGlzcGxheSI6IiJ9fQ==” tds_newsletter1-btn_bg_color=”#0d42a2″ tds_newsletter1-f_btn_font_family=”406″ tds_newsletter1-f_btn_font_transform=”uppercase” tds_newsletter1-f_btn_font_weight=”800″ tds_newsletter1-f_btn_font_spacing=”1″ tds_newsletter1-f_input_font_line_height=”eyJhbGwiOiIzIiwicG9ydHJhaXQiOiIyLjYiLCJsYW5kc2NhcGUiOiIyLjgifQ==” tds_newsletter1-f_input_font_family=”406″ tds_newsletter1-f_input_font_size=”eyJhbGwiOiIxMyIsImxhbmRzY2FwZSI6IjEyIiwicG9ydHJhaXQiOiIxMSIsInBob25lIjoiMTMifQ==” tds_newsletter1-input_bg_color=”#fcfcfc” tds_newsletter1-input_border_size=”0″ tds_newsletter1-f_btn_font_size=”eyJsYW5kc2NhcGUiOiIxMiIsInBvcnRyYWl0IjoiMTEiLCJhbGwiOiIxMyJ9″ content_align_horizontal=”content-horiz-center”]