{"id":7,"date":"2020-08-04T11:58:34","date_gmt":"2020-08-04T11:58:34","guid":{"rendered":"https:\/\/data-science.gotoauthority.com\/2020\/08\/04\/best-confusion-matrix-guide-with-sklearn-python\/"},"modified":"2020-08-04T11:58:34","modified_gmt":"2020-08-04T11:58:34","slug":"best-confusion-matrix-guide-with-sklearn-python","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2020\/08\/04\/best-confusion-matrix-guide-with-sklearn-python\/","title":{"rendered":"Best Confusion Matrix Guide With Sklearn Python"},"content":{"rendered":"<div id=\"tve_editor\" data-post-id=\"4211\" readability=\"119.7074067267\">\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173aebb0daa\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1_Confusion-Matrix.png?resize=613%2C350&amp;ssl=1\" class=\"tve_image wp-image-4213\" alt=\"Confusion Matrix sklearn\" data-id=\"4213\" width=\"613\" data-init-width=\"700\" height=\"350\" data-init-height=\"400\" title=\"1_Confusion Matrix\" loading=\"lazy\" data-width=\"613\" data-height=\"350\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1_Confusion-Matrix.png?w=700&amp;ssl=1 700w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1_Confusion-Matrix.png?resize=300%2C171&amp;ssl=1 300w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4213\" alt=\"Confusion Matrix sklearn\" data-id=\"4213\" width=\"613\" data-init-width=\"700\" height=\"350\" data-init-height=\"400\" title=\"1_Confusion Matrix\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1_Confusion-Matrix.png?resize=613%2C350&amp;ssl=1\" data-width=\"613\" data-height=\"350\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1_Confusion-Matrix.png?w=700&amp;ssl=1 700w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1_Confusion-Matrix.png?resize=300%2C171&amp;ssl=1 300w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box fr-basic\" readability=\"52.774054054054\">\n<p dir=\"ltr\">How many times your read about confusion matrix, and after a while forgot about the <strong>ture positive, false negative<\/strong> &#8230; etc, Even you implemented confusion matrix with sklearn or tensorflow, Still we get confusion about the each componets of the matrix.<\/p>\n<p dir=\"ltr\">But after reading this article, you will never forget confusion matrix any more.<\/p>\n<p dir=\"ltr\">So forgot everything you learned so far, and start fresh now.\u00a0<\/p>\n<p dir=\"ltr\">The machine learning model building journey end goal is not about building the model. The real journey will begin when we start measuring the performance of the model we built.\u00a0<\/p>\n<p dir=\"ltr\">We are having numerous ways to quantify the performance of the model. The <strong>confusion matrix<\/strong> is one such case, This model performance metrics is popularly used for quantifying the performance of the <a href=\"https:\/\/dataaspirant.com\/classification-clustering-alogrithms\/\" target=\"_blank\" class=\"tve-froala\" rel=\"noopener noreferrer\">classification models<\/a> which fall under the supervised learning algorithms.<\/p>\n<p dir=\"ltr\">Before we drive further let me explain what you are about to learn in this article<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box fr-basic\" readability=\"46.984126984127\">\n<h2 dir=\"ltr\" class=\"\" id=\"t-1596364245964\">Why we need a confusion matrix<\/h2>\n<p>Before we learn about the confusion matrix, Let&#8217;s understand what is the need of using the confusion matrix as performance metrics for the classification models.<\/p>\n<p>Accuracy is the popular model evaluation method used for the majority of the classification models in <a href=\"https:\/\/dataaspirant.com\/supervised-and-unsupervised-learning\/\" target=\"_blank\" class=\"tve-froala\" rel=\"noopener noreferrer\">supervised learning algorithms<\/a>. This makes us to think about the below question. \u00a0<\/p>\n<p>When we are having accuracy as a measure for knowing the performance of the classification models then why we need another measure to quantify the performance of the model?<\/p>\n<p>Let\u2019s understand this with an example.<\/p>\n<p>\u00a0Suppose if we are building a <a href=\"https:\/\/dataaspirant.com\/2017\/04\/15\/implement-logistic-regression-model-python-binary-classification\/\" target=\"_blank\" class=\"tve-froala\" rel=\"noopener noreferrer\">binary classification model<\/a> for imbalanced target class data.<\/p>\n<p>Let me give you an example of an imbalanced dataset. In the target class imbalance dataset, the target classes are not properly balanced.\u00a0<\/p>\n<h3 dir=\"ltr\" class=\"\" id=\"t-1596364245965\">What is target class imbalance<\/h3>\n<p dir=\"ltr\">Let&#8217;s say we are having <strong>two expected<\/strong> classes for the target variable.<\/p>\n<p dir=\"ltr\">Which are<\/p>\n<ol class=\"\">\n<li dir=\"ltr\">Positive\u00a0<\/li>\n<li dir=\"ltr\">Negative<\/li>\n<\/ol>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173aec2028f\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=626%2C347&amp;ssl=1\" class=\"tve_image wp-image-4219\" alt=\"Class Imbalance data \" data-id=\"4219\" width=\"626\" data-init-width=\"1024\" height=\"347\" data-init-height=\"567\" title=\"2_target_distribution\" loading=\"lazy\" data-width=\"626\" data-height=\"347\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=1024%2C567&amp;ssl=1 1024w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=300%2C166&amp;ssl=1 300w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=768%2C425&amp;ssl=1 768w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?w=1228&amp;ssl=1 1228w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4219\" alt=\"Class Imbalance data \" data-id=\"4219\" width=\"626\" data-init-width=\"1024\" height=\"347\" data-init-height=\"567\" title=\"2_target_distribution\" loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=626%2C347&amp;ssl=1\" data-width=\"626\" data-height=\"347\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=1024%2C567&amp;ssl=1 1024w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=300%2C166&amp;ssl=1 300w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?resize=768%2C425&amp;ssl=1 768w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_target_distribution.png?w=1228&amp;ssl=1 1228w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"72\">\n<p><strong>95%<\/strong> percentage we are getting a positive class and only <strong>5%<\/strong> percentage we&#8217;re getting the negative class. Here positive class is <strong>dominating<\/strong> the negative class, this kind of in balance of the target class within the target classes is called <strong>imbalance<\/strong>.<\/p>\n<h3 dir=\"ltr\" class=\"\" id=\"t-1596364245966\">Examples of the imbalanced dataset<\/h3>\n<p dir=\"ltr\">\u00a0Below are some of the examples with the imbalance dataset.<\/p>\n<ul class=\"\">\n<li dir=\"ltr\">Credit card fraud detections datasets.<\/li>\n<li dir=\"ltr\">\u00a0Default customers prediction datasets<\/li>\n<\/ul>\n<p dir=\"ltr\">In the above examples the target classes distribution, will not be in equally distributed. In general <strong>target class imbalance<\/strong> means the target classes are not equally distributed, one class will be dominating the other classes.<\/p>\n<p dir=\"ltr\">\u00a0Let&#8217;s go back to our question of <strong>what is the need for confusion matrix <\/strong>when we have the accuracy to calculate the performance of the classification model?<\/p>\n<p dir=\"ltr\">In the above example, we have seen the positive class is coming 95% percent cases whereas the negative class is coming only 5% of the time.\u00a0<\/p>\n<p dir=\"ltr\">Without building any machine learning model if we predict all the target classes as <strong>positive<\/strong>. What do you think our model accuracy could be?<\/p>\n<p dir=\"ltr\">Yes, you are correct our model accuracy is <strong>95%<\/strong>\u00a0<\/p>\n<p dir=\"ltr\">But do you think this is the correct way of quantifying the performance of the model?<\/p>\n<blockquote dir=\"ltr\" class=\"\"><p><strong>A big No!<\/strong><\/p><\/blockquote>\n<p dir=\"ltr\">So when we are dealing with target class imbalance datasets, <strong>accuracy<\/strong> is not the best performance measure technique.\u00a0<\/p>\n<p dir=\"ltr\">In such cases, we will use the <strong>confusion matrix<\/strong> to measure the efficiency of the classification model.<\/p>\n<p dir=\"ltr\">In the next section of this article, we will learn more about the confusion matrix representation.<\/p>\n<h2 dir=\"ltr\" class=\"\" id=\"t-1596364245967\">What is confusion matrix?<\/h2>\n<p dir=\"ltr\">To understand the confusion matrix in the much deeper level we are considering the below example dataset.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"42\">\n<p dir=\"ltr\">The above table contains the actual target class and the predicted class information. <\/p>\n<p dir=\"ltr\">If we calcualte the accuracy of this data it will 70%, as the predicted target column\u2019s values are matching <strong>7<\/strong> times in an overall 10 cases in actual targets.<\/p>\n<p dir=\"ltr\">Accuracy is not able to explain the below question.<\/p>\n<ul class=\"\">\n<li dir=\"ltr\">How many actual positive targets are predicted as positive?<\/li>\n<li dir=\"ltr\">How many actual positive targets are predicted as negative?<\/li>\n<li dir=\"ltr\">How many actual negative targets are predicted as positive?<\/li>\n<li dir=\"ltr\">How many actual negative targets are predicted as negative?<\/li>\n<\/ul>\n<p dir=\"ltr\">We can answer all these questions with a confusion matrix, below is the pictorial representation of answer all the above questions.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173aece9073\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=626%2C369&amp;ssl=1\" class=\"tve_image wp-image-4226\" alt=\"Confusion matrix representation\" data-id=\"4226\" width=\"626\" data-init-width=\"1024\" height=\"369\" data-init-height=\"604\" title=\"3_confusion_matrix\" loading=\"lazy\" data-width=\"626\" data-height=\"369\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=1024%2C604&amp;ssl=1 1024w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=300%2C177&amp;ssl=1 300w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=768%2C453&amp;ssl=1 768w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=1536%2C905&amp;ssl=1 1536w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?w=1968&amp;ssl=1 1968w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4226\" alt=\"Confusion matrix representation\" data-id=\"4226\" width=\"626\" data-init-width=\"1024\" height=\"369\" data-init-height=\"604\" title=\"3_confusion_matrix\" loading=\"lazy\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=626%2C369&amp;ssl=1\" data-width=\"626\" data-height=\"369\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=1024%2C604&amp;ssl=1 1024w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=300%2C177&amp;ssl=1 300w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=768%2C453&amp;ssl=1 768w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?resize=1536%2C905&amp;ssl=1 1536w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?w=1968&amp;ssl=1 1968w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_confusion_matrix.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"40\">\n<p>A confusion matrix is a matrix representation of showing how well the trained model predicting each target class with respect to the counts. Confusion Matrix mainly used for the classification algorithms which fall under supervised learning.\u00a0<\/p>\n<p>Below are the descriptions for the terms used in the confusion matrix<\/p>\n<ul class=\"\">\n<li>\n<strong>Ture positive<\/strong>: \u00a0Target is positive \u00a0and the model predicted it as positive<\/li>\n<li>\n<strong>False negative<\/strong>: Target is positive and the model predicted it as negative<\/li>\n<li>\n<strong>False positive<\/strong>: \u00a0Target is negative and the model predicted it as positive<\/li>\n<li>\n<strong>True negative<\/strong>: \u00a0Target is negative and the model predicted it as negative<\/li>\n<\/ul>\n<p>Using the above positive and negative targets information table, we will populate the matrix which gives a much more clear understanding of how the confusion matrix constructed.<\/p>\n<h3 class=\"\" id=\"t-1596364245968\">Confusion matrix representation for a binary classification problem<\/h3>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173aed10f38\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=626%2C392&amp;ssl=1\" class=\"tve_image wp-image-4230\" alt=\"Confusion matrix for binary classification\" data-id=\"4230\" width=\"626\" data-init-width=\"1024\" height=\"392\" data-init-height=\"642\" title=\"4_binary_classification_confusion_matrix\" loading=\"lazy\" data-width=\"626\" data-height=\"392\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=1024%2C642&amp;ssl=1 1024w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=300%2C188&amp;ssl=1 300w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=768%2C482&amp;ssl=1 768w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=1536%2C964&amp;ssl=1 1536w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?w=1750&amp;ssl=1 1750w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4230\" alt=\"Confusion matrix for binary classification\" data-id=\"4230\" width=\"626\" data-init-width=\"1024\" height=\"392\" data-init-height=\"642\" title=\"4_binary_classification_confusion_matrix\" loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=626%2C392&amp;ssl=1\" data-width=\"626\" data-height=\"392\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=1024%2C642&amp;ssl=1 1024w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=300%2C188&amp;ssl=1 300w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=768%2C482&amp;ssl=1 768w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?resize=1536%2C964&amp;ssl=1 1536w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?w=1750&amp;ssl=1 1750w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_binary_classification_confusion_matrix.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"51\">\n<p>The above image is representing the confusion matrix for the binary classification problem, each cell values of the matrixs, are calculated for the example dataset we showed before.<\/p>\n<ul class=\"\">\n<li>TP: Out of 8 actual positive cases, in 6 cases the model predicted positive.<\/li>\n<li>FN: (8 &#8211; 6), the remaining 2 cases will fall into the true negative cases.<\/li>\n<li>FP:\u00a0 We are having 2 negative cases and 1 we predicted as positive.<\/li>\n<li>TN: Out of 2 negative cases, the model predicted 1 negative case correctly.<\/li>\n<\/ul>\n<p>So these cell values of the confusion matrix are addressed the above questions we have.\u00a0<\/p>\n<p>By seeing the matrix representation, we can understand where the model is much more accurate and we can clearly know where the model not able to predict properly. This helps in tuning the right model parameters to reduce the false positive and false negative.<\/p>\n<p>By now we are having clear understanding about each component of the confusion but still <strong>TP, TN, FP, FN<\/strong> is hard to remember, we know the concepts but these terms are really a bit confusing.\u00a0<\/p>\n<p>So Let&#8217;s understand how we can remember these terms forever.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173b26ab0e5\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=613%2C402&amp;ssl=1\" class=\"tve_image wp-image-4297\" alt=\"Ture positive and negatives graph\" data-id=\"4297\" width=\"613\" data-init-width=\"1024\" height=\"402\" data-init-height=\"672\" title=\"11 ture_positve_negatives\" loading=\"lazy\" data-width=\"613\" data-height=\"402\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=1024%2C672&amp;ssl=1 1024w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=300%2C197&amp;ssl=1 300w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=768%2C504&amp;ssl=1 768w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=1536%2C1008&amp;ssl=1 1536w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?w=1706&amp;ssl=1 1706w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4297\" alt=\"Ture positive and negatives graph\" data-id=\"4297\" width=\"613\" data-init-width=\"1024\" height=\"402\" data-init-height=\"672\" title=\"11 ture_positve_negatives\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=613%2C402&amp;ssl=1\" data-width=\"613\" data-height=\"402\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=1024%2C672&amp;ssl=1 1024w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=300%2C197&amp;ssl=1 300w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=768%2C504&amp;ssl=1 768w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?resize=1536%2C1008&amp;ssl=1 1536w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?w=1706&amp;ssl=1 1706w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11-ture_positve_negatives.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"59\">\n<p dir=\"ltr\">In the above image we spilt the each term into two characters, the <strong>second<\/strong><br \/>\n<strong>character<\/strong> represents what the model predicting, in our case, is the model predicting postive class or negative class. The <strong>first character<\/strong> represents, is the model prediction is correct or not.<\/p>\n<p dir=\"ltr\">Let&#8217;s undersand with an example.<\/p>\n<p dir=\"ltr\">Suppose TN means, the second character is <strong>N<\/strong> means model predicted negative class, the first character <strong>T<\/strong> means model predicted correctly.<\/p>\n<p dir=\"ltr\">Hope this gives clear picture about these individual components about the matrix.<\/p>\n<h3 dir=\"ltr\" class=\"\" id=\"t-1596364245969\">What is the ideal model?<\/h3>\n<p>The ideal machine learning model which will always predict the correct target values. If we are using accuracy as a measure to quantify the performance of the model. The ideal model should get <strong>100%<\/strong> accuracy.<\/p>\n<p>In the same way, to say a model is ideal with confusion matrix performance metrics, it should have zero cases in false positive and false negative, which are called as type 1 and type 2 errors.<\/p>\n<h2 class=\"\" id=\"t-1596364245970\">What are Type 1 and Type 2 errors?<\/h2>\n<p dir=\"ltr\">Below are the two error types we can represent with confusion matrix.<\/p>\n<ul class=\"\">\n<li dir=\"ltr\">Type 01 (False positive)<\/li>\n<li dir=\"ltr\">Type 02 (False negative)<\/li>\n<\/ul>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"33\">\n<p>The above image clearly explaining the difference between Type 1 and type 2 errors. Below are the key difference between type 1 and type 2 errors.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv-page-section thrv-lp-block\" data-inherit-lp-settings=\"1\" data-css=\"tve-u-173aed5dc25\" tcb-template-name=\"Pros&amp;Cons 02\" tcb-template-id=\"60857\" data-keep-css_id=\"1\" readability=\"31\">\n<div class=\"tve-page-section-in tve_empty_dropzone  \" data-css=\"tve-u-173aed5ddf3\" readability=\"36\">\n<grammarly-extension class=\"_1KJtL\"><\/grammarly-extension><grammarly-extension class=\"_1KJtL\"><\/grammarly-extension><grammarly-extension class=\"_1KJtL\"><\/grammarly-extension><grammarly-extension class=\"_1KJtL\"><\/grammarly-extension><\/p>\n<h3 class=\"\" id=\"t-1596364245972\">Difference between Type 1 and Type 2 errors<\/h3>\n<div class=\"thrv_wrapper thrv-columns dynamic-group-kbuvrect\" data-css=\"tve-u-173aed5dc2b\">\n<div class=\"tcb-flex-row v-2 tcb--cols--2\" data-css=\"tve-u-173aed5dc2c\">\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbuvrcvb\" data-css=\"tve-u-173aed5dc2d\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbuvrbgn\" data-css=\"tve-u-173aed5dc2e\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv-styled_list tcb-icon-display dynamic-group-kbuvr94e\" data-icon-code=\"icon-check-circle-solid\" data-css=\"tve-u-173aed5dc34\">\n<ul class=\"tcb-styled-list\">\n<li class=\"thrv-styled-list-item dynamic-group-kbksn48l\" data-css=\"tve-u-173aed5dc35\">\n<p><span class=\"thrv-advanced-inline-text tve_editable tcb-styled-list-icon-text tcb-no-delete tcb-no-save dynamic-group-kbksmpx3\" data-css=\"tve-u-173aed5dc37\">False positive from confusion matrix.<\/span>\n<\/li>\n<li class=\"thrv-styled-list-item dynamic-group-kbksn48l\" data-css=\"tve-u-173aed5dc38\">\n<p><span class=\"thrv-advanced-inline-text tve_editable tcb-styled-list-icon-text tcb-no-delete tcb-no-save dynamic-group-kbksmpx3\" data-css=\"tve-u-173aed5dc3a\">Predicting negative as positive.<\/span>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbuvrcvb\" data-css=\"tve-u-173aed5dc41\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbuvrbgn\" data-css=\"tve-u-173aed5dc43\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper thrv-styled_list dynamic-group-kbuvr94e\" data-icon-code=\"icon-times-circle-solid\" data-css=\"tve-u-173aed5dc49\">\n<ul class=\"tcb-styled-list\">\n<li class=\"thrv-styled-list-item dynamic-group-kbksn48l\" data-css=\"tve-u-173aed5dc4a\">\n<p><span class=\"thrv-advanced-inline-text tve_editable tcb-styled-list-icon-text tcb-no-delete tcb-no-save dynamic-group-kbksmpx3\" data-css=\"tve-u-173aed5dc4c\">False negative from confusion matrix.<\/span>\n<\/li>\n<li class=\"thrv-styled-list-item dynamic-group-kbksn48l\" data-css=\"tve-u-173aed5dc4d\">\n<p><span class=\"thrv-advanced-inline-text tve_editable tcb-styled-list-icon-text tcb-no-delete tcb-no-save dynamic-group-kbksmpx3\" data-css=\"tve-u-173aed5dc4f\">Predicting positive as negative.<\/span>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"36\">\n<p>By now we clearly understood how the confusion matrix can build and aware of the components of the confusion matrix.\u00a0<\/p>\n<p>Now we will learn how to implement the confusion matrix in different ways. Before that below is the full representation \u00a0of the learnings we got in one picture.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173b807e727\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=690%2C449&amp;ssl=1\" class=\"tve_image wp-image-4315\" alt=\"Confusion matrix full representation\" data-id=\"4315\" width=\"690\" data-init-width=\"1024\" height=\"449\" data-init-height=\"667\" title=\"Confusion matrix full representation\" loading=\"lazy\" data-width=\"760\" data-height=\"495\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=1024%2C667&amp;ssl=1 1024w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=300%2C195&amp;ssl=1 300w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=768%2C500&amp;ssl=1 768w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=1536%2C1000&amp;ssl=1 1536w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?w=1612&amp;ssl=1 1612w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 690px) 100vw, 690px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4315\" alt=\"Confusion matrix full representation\" data-id=\"4315\" width=\"690\" data-init-width=\"1024\" height=\"449\" data-init-height=\"667\" title=\"Confusion matrix full representation\" loading=\"lazy\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=690%2C449&amp;ssl=1\" data-width=\"760\" data-height=\"495\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=1024%2C667&amp;ssl=1 1024w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=300%2C195&amp;ssl=1 300w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=768%2C500&amp;ssl=1 768w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?resize=1536%2C1000&amp;ssl=1 1536w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?w=1612&amp;ssl=1 1612w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12-Confusion-matrix-full-representation.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 690px) 100vw, 690px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"32\">\n<h2 class=\"\" id=\"t-1596364245975\">Implementing confusion matrix in python<\/h2>\n<p>We are going to implement confusion matrix in two different ways.<\/p>\n<ol class=\"\">\n<li>Confusion matrix with Sklearn<\/li>\n<li>Confusion matrix with Tensorflow<\/li>\n<\/ol>\n<h3 class=\"\" id=\"t-1596364245976\">Confusion matrix implementation with sklearn<\/h3>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173aede801b\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=613%2C323&amp;ssl=1\" class=\"tve_image wp-image-4244\" alt=\"Confusion matrix sklearn\" data-id=\"4244\" width=\"613\" data-init-width=\"1024\" height=\"323\" data-init-height=\"540\" title=\"7_confusion_matrix_sklearn\" loading=\"lazy\" data-width=\"613\" data-height=\"323\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=1024%2C540&amp;ssl=1 1024w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=300%2C158&amp;ssl=1 300w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=768%2C405&amp;ssl=1 768w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?w=1524&amp;ssl=1 1524w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4244\" alt=\"Confusion matrix sklearn\" data-id=\"4244\" width=\"613\" data-init-width=\"1024\" height=\"323\" data-init-height=\"540\" title=\"7_confusion_matrix_sklearn\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=613%2C323&amp;ssl=1\" data-width=\"613\" data-height=\"323\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=1024%2C540&amp;ssl=1 1024w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=300%2C158&amp;ssl=1 300w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=768%2C405&amp;ssl=1 768w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?w=1524&amp;ssl=1 1524w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173b042d62d\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=626%2C61&amp;ssl=1\" class=\"tve_image wp-image-4291\" alt=\"Confusion matrix output\" data-id=\"4291\" width=\"626\" data-init-width=\"1024\" height=\"61\" data-init-height=\"100\" title=\"10_Confusion-matrix-output\" loading=\"lazy\" data-width=\"626\" data-height=\"61\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=1024%2C100&amp;ssl=1 1024w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=300%2C29&amp;ssl=1 300w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=768%2C75&amp;ssl=1 768w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=1536%2C150&amp;ssl=1 1536w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?w=1924&amp;ssl=1 1924w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4291\" alt=\"Confusion matrix output\" data-id=\"4291\" width=\"626\" data-init-width=\"1024\" height=\"61\" data-init-height=\"100\" title=\"10_Confusion-matrix-output\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=626%2C61&amp;ssl=1\" data-width=\"626\" data-height=\"61\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=1024%2C100&amp;ssl=1 1024w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=300%2C29&amp;ssl=1 300w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=768%2C75&amp;ssl=1 768w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?resize=1536%2C150&amp;ssl=1 1536w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?w=1924&amp;ssl=1 1924w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_Confusion-matrix-output.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 626px) 100vw, 626px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"36\">\n<p>The scikit learn confusion matrix representation will be a bit different, as scikit learn considers the actual target classes as <strong>columns<\/strong> and the predicted classes as <strong>rows<\/strong>, because of this scikit learn confusion matrix output look different.<\/p>\n<h3 class=\"\" id=\"t-1596364245977\">Confusion matrix implementation with Tensorflow<\/h3>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173aee325cb\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=613%2C367&amp;ssl=1\" class=\"tve_image wp-image-4249\" alt=\"Confusion matrix tensor flow\" data-id=\"4249\" width=\"613\" data-init-width=\"1024\" height=\"367\" data-init-height=\"613\" title=\"8_confusion-matrix-tensorflow\" loading=\"lazy\" data-width=\"613\" data-height=\"367\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=1024%2C613&amp;ssl=1 1024w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=300%2C180&amp;ssl=1 300w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=768%2C460&amp;ssl=1 768w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?w=1524&amp;ssl=1 1524w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4249\" alt=\"Confusion matrix tensor flow\" data-id=\"4249\" width=\"613\" data-init-width=\"1024\" height=\"367\" data-init-height=\"613\" title=\"8_confusion-matrix-tensorflow\" loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=613%2C367&amp;ssl=1\" data-width=\"613\" data-height=\"367\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=1024%2C613&amp;ssl=1 1024w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=300%2C180&amp;ssl=1 300w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?resize=768%2C460&amp;ssl=1 768w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?w=1524&amp;ssl=1 1524w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_confusion-matrix-tensorflow.png?w=1380&amp;ssl=1 1380w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"35\">\n<h2 class=\"\" id=\"t-1596364245978\">How to plot the confusion matrix<\/h2>\n<p>Using the below code, we can easily plot the confusion matrix, we are using seaborn heat map to visuvalize the confusion matrix in more representive way.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173aeebc4c0\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=613%2C336&amp;ssl=1\" class=\"tve_image wp-image-4252\" alt=\"Confusion matrix graph\" data-id=\"4252\" width=\"613\" data-init-width=\"1024\" height=\"336\" data-init-height=\"561\" title=\"Confusion matrix graph\" loading=\"lazy\" data-width=\"613\" data-height=\"336\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=1024%2C561&amp;ssl=1 1024w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=300%2C164&amp;ssl=1 300w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=768%2C421&amp;ssl=1 768w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?w=1368&amp;ssl=1 1368w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4252\" alt=\"Confusion matrix graph\" data-id=\"4252\" width=\"613\" data-init-width=\"1024\" height=\"336\" data-init-height=\"561\" title=\"Confusion matrix graph\" loading=\"lazy\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=613%2C336&amp;ssl=1\" data-width=\"613\" data-height=\"336\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=1024%2C561&amp;ssl=1 1024w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=300%2C164&amp;ssl=1 300w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?resize=768%2C421&amp;ssl=1 768w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_Confusion-matrix-graph.png?w=1368&amp;ssl=1 1368w\" sizes=\"(max-width: 613px) 100vw, 613px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box\" readability=\"26.973333333333\">\n<p>If we run the above code we will get the below kind of graph, the below graph is the confusion matrix created for the <a href=\"https:\/\/dataaspirant.com\/build-email-spam-classification-model-spacy-python\/\" target=\"_blank\" class=\"tve-froala\" rel=\"noopener noreferrer\">email spam classification model<\/a>.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173afb58644\">\n<span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/07\/confusion_matrix_test.png?resize=559%2C466&amp;ssl=1\" class=\"tve_image wp-image-4055\" alt=\"Confusion matrix on test dataset\" data-id=\"4055\" width=\"559\" data-init-width=\"559\" height=\"466\" data-init-height=\"466\" title=\"confusion_matrix_test\" loading=\"lazy\" data-width=\"559\" data-height=\"466\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/07\/confusion_matrix_test.png?w=559&amp;ssl=1 559w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/07\/confusion_matrix_test.png?resize=300%2C250&amp;ssl=1 300w\" sizes=\"(max-width: 559px) 100vw, 559px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4055\" alt=\"Confusion matrix on test dataset\" data-id=\"4055\" width=\"559\" data-init-width=\"559\" height=\"466\" data-init-height=\"466\" title=\"confusion_matrix_test\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/07\/confusion_matrix_test.png?resize=559%2C466&amp;ssl=1\" data-width=\"559\" data-height=\"466\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/07\/confusion_matrix_test.png?w=559&amp;ssl=1 559w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/07\/confusion_matrix_test.png?resize=300%2C250&amp;ssl=1 300w\" sizes=\"(max-width: 559px) 100vw, 559px\" data-recalc-dims=\"1\"><\/noscript><\/span><\/p>\n<p class=\"thrv-inline-text wp-caption-text\">Confusion matrix<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"43.607142857143\">\n<p>By now we know the different components of the confusion matrix, using these components we can derive multiple model performance measures to quantify the performance of the trained model. We will cover that in another article.<\/p>\n<p>Below are the different measures we can calculate using the confusion matrix.<\/p>\n<ul class=\"\">\n<li>Accuracy\u00a0<\/li>\n<li>Misclassification rate<\/li>\n<li>True positive rate or Recall or Sensitivity<\/li>\n<li>False positive rate<\/li>\n<li>True negative rate or Specificity<\/li>\n<li>Precision<\/li>\n<li>F 1 score<\/li>\n<\/ul>\n<p>We will learn about these measures in the <strong>upcoming<\/strong> article.<\/p>\n<h4 class=\"\" id=\"t-1596364245979\">Complete Code<\/h4>\n<p>Below is the code for implementing confusion matrix in sklearn and tensorflow along with visuvalization code.<\/p>\n<p>You can also clone this code in our <a href=\"https:\/\/github.com\/saimadhu-polamuri\/DataAspirant_codes\/blob\/master\/confusion_matrix\/\" target=\"_blank\" rel=\"noopener noreferrer\">Github<\/a>.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\" readability=\"36\">\n<h2 class=\"\" id=\"t-1596381291687\">Conclusion<\/h2>\n<p>In this article we learned what is the need for confusion matrix, \u00a0different components of the confusion matrix, how to implement them with sklearn and TensorFlow and we also have seen the code to visualize it.<\/p>\n<h4 class=\"\">Recommended Courses<\/h4>\n<\/div>\n<div class=\"thrv_wrapper thrv-page-section thrv-lp-block\" data-inherit-lp-settings=\"1\" data-css=\"tve-u-173b24f56fb\" tcb-template-name=\"Product Highlight 03\" tcb-template-id=\"60907\" data-keep-css_id=\"1\">\n<div class=\"tve-page-section-in tve_empty_dropzone  \" data-css=\"tve-u-173b24f59e1\">\n<div class=\"thrv_wrapper thrv-columns dynamic-group-kbt3q0q7\" data-css=\"tve-u-173b24f56fd\">\n<div class=\"tcb-flex-row v-2 tcb--cols--3 tcb-medium-no-wrap tcb-mobile-wrap m-edit\" data-css=\"tve-u-173b24f56fe\">\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbt3pyfd\" data-css=\"tve-u-173b24f56ff\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173b24f5700\" readability=\"26.707692307692\">\n<div class=\"tve-cb\" readability=\"6.0307692307692\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173b24f570b\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?resize=172%2C97&amp;ssl=1\" class=\"tve_image wp-image-4302\" alt=\"Machine learning \" data-id=\"4302\" width=\"172\" data-init-width=\"750\" height=\"97\" data-init-height=\"422\" title=\"machine learning 1\" loading=\"lazy\" data-width=\"172\" data-height=\"97\" mt-d=\"-2\" data-css=\"tve-u-173b24f570c\" center-v-d=\"false\" mt-t=\"-65\" mt-m=\"-86\" ml-d=\"0\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?w=750&amp;ssl=1 750w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?resize=300%2C169&amp;ssl=1 300w\" sizes=\"(max-width: 172px) 100vw, 172px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4302\" alt=\"Machine learning \" data-id=\"4302\" width=\"172\" data-init-width=\"750\" height=\"97\" data-init-height=\"422\" title=\"machine learning 1\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?resize=172%2C97&amp;ssl=1\" data-width=\"172\" data-height=\"97\" mt-d=\"-2\" data-css=\"tve-u-173b24f570c\" center-v-d=\"false\" mt-t=\"-65\" mt-m=\"-86\" ml-d=\"0\" srcset=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?w=750&amp;ssl=1 750w, https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/machine-learning-1.jpg?resize=300%2C169&amp;ssl=1 300w\" sizes=\"(max-width: 172px) 100vw, 172px\" data-recalc-dims=\"1\"><\/noscript><br \/>\n<span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 class=\"\" data-css=\"tve-u-173b24f570e\">Machine Learning A to Z Course<\/h4>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbt3pyfd\" data-css=\"tve-u-173b24f56ff\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173b24f571a\" readability=\"25.833333333333\">\n<div class=\"tve-cb\" readability=\"5.8333333333333\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173b24f571b\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/AWS_course.jpg?resize=172%2C97&amp;ssl=1\" class=\"tve_image wp-image-4305\" alt=\"AWS Course\" data-id=\"4305\" width=\"172\" data-init-width=\"800\" height=\"97\" data-init-height=\"400\" title=\"AWS_course\" loading=\"lazy\" data-width=\"172\" data-height=\"97\" mt-d=\"-2\" data-css=\"tve-u-173b24f571c\" center-v-d=\"false\" mt-t=\"-65\" mt-m=\"-86\" ml-d=\"0\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/AWS_course.jpg?zoom=2&amp;resize=172%2C97&amp;ssl=1 344w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/AWS_course.jpg?zoom=3&amp;resize=172%2C97&amp;ssl=1 516w\" sizes=\"(max-width: 172px) 100vw, 172px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4305\" alt=\"AWS Course\" data-id=\"4305\" width=\"172\" data-init-width=\"800\" height=\"97\" data-init-height=\"400\" title=\"AWS_course\" loading=\"lazy\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/AWS_course.jpg?resize=172%2C97&amp;ssl=1\" data-width=\"172\" data-height=\"97\" mt-d=\"-2\" data-css=\"tve-u-173b24f571c\" center-v-d=\"false\" mt-t=\"-65\" mt-m=\"-86\" ml-d=\"0\" srcset=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/AWS_course.jpg?zoom=2&amp;resize=172%2C97&amp;ssl=1 344w, https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/AWS_course.jpg?zoom=3&amp;resize=172%2C97&amp;ssl=1 516w\" sizes=\"(max-width: 172px) 100vw, 172px\" data-recalc-dims=\"1\"><\/noscript><br \/>\n<span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 class=\"\" data-css=\"tve-u-173b24f571f\">AWS Machine Learning Full Guide<\/h4>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbt3pyfd\" data-css=\"tve-u-173b24f56ff\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173b24f572a\" readability=\"25.735849056604\">\n<div class=\"tve-cb\" readability=\"5.811320754717\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173b24f572b\"><span class=\"tve_image_frame\"><img src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/plugins\/lazy-load\/images\/1x1.trans.gif?ssl=1\" data-lazy-src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/datascience-spelization.jpg?resize=172%2C97&amp;ssl=1\" class=\"tve_image wp-image-4307\" alt=\"Data Science Full Specialization\" data-id=\"4307\" width=\"172\" data-init-width=\"175\" height=\"97\" data-init-height=\"175\" title=\"datascience spelization\" loading=\"lazy\" data-width=\"172\" data-height=\"97\" mt-d=\"-2\" data-css=\"tve-u-173b24f572c\" center-v-d=\"false\" mt-t=\"-65\" mt-m=\"-86\" ml-d=\"0\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/datascience-spelization.jpg?zoom=2&amp;resize=172%2C97&amp;ssl=1 344w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/datascience-spelization.jpg?zoom=3&amp;resize=172%2C97&amp;ssl=1 516w\" sizes=\"(max-width: 172px) 100vw, 172px\" data-recalc-dims=\"1\"><noscript><img class=\"tve_image wp-image-4307\" alt=\"Data Science Full Specialization\" data-id=\"4307\" width=\"172\" data-init-width=\"175\" height=\"97\" data-init-height=\"175\" title=\"datascience spelization\" loading=\"lazy\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/datascience-spelization.jpg?resize=172%2C97&amp;ssl=1\" data-width=\"172\" data-height=\"97\" mt-d=\"-2\" data-css=\"tve-u-173b24f572c\" center-v-d=\"false\" mt-t=\"-65\" mt-m=\"-86\" ml-d=\"0\" srcset=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/datascience-spelization.jpg?zoom=2&amp;resize=172%2C97&amp;ssl=1 344w, https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/datascience-spelization.jpg?zoom=3&amp;resize=172%2C97&amp;ssl=1 516w\" sizes=\"(max-width: 172px) 100vw, 172px\" data-recalc-dims=\"1\"><\/noscript><br \/>\n<span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 class=\"\" data-css=\"tve-u-173b24f572e\">Data Science Full Specialization<\/h4>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/dataaspirant.com\/confusion-matrix-sklearn-python\/<\/p>\n","protected":false},"author":1,"featured_media":8,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/7"}],"collection":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/comments?post=7"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/7\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/8"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=7"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=7"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=7"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}