{"id":182,"date":"2020-08-07T08:00:26","date_gmt":"2020-08-07T08:00:26","guid":{"rendered":"https:\/\/data-science.gotoauthority.com\/2020\/08\/07\/six-popular-classification-evaluation-metrics-in-machine-learning\/"},"modified":"2020-08-07T08:00:26","modified_gmt":"2020-08-07T08:00:26","slug":"six-popular-classification-evaluation-metrics-in-machine-learning","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2020\/08\/07\/six-popular-classification-evaluation-metrics-in-machine-learning\/","title":{"rendered":"Six Popular Classification Evaluation Metrics In Machine Learning"},"content":{"rendered":"<div id=\"tve_editor\" data-post-id=\"4326\">\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3d1c781\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/1_Evaluation-metrics.png?resize=613%2C394&amp;ssl=1\" class=\"tve_image wp-image-4327\" alt=\"Evaluation metrics\" data-id=\"4327\" width=\"613\" data-init-width=\"700\" height=\"394\" data-init-height=\"450\" title=\"Evaluation metrics\" data-width=\"613\" data-height=\"394\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4327\" alt=\"Evaluation metrics\" data-id=\"4327\" width=\"613\" data-init-width=\"700\" height=\"394\" data-init-height=\"450\" title=\"Evaluation metrics\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/1_Evaluation-metrics.png?resize=613%2C394&amp;ssl=1\" data-width=\"613\" data-height=\"394\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box fr-basic\">\n<p dir=\"ltr\">Evaluation metrics are the most important topic in machine learning and deep learning model building. These metrics help in determining <strong>how good<\/strong> the model is trained. We are having different evaluation metrics for a different set of machine learning algorithms.<\/p>\n<p dir=\"ltr\">For evaluating classification models we use classification evaluation metrics, whereas for regression kind of models we use the regression evaluation metrics.<\/p>\n<p dir=\"ltr\">There are a number of model evaluation metrics that are available for both <a href=\"https:\/\/dataaspirant.com\/supervised-and-unsupervised-learning\/\" class=\"tve-froala\">supervised and unsupervised learning<\/a> techniques.<\/p>\n<p dir=\"ltr\">In this article, we will focus on what kind of model evaluation metrics are used in \u00a0quantifying the performance of the models built with supervised learning algorithms. <\/p>\n<p dir=\"ltr\">In particular, we learn about different <strong>classification evaluation<\/strong> metrics.<\/p>\n<p dir=\"ltr\">Before we go further let\u2019s list down the topics we are going to learn in this article.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box fr-basic\">\n<h2 class=\"\" id=\"t-1596718010199\">Supervised and Unsupervised Learning Algorithms<\/h2>\n<h3 class=\"\" id=\"t-1596718010200\">Supervised Learning Algorithms<\/h3>\n<p>Supervised Learning algorithms are only applicable to the dataset which has <strong>features as well as labels<\/strong>. For the given combination of feature, the expected target is called as label.<\/p>\n<p>For example, If we know the shape and color of the fruit. We can strongly say what fruit it could be. Here <strong>shape and color<\/strong> are the features and fruit type is target (label).<\/p>\n<p>Supervised learning algorithms further classified as <strong>two different<\/strong> categories.<\/p>\n<ul>\n<li class=\"\">Classification Algorithms<\/li>\n<li class=\"\">Regression Algorithms<\/li>\n<\/ul>\n<h4 class=\"\" id=\"t-1596718010201\">Classification Algorithms<\/h4>\n<p>If the target value is categorical values like input image have a chair (label 1) or not having a chair (label 0) then we apply the techniques of classification algorithms. This means we will classify the features to any one of the available classes or labels.<\/p>\n<p dir=\"ltr\">Below are the most popular classification algorithms<\/p>\n<h4 id=\"t-1596718010202\" class=\"\">Regression Algorithms<\/h4>\n<p>If the target values are <strong>real values<\/strong> like the price of the house or the approved loan amount. Then we apply the technique of Regression algorithms. Using regression techniques we can predict the real value, rather than target class.<\/p>\n<p>Below are the most popular regression algorithms.<\/p>\n<h3 id=\"t-1596718010203\" class=\"\">Unsupervised Learning<\/h3>\n<p>Unsupervised learning techniques are applicable for dataset which <strong>don&#8217;t<\/strong> have any target values for features.<\/p>\n<p>In this article we learn only the popular evaluation metrics which can be used for quantifying the classification algorithms. In short we will learn classification metrics for evaluating the performance of the classification models.<\/p>\n<p>Before that let\u2019s learn why we \u00a0need evaluation metrics.<\/p>\n<h2 id=\"t-1596718010204\" class=\"\">Why we need evaluation metrics<\/h2>\n<p>For every machine learning or deep learning model. We need to know how good the model learnt from the training data. Also we need to know how good the same model will predict future or unseen data.\u00a0<\/p>\n<p>For this we need a way to measure the model performance. \u00a0In machine learing these performance measure are nothing but evaluation metrics.<\/p>\n<p>We are having some bunch of evaluation metrics, but we need to know which metrics need to use for what kind of problems.\u00a0<\/p>\n<p>Let\u2019s say for classification models, we use the classification metrics. For regression kind of models, we use the regression metrics. <\/p>\n<p>If we don\u2019t know which evaluation metrics to use, then we will compare the oranges to apples kind of comparison. Which is not at all good.<\/p>\n<p>In the scope of this article. We will learn different evaluation metrics used in testing performance of <a href=\"https:\/\/dataaspirant.com\/classification-clustering-alogrithms\/\" target=\"_blank\" rel=\"noopener noreferrer\">classification algorithms<\/a>.<\/p>\n<p>Let\u2019s get started then.<\/p>\n<h2 class=\"\" id=\"t-1596718010205\">Classification Evaluation metrics<\/h2>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3da2991\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/2_6_classification_evaluation_metrics.png?resize=626%2C448&amp;ssl=1\" class=\"tve_image wp-image-4341\" alt=\"6 classification evaluation metrics\" data-id=\"4341\" width=\"626\" data-init-width=\"1024\" height=\"448\" data-init-height=\"733\" title=\"6 classification evaluation metrics\" data-width=\"626\" data-height=\"448\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4341\" alt=\"6 classification evaluation metrics\" data-id=\"4341\" width=\"626\" data-init-width=\"1024\" height=\"448\" data-init-height=\"733\" title=\"6 classification evaluation metrics\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/2_6_classification_evaluation_metrics.png?resize=626%2C448&amp;ssl=1\" data-width=\"626\" data-height=\"448\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">Here we will discuss <strong>6 different<\/strong> types of classification evaluation metrics.<\/p>\n<ol class=\"\">\n<li>Accuracy<\/li>\n<li>Confusion matrix<\/li>\n<li>Precision<\/li>\n<li>Recall<\/li>\n<li>F1 Score<\/li>\n<li>Log Loss<\/li>\n<\/ol>\n<h3 id=\"t-1596718010206\" class=\"\">Accuracy<\/h3>\n<p>Let me take one example dataset that has binary classes, means target values are only 2 classes or labels like 0 and 1. We consider the dataset as a sentiment analysis dataset. where we have 2 labels like positive (label 1) and negative (label 0).\u00a0<\/p>\n<p>Here our task is to build a model that will predict whether a given input sentence is positive (label 1) or negative (label 0) sentiment.<\/p>\n<p>For example. Our dataset has 200 sentences. The target class 0 has 100 sentences and class 1 has 100 sentences. This means each target class has equal number of sentences.<\/p>\n<p>The first step is to divide the whole dataset into 2 parts as a training dataset \u00a0and test dataset.\u00a0<\/p>\n<ul>\n<li class=\"\">Training Dataset: Useful for model training.<\/li>\n<li class=\"\">Test Dataset: Useful for validating the trained model.<\/li>\n<\/ul>\n<p>Now each dataset has an equal number of sentences and as well as an equal number of labeled sentiments mean 50 sentences have class 0 and 50 sentences have class 1.<\/p>\n<p>When we have an equal number of samples for both classes or all classes then we can use Accuracy, Precision, Recall, and F1 Score as evaluation metrics.<\/p>\n<h4 class=\"\" id=\"t-1596718010207\">Accuracy measure python implementation<\/h4>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3dcd640\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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_Accuracy.png?resize=626%2C467&amp;ssl=1\" class=\"tve_image wp-image-4345\" alt=\"Accuracy Calculation With Python\" data-id=\"4345\" width=\"626\" data-init-width=\"1024\" height=\"467\" data-init-height=\"764\" title=\"Accuracy Calculation With Python\" data-width=\"626\" data-height=\"467\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4345\" alt=\"Accuracy Calculation With Python\" data-id=\"4345\" width=\"626\" data-init-width=\"1024\" height=\"467\" data-init-height=\"764\" title=\"Accuracy Calculation With Python\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/3_Accuracy.png?resize=626%2C467&amp;ssl=1\" data-width=\"626\" data-height=\"467\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Output<\/strong><\/p>\n<p><strong><\/strong>Below is the output for the above code.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3de9115\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/4_Accuracy_calculation.png?resize=626%2C32&amp;ssl=1\" class=\"tve_image wp-image-4349\" alt=\"Accuracy calculation output\" data-id=\"4349\" width=\"626\" data-init-width=\"2852\" height=\"32\" data-init-height=\"144\" title=\"Accuracy calculation\" data-width=\"626\" data-height=\"32\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4349\" alt=\"Accuracy calculation output\" data-id=\"4349\" width=\"626\" data-init-width=\"2852\" height=\"32\" data-init-height=\"144\" title=\"Accuracy calculation\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/4_Accuracy_calculation.png?resize=626%2C32&amp;ssl=1\" data-width=\"626\" data-height=\"32\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h4 class=\"\" id=\"t-1596718010208\">Accuracy from scikit-learn library<\/h4>\n<p dir=\"ltr\">We can also calculate the accuracy measure with scikit-learn. Using the below code we can achieve the same result.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3e18c5d\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/5_accuracy_code_with_scikitlearn.png?resize=626%2C247&amp;ssl=1\" class=\"tve_image wp-image-4352\" alt=\"Accuracy code with scikitlearn\" data-id=\"4352\" width=\"626\" data-init-width=\"1024\" height=\"247\" data-init-height=\"404\" title=\"Accuracy code with scikitlearn\" data-width=\"626\" data-height=\"247\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4352\" alt=\"Accuracy code with scikitlearn\" data-id=\"4352\" width=\"626\" data-init-width=\"1024\" height=\"247\" data-init-height=\"404\" title=\"Accuracy code with scikitlearn\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/5_accuracy_code_with_scikitlearn.png?resize=626%2C247&amp;ssl=1\" data-width=\"626\" data-height=\"247\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3e2f2ce\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/6_scikit-learn-accuracy.png?resize=626%2C37&amp;ssl=1\" class=\"tve_image wp-image-4354\" alt=\"Scikit learn accuracy output\" data-id=\"4354\" width=\"626\" data-init-width=\"1024\" height=\"37\" data-init-height=\"61\" title=\"Scikit learn accuracy output\" data-width=\"626\" data-height=\"37\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4354\" alt=\"Scikit learn accuracy output\" data-id=\"4354\" width=\"626\" data-init-width=\"1024\" height=\"37\" data-init-height=\"61\" title=\"Scikit learn accuracy output\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/6_scikit-learn-accuracy.png?resize=626%2C37&amp;ssl=1\" data-width=\"626\" data-height=\"37\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 id=\"t-1596718010209\" class=\"\">Confusion matrix<\/h3>\n<p>Confusion matrix gives the matrix representation, to give a clear picture about how well the individual target classes are predicted by the model.<\/p>\n<p>It\u2019s quite normal that we always forget about the individual components of the confusion matrix. We refresh the confusion matrix concept once again with the below image.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3e514f0\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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=\"confusion matrix\" data-width=\"626\" data-height=\"369\" data-recalc-dims=\"1\"><img loading=\"lazy\" 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=\"confusion matrix\" 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\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala fr-box fr-basic\">\n<p dir=\"ltr\">I would suggest to spend time on understanding the confusion matrix, as all the other classification metrics are using the individual <a href=\"https:\/\/dataaspirant.com\/confusion-matrix-sklearn-python\/\" class=\"tve-froala\">components of the confusion matrix<\/a>.<\/p>\n<h4 class=\"\" id=\"t-1596718010210\">Confusion matrix code with Sklearn<\/h4>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3ef661a\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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=626%2C330&amp;ssl=1\" class=\"tve_image wp-image-4244\" alt=\"Confusion matrix sklearn\" data-id=\"4244\" width=\"626\" data-init-width=\"1524\" height=\"330\" data-init-height=\"804\" title=\"7_confusion_matrix_sklearn\" data-width=\"626\" data-height=\"330\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4244\" alt=\"Confusion matrix sklearn\" data-id=\"4244\" width=\"626\" data-init-width=\"1524\" height=\"330\" data-init-height=\"804\" title=\"7_confusion_matrix_sklearn\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_sklearn.png?resize=626%2C330&amp;ssl=1\" data-width=\"626\" data-height=\"330\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h4 id=\"t-1596718010211\" class=\"\">Output<\/h4>\n<p dir=\"ltr\">Below is the output for the above confusion matrix code.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3f1ba8d\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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=\"1924\" height=\"61\" data-init-height=\"188\" title=\"10_Confusion-matrix-output\" data-width=\"626\" data-height=\"61\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4291\" alt=\"Confusion matrix output\" data-id=\"4291\" width=\"626\" data-init-width=\"1924\" height=\"61\" data-init-height=\"188\" title=\"10_Confusion-matrix-output\" 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\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 id=\"t-1596718010212\" class=\"\">Precision<\/h3>\n<p>Precision evaluation metric is useful for the skewed or unbalanced datasets. Skewed dataset means one class has very fewer samples than another class.\u00a0<\/p>\n<p>For example in sentiment analysis the dataset has 200 sentences. Out of 200 let say\u00a0 20 samples belong to label 0, and the remaining 180 samples are label 1.<\/p>\n<p>In this case, accuracy is not performed well as an evaluation metric. We can get high accuracy but the model will not be performed well on unseen data. Before going to discuss more about precision metrics we have to know a few terms here. Just remember in our dataset positive class has label 1 and negative class has label 0.<\/p>\n<p>The below terms are considered from the confusion matrix.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<ul class=\"\">\n<li>\n<strong>True Positive (TP)<\/strong> :- \u00a0The number of positive samples correctly predicted \u00a0by the trained model as positive class. This means if the actual target value is 1 model also predicted as 1.<\/li>\n<li>\n<strong>True Negative (TN)<\/strong> :- The number of negative samples correctly predicted as negative class by the trained model. This means if the actual target value is 0, the model also predicted as 0.<\/li>\n<li>\n<strong>False Positive (FP)<\/strong> :- The number of negative target classes are incorrectly predicted as positive by the trained model.This means if the actual target value is 0 model is incorrectly predicted as 1.<\/li>\n<li>\n<strong>False Negative (FN)<\/strong> :- The number of positive target classes are incorrectly predicted as negative by the trained model.This means if the actual target value is 1, the model incorrectly predicted as 0.<\/li>\n<\/ul>\n<p dir=\"ltr\">Let&#8217;s see python implementations of all these terms.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3e68e5b\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/7_confusion_matrix_individual_components.png?resize=486%2C1166&amp;ssl=1\" class=\"tve_image wp-image-4359\" alt=\"Confusion matrix individual components with python\" data-id=\"4359\" width=\"486\" data-init-width=\"1348\" height=\"1166\" data-init-height=\"3234\" title=\"Confusion matrix individual components\" data-width=\"486\" data-height=\"1166\" data-css=\"tve-u-173c3e6b9c5\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4359\" alt=\"Confusion matrix individual components with python\" data-id=\"4359\" width=\"486\" data-init-width=\"1348\" height=\"1166\" data-init-height=\"3234\" title=\"Confusion matrix individual components\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/7_confusion_matrix_individual_components.png?resize=486%2C1166&amp;ssl=1\" data-width=\"486\" data-height=\"1166\" data-css=\"tve-u-173c3e6b9c5\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element tve-froala\">\n<p dir=\"ltr\">Now Let&#8217;s call all the above terms in single funciton.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3e8f5db\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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_calculate-confusion-matrix-components.png?resize=626%2C752&amp;ssl=1\" class=\"tve_image wp-image-4363\" alt=\"calculate confusion matrix components\" data-id=\"4363\" width=\"626\" data-init-width=\"1524\" height=\"752\" data-init-height=\"1830\" title=\"calculate confusion matrix components\" data-width=\"626\" data-height=\"752\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4363\" alt=\"calculate confusion matrix components\" data-id=\"4363\" width=\"626\" data-init-width=\"1524\" height=\"752\" data-init-height=\"1830\" title=\"calculate confusion matrix components\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/8_calculate-confusion-matrix-components.png?resize=626%2C752&amp;ssl=1\" data-width=\"626\" data-height=\"752\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\"><strong>Output<\/strong><\/p>\n<p dir=\"ltr\">Below is the output for the above code.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3ea8dd2\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/9_confusion_matrix_output.png?resize=626%2C66&amp;ssl=1\" class=\"tve_image wp-image-4365\" alt=\"confusion matrix output\" data-id=\"4365\" width=\"626\" data-init-width=\"2866\" height=\"66\" data-init-height=\"304\" title=\"confusion matrix output\" data-width=\"626\" data-height=\"66\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4365\" alt=\"confusion matrix output\" data-id=\"4365\" width=\"626\" data-init-width=\"2866\" height=\"66\" data-init-height=\"304\" title=\"confusion matrix output\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/9_confusion_matrix_output.png?resize=626%2C66&amp;ssl=1\" data-width=\"626\" data-height=\"66\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Using the individual components of confusion matrix we can calculate accuracy measures also. Below is the formula for that.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3f44e44\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/10_accuracy_formula.png?resize=626%2C432&amp;ssl=1\" class=\"tve_image wp-image-4377\" alt=\"accuracy formula with confusion matrixs\" data-id=\"4377\" width=\"626\" data-init-width=\"1024\" height=\"432\" data-init-height=\"707\" title=\"accuracy formula with confusion matrixs\" data-width=\"626\" data-height=\"432\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4377\" alt=\"accuracy formula with confusion matrixs\" data-id=\"4377\" width=\"626\" data-init-width=\"1024\" height=\"432\" data-init-height=\"707\" title=\"accuracy formula with confusion matrixs\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/10_accuracy_formula.png?resize=626%2C432&amp;ssl=1\" data-width=\"626\" data-height=\"432\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Accuracy Score<\/strong> = (TP+TN)\/(TP+TN+FP+FN)<\/p>\n<p>Now we will be creating a function which calcautates each these confusion matrix components and calculates the accuracy.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3f836cd\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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_accuracy_score_with_confusion_matrix.png?resize=626%2C608&amp;ssl=1\" class=\"tve_image wp-image-4385\" alt=\"Accuracy score with confusion matrix\" data-id=\"4385\" width=\"626\" data-init-width=\"1024\" height=\"608\" data-init-height=\"994\" title=\"Accuracy score with confusion matrix\" data-width=\"626\" data-height=\"608\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4385\" alt=\"Accuracy score with confusion matrix\" data-id=\"4385\" width=\"626\" data-init-width=\"1024\" height=\"608\" data-init-height=\"994\" title=\"Accuracy score with confusion matrix\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/11_accuracy_score_with_confusion_matrix.png?resize=626%2C608&amp;ssl=1\" data-width=\"626\" data-height=\"608\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\"><strong>Output<\/strong><\/p>\n<p dir=\"ltr\">Output of the above code, this will be similar to the accuracy score calculated before.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3f8fdaf\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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_accuracy_score_output.png?resize=626%2C43&amp;ssl=1\" class=\"tve_image wp-image-4387\" alt=\"accuracy score output\" data-id=\"4387\" width=\"626\" data-init-width=\"2858\" height=\"43\" data-init-height=\"198\" title=\"accuracy score output\" data-width=\"626\" data-height=\"43\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4387\" alt=\"accuracy score output\" data-id=\"4387\" width=\"626\" data-init-width=\"2858\" height=\"43\" data-init-height=\"198\" title=\"accuracy score output\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/12_accuracy_score_output.png?resize=626%2C43&amp;ssl=1\" data-width=\"626\" data-height=\"43\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Now let go back to precision. For calcuating precision we will be using the below formula.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3fa455e\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/13_precision_formula.png?resize=626%2C439&amp;ssl=1\" class=\"tve_image wp-image-4390\" alt=\"Precision formula\" data-id=\"4390\" width=\"626\" data-init-width=\"1024\" height=\"439\" data-init-height=\"718\" title=\"Precision formula\" data-width=\"626\" data-height=\"439\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4390\" alt=\"Precision formula\" data-id=\"4390\" width=\"626\" data-init-width=\"1024\" height=\"439\" data-init-height=\"718\" title=\"Precision formula\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/13_precision_formula.png?resize=626%2C439&amp;ssl=1\" data-width=\"626\" data-height=\"439\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Precision<\/strong> = TP \/ (TP+FP)<\/p>\n<p>Let me explain about precision score with an example.<\/p>\n<p>Remember our dataset has 80 samples belonging to label 0 and 20 samples belong to label 1. From these samples, if our trained model correctly predicted 60 samples as label negative out of 80 samples and also correctly predictly 10 samples as label as positive out of 20 samples.<\/p>\n<p>If we calculate accuracy for this model , we will get an accuracy score as 0.7 (70% of accuracy).<\/p>\n<p>But the model misclassified as negative class 10 out of 20 positive classes and as positive class 20 out of 80 negative samples.<\/p>\n<p dir=\"ltr\">From these samples :-<\/p>\n<ul class=\"\">\n<li>TP = 10<\/li>\n<li>FP = 20<\/li>\n<li>TN = 60<\/li>\n<li>FN = 10<\/li>\n<\/ul>\n<p><strong>Precision value<\/strong> \u00a0= 10\/(10+20) = 0.33<\/p>\n<p>This means the model is predicting correct outputs only \u00a033% times when it&#8217;s trying to identify positive samples .<\/p>\n<h4 class=\"\" id=\"t-1596718010213\">Precision python code implementation<\/h4>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3fd812a\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/14_precision.png?resize=626%2C597&amp;ssl=1\" class=\"tve_image wp-image-4397\" alt=\"Precision implementation in python\" data-id=\"4397\" width=\"626\" data-init-width=\"1024\" height=\"597\" data-init-height=\"976\" title=\"Precision implementation in python\" data-width=\"626\" data-height=\"597\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4397\" alt=\"Precision implementation in python\" data-id=\"4397\" width=\"626\" data-init-width=\"1024\" height=\"597\" data-init-height=\"976\" title=\"Precision implementation in python\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/14_precision.png?resize=626%2C597&amp;ssl=1\" data-width=\"626\" data-height=\"597\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\"><strong>Output<\/strong><\/p>\n<p dir=\"ltr\">Below is the calculated precision score calculated from using the above code.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c3fec72e\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/15_precision_output.png?resize=626%2C39&amp;ssl=1\" class=\"tve_image wp-image-4400\" alt=\"Precision Output\" data-id=\"4400\" width=\"626\" data-init-width=\"1024\" height=\"39\" data-init-height=\"63\" title=\"Precision Output\" data-width=\"626\" data-height=\"39\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4400\" alt=\"Precision Output\" data-id=\"4400\" width=\"626\" data-init-width=\"1024\" height=\"39\" data-init-height=\"63\" title=\"Precision Output\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/15_precision_output.png?resize=626%2C39&amp;ssl=1\" data-width=\"626\" data-height=\"39\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h3 id=\"t-1596718010214\" class=\"\">Recall<\/h3>\n<p>Recall is one of the evaluation metrics , by using this metric we can know the number of correct positive classes out of all positive classes. Along with this information recall also provide information \u00a0about missclassified positive classes.<\/p>\n<p>Recall evaluation metric can be defined as below.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c400f473\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/16_recall_formula.png?resize=626%2C434&amp;ssl=1\" class=\"tve_image wp-image-4404\" alt=\"Recall formula\" data-id=\"4404\" width=\"626\" data-init-width=\"1114\" height=\"434\" data-init-height=\"772\" title=\"Recall formula\" data-width=\"626\" data-height=\"434\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4404\" alt=\"Recall formula\" data-id=\"4404\" width=\"626\" data-init-width=\"1114\" height=\"434\" data-init-height=\"772\" title=\"Recall formula\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/16_recall_formula.png?resize=626%2C434&amp;ssl=1\" data-width=\"626\" data-height=\"434\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Recall<\/strong> = TP\/(TP+FN)<\/p>\n<p>From the above precision example , we can calculate recall metric value also, for these values of<\/p>\n<p>Recall = 10\/(10+10)\u00a0<\/p>\n<p>recall score is 0.50 this means our model will identify 50% of positive samples correctly. Now we will write a function to calcuate the recall value.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c40416ee\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/17_recall_code.png?resize=626%2C594&amp;ssl=1\" class=\"tve_image wp-image-4412\" alt=\"Recall implementation in python\" data-id=\"4412\" width=\"626\" data-init-width=\"1188\" height=\"594\" data-init-height=\"1128\" title=\"Recall implementation in python\" data-width=\"626\" data-height=\"594\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4412\" alt=\"Recall implementation in python\" data-id=\"4412\" width=\"626\" data-init-width=\"1188\" height=\"594\" data-init-height=\"1128\" title=\"Recall implementation in python\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/17_recall_code.png?resize=626%2C594&amp;ssl=1\" data-width=\"626\" data-height=\"594\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Output<\/strong><\/p>\n<p dir=\"ltr\">The calculated recall score, using the above code.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c40335b5\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/18_recall_output.png?resize=626%2C41&amp;ssl=1\" class=\"tve_image wp-image-4410\" alt=\"Recall python script output\" data-id=\"4410\" width=\"626\" data-init-width=\"2868\" height=\"41\" data-init-height=\"188\" title=\"Recall python script output\" data-width=\"626\" data-height=\"41\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4410\" alt=\"Recall python script output\" data-id=\"4410\" width=\"626\" data-init-width=\"2868\" height=\"41\" data-init-height=\"188\" title=\"Recall python script output\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/18_recall_output.png?resize=626%2C41&amp;ssl=1\" data-width=\"626\" data-height=\"41\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Well trained machine learning models will have high recall and precision scores.<\/p>\n<p>In real time problems. We have to take some threshold value and repredict the predicted values for precision and recall metrics based on threshold value. We can plot graphs by using these precision and recall values. This gives us a graph curve which is called a <strong>precision-recall<\/strong> curve.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c405f56a\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/19_precision_recall_curves.png?resize=626%2C574&amp;ssl=1\" class=\"tve_image wp-image-4416\" alt=\"Precision Recall Curves\" data-id=\"4416\" width=\"626\" data-init-width=\"1524\" height=\"574\" data-init-height=\"1398\" title=\"Precision Recall Curves\" data-width=\"626\" data-height=\"574\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4416\" alt=\"Precision Recall Curves\" data-id=\"4416\" width=\"626\" data-init-width=\"1524\" height=\"574\" data-init-height=\"1398\" title=\"Precision Recall Curves\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/19_precision_recall_curves.png?resize=626%2C574&amp;ssl=1\" data-width=\"626\" data-height=\"574\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Will calculate the usage threshold by calling the above function with input values.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c407a2a9\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/20_precision_recall_graph_code.png?resize=626%2C574&amp;ssl=1\" class=\"tve_image wp-image-4418\" alt=\"Precision Recall Graph Code\" data-id=\"4418\" width=\"626\" data-init-width=\"1024\" height=\"574\" data-init-height=\"939\" title=\"Precision Recall Graph Code\" data-width=\"626\" data-height=\"574\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4418\" alt=\"Precision Recall Graph Code\" data-id=\"4418\" width=\"626\" data-init-width=\"1024\" height=\"574\" data-init-height=\"939\" title=\"Precision Recall Graph Code\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/20_precision_recall_graph_code.png?resize=626%2C574&amp;ssl=1\" data-width=\"626\" data-height=\"574\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\"><strong>Output :<\/strong><\/p>\n<p>We can find the <strong>best threshold<\/strong> value for precision and recall from the precision-recall curve.\u00a0<\/p>\n<p>A high area under the curve represents both high recall and high precision, where high precision relates to a low false positive rate, and high recall relates to a low false negative rate.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c40900f8\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/21_precision_recall_graph.png?resize=626%2C307&amp;ssl=1\" class=\"tve_image wp-image-4421\" alt=\"Precision Recall Graph\" data-id=\"4421\" width=\"626\" data-init-width=\"1520\" height=\"307\" data-init-height=\"746\" title=\"Precision Recall Graph\" data-width=\"626\" data-height=\"307\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4421\" alt=\"Precision Recall Graph\" data-id=\"4421\" width=\"626\" data-init-width=\"1520\" height=\"307\" data-init-height=\"746\" title=\"Precision Recall Graph\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/21_precision_recall_graph.png?resize=626%2C307&amp;ssl=1\" data-width=\"626\" data-height=\"307\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>In real-time scenario this is difficult to choose threshold value to get \u00a0good values for both Precision and Recall scores. If we take high threshold value, we will get a smaller number of true positives and higher number of false negatives.This will decrease our recall values, but increase precision value. If we take low threshold value false positives are high then precision value will be less.<\/p>\n<p>Both Precision and Recall values range between 0 to 1. If these values are close to 1 this means model will be a good predicted model. Due to this threshold value selection we can use another evaluation metric which combines both these metrics (Precision and Recall).<\/p>\n<h3 id=\"t-1596718010215\" class=\"\">F1 score<\/h3>\n<p>F1 score is the combination of both precision and recall score. We can define F1-score as simple weighted average of precision and recall.vIf we denote R for Recall and P for Precision we can defined F1-score as below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c40b7750\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/22_f1_score_formula_1.png?resize=626%2C426&amp;ssl=1\" class=\"tve_image wp-image-4426\" alt=\"f1 score formula\" data-id=\"4426\" width=\"626\" data-init-width=\"1024\" height=\"426\" data-init-height=\"697\" title=\"f1 score formula\" data-width=\"626\" data-height=\"426\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4426\" alt=\"f1 score formula\" data-id=\"4426\" width=\"626\" data-init-width=\"1024\" height=\"426\" data-init-height=\"697\" title=\"f1 score formula\" src=\"https:\/\/i0.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/22_f1_score_formula_1.png?resize=626%2C426&amp;ssl=1\" data-width=\"626\" data-height=\"426\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>F1-score<\/strong> = 2PR \/ (P+R)<\/p>\n<p dir=\"ltr\">We can also define F1-score using True,Positive terms then<\/p>\n<p>F1-score = 2TP \/ (2TP+FP+FN)<\/p>\n<h4 class=\"\" id=\"t-1596718010216\">Python code for F1-score metric<\/h4>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c40d375c\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/23_f1_score_code.png?resize=626%2C594&amp;ssl=1\" class=\"tve_image wp-image-4429\" alt=\"f1 score code\" data-id=\"4429\" width=\"626\" data-init-width=\"1024\" height=\"594\" data-init-height=\"972\" title=\"f1 score code\" data-width=\"626\" data-height=\"594\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4429\" alt=\"f1 score code\" data-id=\"4429\" width=\"626\" data-init-width=\"1024\" height=\"594\" data-init-height=\"972\" title=\"f1 score code\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/23_f1_score_code.png?resize=626%2C594&amp;ssl=1\" data-width=\"626\" data-height=\"594\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\"><strong>Output<\/strong><\/p>\n<p dir=\"ltr\">Below is the output for the calculated F1 score<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c40e132c\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/24_f1_score_output.png?resize=626%2C35&amp;ssl=1\" class=\"tve_image wp-image-4431\" alt=\"F1 score output\" data-id=\"4431\" width=\"626\" data-init-width=\"1024\" height=\"35\" data-init-height=\"58\" title=\"F1 score output\" data-width=\"626\" data-height=\"35\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4431\" alt=\"F1 score output\" data-id=\"4431\" width=\"626\" data-init-width=\"1024\" height=\"35\" data-init-height=\"58\" title=\"F1 score output\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/24_f1_score_output.png?resize=626%2C35&amp;ssl=1\" data-width=\"626\" data-height=\"35\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p>Instead of looking at precision and recall individually. You can also just look at F1 \u00a0score. Same as for precision, recall and accuracy, F1 score also ranges from 0 to 1. \u00a0The perfect prediction model has a value of F1 score is equal to 1. When dealing with datasets that have \u00a0skewed targets, we should look at F1 (or precision and recall) instead of accuracy.\u00a0<\/p>\n<h3 class=\"\" id=\"t-1596718010217\">Log-Loss<\/h3>\n<p>This is the last evaluation metric in this article for machine learning classification problems. We can defined log-loss metric for binary classification problem as below<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c411bb0f\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/25_log_loss_formula.png?resize=626%2C425&amp;ssl=1\" class=\"tve_image wp-image-4438\" alt=\"Log loss formula\" data-id=\"4438\" width=\"626\" data-init-width=\"1024\" height=\"425\" data-init-height=\"695\" title=\"Log loss formula\" data-width=\"626\" data-height=\"425\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4438\" alt=\"Log loss formula\" data-id=\"4438\" width=\"626\" data-init-width=\"1024\" height=\"425\" data-init-height=\"695\" title=\"Log loss formula\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/25_log_loss_formula.png?resize=626%2C425&amp;ssl=1\" data-width=\"626\" data-height=\"425\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p><strong>Log loss<\/strong> = -1.0 * \u00a0( y_true * log(y_pred) + (1-y_true) * log(1- y_pred) )<\/p>\n<p>Here y_pred are probabilities of corresponding samples.<\/p>\n<h4 class=\"\" id=\"t-1596718010218\">Implementation of Log loss with python code<\/h4>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c4133739\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/26_log_loss_code.png?resize=626%2C818&amp;ssl=1\" class=\"tve_image wp-image-4440\" alt=\"log loss python code implementation\" data-id=\"4440\" width=\"626\" data-init-width=\"1524\" height=\"818\" data-init-height=\"1992\" title=\"log loss python code implementation\" data-width=\"626\" data-height=\"818\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4440\" alt=\"log loss python code implementation\" data-id=\"4440\" width=\"626\" data-init-width=\"1524\" height=\"818\" data-init-height=\"1992\" title=\"log loss python code implementation\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/26_log_loss_code.png?resize=626%2C818&amp;ssl=1\" data-width=\"626\" data-height=\"818\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">Output of the log loss after calling the log loss metrics function.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c4144c2f\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/27_log_loss_output.png?resize=626%2C45&amp;ssl=1\" class=\"tve_image wp-image-4441\" alt=\"log loss output\" data-id=\"4441\" width=\"626\" data-init-width=\"1024\" height=\"45\" data-init-height=\"74\" title=\"log loss output\" data-width=\"626\" data-height=\"45\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4441\" alt=\"log loss output\" data-id=\"4441\" width=\"626\" data-init-width=\"1024\" height=\"45\" data-init-height=\"74\" title=\"log loss output\" src=\"https:\/\/i2.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/27_log_loss_output.png?resize=626%2C45&amp;ssl=1\" data-width=\"626\" data-height=\"45\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">We can also implement a log loss evaluation metric by using sklearn.<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c4155375\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/28_logloss_with_scikitlearn.png?resize=626%2C362&amp;ssl=1\" class=\"tve_image wp-image-4443\" alt=\"Logloss with scikit learn\" data-id=\"4443\" width=\"626\" data-init-width=\"1296\" height=\"362\" data-init-height=\"750\" title=\"Logloss with scikit learn\" data-width=\"626\" data-height=\"362\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4443\" alt=\"Logloss with scikit learn\" data-id=\"4443\" width=\"626\" data-init-width=\"1296\" height=\"362\" data-init-height=\"750\" title=\"Logloss with scikit learn\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/28_logloss_with_scikitlearn.png?resize=626%2C362&amp;ssl=1\" data-width=\"626\" data-height=\"362\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\"><strong>Output<\/strong><\/p>\n<p dir=\"ltr\">Below is the output for log loss metric with scikit learn<\/p>\n<\/div>\n<div class=\"thrv_wrapper tve_image_caption\" data-css=\"tve-u-173c4161739\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\/29_logloss_with_scikilearn_output.png?resize=626%2C45&amp;ssl=1\" class=\"tve_image wp-image-4445\" alt=\"logloss with scikilearn output\" data-id=\"4445\" width=\"626\" data-init-width=\"2864\" height=\"45\" data-init-height=\"208\" title=\"logloss with scikilearn output\" data-width=\"626\" data-height=\"45\" data-recalc-dims=\"1\"><img loading=\"lazy\" class=\"tve_image wp-image-4445\" alt=\"logloss with scikilearn output\" data-id=\"4445\" width=\"626\" data-init-width=\"2864\" height=\"45\" data-init-height=\"208\" title=\"logloss with scikilearn output\" src=\"https:\/\/i1.wp.com\/dataaspirant.com\/wp-content\/uploads\/2020\/08\/29_logloss_with_scikilearn_output.png?resize=626%2C45&amp;ssl=1\" data-width=\"626\" data-height=\"45\" data-recalc-dims=\"1\"><\/span><\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\">Log loss penalizes quite high for an incorrect or correct prediction means this metric will punish you for being very sure and very worst other than remaining metrics.<\/p>\n<p dir=\"ltr\">For example,\u00a0<\/p>\n<p dir=\"ltr\">If you are 51% sure about a sample belonging to class 1<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<p dir=\"ltr\"><strong>log loss \u00a0would be:<\/strong> \u00a0&#8211; 1.0 * (1 * log(0.51) + (1 &#8211; 1) * log(1 \u2013 0.51)) = 0.67 \u00a0<\/p>\n<p dir=\"ltr\">And if you are 49% sure for a sample belonging to class 0<\/p>\n<p dir=\"ltr\"><strong>log loss would be<\/strong>: \u00a0&#8211; 1.0 * (0 * log(0.49) + (1 &#8211; 0) * log(1 \u2013 0.49)) = 0.67\u00a0<\/p>\n<p dir=\"ltr\">We can observe from the above example , both are given the same log loss values.<\/p>\n<h2 class=\"\" id=\"t-1596718010220\">Complete Code<\/h2>\n<p>The complete code is placed below, you can also fork the code in our <a href=\"https:\/\/github.com\/saimadhu-polamuri\/DataAspirant_codes\/tree\/master\/evaluation_metrics\" target=\"_blank\" rel=\"noopener noreferrer\">Github repo<\/a>.<\/p>\n<\/div>\n<div class=\"thrv_wrapper thrv_text_element\">\n<h2 class=\"\" id=\"t-1596718010219\">Conclusion<\/h2>\n<p dir=\"ltr\">Every model which is either machine learning or deep learning should be evaluated after model training. Without evaluation developers don&#8217;t know about how much a trained model learnt from history data. <\/p>\n<p dir=\"ltr\">We can evaluate any machine learning classification trained model using any one of the above metrics. Not only to know about trained model performance we can also use these values to know where we can exactly focus to improve performance of models.<\/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-173c42068a2\">\n<div class=\"tve-page-section-in tve_empty_dropzone  \" data-css=\"tve-u-173c4206ba8\">\n<div class=\"thrv_wrapper thrv-columns dynamic-group-kbt3q0q7\" data-css=\"tve-u-173c42068a4\">\n<div class=\"tcb-flex-row v-2 tcb--cols--3 tcb-medium-no-wrap tcb-mobile-wrap m-edit\" data-css=\"tve-u-173c42068a5\">\n<div class=\"tcb-flex-col\">\n<div class=\"tcb-col dynamic-group-kbt3pyfd\" data-css=\"tve-u-173c42068a6\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173c42068a7\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173c42068b2\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\" data-width=\"172\" data-height=\"97\" data-css=\"tve-u-173c42068b3\" data-recalc-dims=\"1\"><img loading=\"lazy\" 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\" 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\" data-css=\"tve-u-173c42068b3\" data-recalc-dims=\"1\"><br \/>\n<span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 data-css=\"tve-u-173b24f570e\" class=\"\">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-173c42068a6\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173c42068c1\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173c42068c2\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\" data-width=\"172\" data-height=\"97\" data-css=\"tve-u-173c42068c3\" data-recalc-dims=\"1\"><img loading=\"lazy\" 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\" 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\" data-css=\"tve-u-173c42068c3\" data-recalc-dims=\"1\"><br \/>\n<span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 data-css=\"tve-u-173b24f571f\" class=\"\">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-173c42068a6\">\n<div class=\"thrv_wrapper thrv_contentbox_shortcode thrv-content-box tve-elem-default-pad dynamic-group-kbt3pwhk\" data-css=\"tve-u-173c42068d0\">\n<div class=\"tve-cb\">\n<div class=\"thrv_wrapper tve_image_caption dynamic-group-kbt3pu4z\" data-css=\"tve-u-173c42068d1\"><span class=\"tve_image_frame\"><img loading=\"lazy\" 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\" data-width=\"172\" data-height=\"97\" data-css=\"tve-u-173c42068d2\" data-recalc-dims=\"1\"><img loading=\"lazy\" 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\" 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\" data-css=\"tve-u-173c42068d2\" data-recalc-dims=\"1\"><br \/>\n<span class=\"tve-image-overlay\"><\/span><\/span><\/div>\n<h4 data-css=\"tve-u-173b24f572e\" class=\"\">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\/six-popular-classification-evaluation-metrics-in-machine-learning\/<\/p>\n","protected":false},"author":0,"featured_media":183,"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\/182"}],"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"}],"replies":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/comments?post=182"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/182\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/183"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=182"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=182"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=182"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}