{"id":943,"date":"2020-09-03T14:15:15","date_gmt":"2020-09-03T14:15:15","guid":{"rendered":"https:\/\/data-science.gotoauthority.com\/2020\/09\/03\/how-to-evaluate-the-performance-of-your-machine-learning-model\/"},"modified":"2020-09-03T14:15:15","modified_gmt":"2020-09-03T14:15:15","slug":"how-to-evaluate-the-performance-of-your-machine-learning-model","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2020\/09\/03\/how-to-evaluate-the-performance-of-your-machine-learning-model\/","title":{"rendered":"How to Evaluate the Performance of Your Machine Learning Model"},"content":{"rendered":"<div id=\"post-\">\n<p><b>By <a href=\"https:\/\/www.linkedin.com\/in\/saurabh-raj-445821153\/\" target=\"_blank\" rel=\"noopener noreferrer\">Saurabh Raj<\/a>, IIT Jammu<\/b>.<\/p>\n<p>\u00a0<\/p>\n<h3>Why evaluation is necessary?<\/h3>\n<p>\u00a0<\/p>\n<p>Let me start with a very simple example.<\/p>\n<blockquote>\n<p><em>Robin and Sam both started preparing for an entrance exam for engineering college. They both shared a room and put an equal amount of hard work while solving numerical problems. They both studied almost the same hours for the entire year and appeared in the final exam. Surprisingly, Robin cleared, but Sam did not. When asked, we got to know that there was one difference in their strategy of preparation, \u201ctest series.\u201d Robin had joined a test series, and he used to test his knowledge and understanding by giving those exams and then further evaluating where is he lagging. But Sam was confident, and he just kept training himself.<\/em><\/p>\n<\/blockquote>\n<p>In the same fashion, as discussed above, a machine learning model can be trained extensively with many parameters and new techniques, but as long as you are skipping its evaluation, you cannot trust it.<\/p>\n<p><strong>How to read the Confusion Matrix?<\/strong><\/p>\n<p>A\u00a0<strong>confusion matrix<\/strong>\u00a0is a correlation between the predictions of a model and the actual class labels of the data points.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*GPrgyxOQDoKwTW8_AreUjQ.png\" width=\"90%\"><\/p>\n<p><em>Confusion Matrix for a Binary Classification.<\/em><\/p>\n<p>Let\u2019s say you are building a model that detects whether a person has diabetes or not. After the train-test split, you got a test set of length 100, out of which 70 data points are labeled positive (1), and 30 data points are labelled negative (0). Now let me draw the matrix for your test prediction:<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*1eSuQjZzMXnEpajA-pCamQ.png\" width=\"90%\"><\/p>\n<p>Out of 70 actual positive data points, your model predicted 64 points as positive and 6 as negative. Out of 30 actual negative points, it predicted 3 as positive and 27 as negative.<\/p>\n<p><strong>Note:<\/strong>\u00a0In the notations,\u00a0<strong>True Positive, True Negative, False Positive, &amp; False Negative<\/strong>, notice that the second term (Positive or Negative) is denoting your prediction, and the first term denotes whether you predicted right or wrong.<\/p>\n<p>Based on the above matrix, we can define some very important ratios:<\/p>\n<ul>\n<li><strong>TPR (True Positive Rate) = ( True Positive \/ Actual Positive )<\/strong><\/li>\n<li><strong>TNR (True Negative Rate) = ( True Negative\/ Actual Negative)<\/strong><\/li>\n<li><strong>FPR (False Positive Rate) = ( False Positive \/ Actual Negative )<\/strong><\/li>\n<li><strong>FNR (False Negative Rate) = ( False Negative \/ Actual Positive )<\/strong><\/li>\n<\/ul>\n<p>For our case of diabetes detection model, we can calculate these ratios:<\/p>\n<p>TPR = 91.4%<\/p>\n<p>TNR = 90%<\/p>\n<p>FPR = 10%<\/p>\n<p>FNR = 8.6%<\/p>\n<p>If you want your model to be smart, then your model has to predict correctly. This means your\u00a0<strong>True Positives\u00a0<\/strong>and<strong>\u00a0True Negatives <\/strong><em>should be as high as possible<\/em>, and at the same time, you need to minimize your mistakes for which your\u00a0<strong>False Positives\u00a0<\/strong>and<strong>\u00a0False Negatives <\/strong><em>should be as low as possible.<\/em><strong>\u00a0<\/strong>Also in terms of ratios, your\u00a0<strong>TPR &amp; TNR <\/strong><em>should be very high<\/em><strong>\u00a0<\/strong>whereas<strong>\u00a0FPR &amp; FNR <\/strong><em>should be very low<\/em><strong>,<\/strong><\/p>\n<p><strong>A smart model: <\/strong>TPR \u2191 , TNR \u2191, FPR \u2193, FNR \u2193<\/p>\n<p><strong>A dumb model: <\/strong>Any other combination of TPR, TNR, FPR, FNR<\/p>\n<p>One may argue that it is not possible to take care of all four ratios equally because, at the end of the day, no model is perfect. Then what should we do?<\/p>\n<p>Yes, it is true. So that is why we build a model keeping the domain in our mind. There are certain domains that demand us to keep a specific ratio as the main priority, even at the cost of other ratios being poor. For example, in cancer diagnosis, we cannot miss any positive patient at any cost. So we are supposed to keep TPR at the maximum and FNR close to 0. Even if we predict any healthy patient as diagnosed, it is still okay as he can go for further check-ups.<\/p>\n<p>\u00a0<\/p>\n<h3>Accuracy<\/h3>\n<p>\u00a0<\/p>\n<p>Accuracy is what its literal meaning says, a measure of how accurate your model is.<\/p>\n<p><strong>Accuracy = Correct Predictions \/ Total Predictions<\/strong><\/p>\n<p><strong>By using confusion matrix, Accuracy = (TP + TN)\/(TP+TN+FP+FN)<\/strong><\/p>\n<p>Accuracy is one of the simplest performance metrics we can use. But let me warn you, accuracy can sometimes lead you to false illusions about your model, and hence you should first know your data set and algorithm used then only decide whether to use accuracy or not.<\/p>\n<p>Before going to the failure cases of accuracy, let me introduce you with two types of data sets:<\/p>\n<ol>\n<li>\n<strong>Balanced:<\/strong>A data set that contains almost equal entries for all labels\/classes. E.g., out of 1000 data points, 600 are positive, and 400 are negative.<\/li>\n<li>\n<strong>Imbalanced:<\/strong>A data set that contains a biased distribution of entries towards a particular label\/class. E.g., out of 1000 entries, 990 are positive class, 10 are negative class.<\/li>\n<\/ol>\n<p><strong>Very Important: Never use accuracy as a measure when dealing with imbalanced test set.<\/strong><\/p>\n<p>Why?<\/p>\n<p>Suppose you have an imbalanced test set of 1000 entries with\u00a0<strong>990 (+ve)<\/strong>\u00a0and\u00a0<strong>10 (-ve)<\/strong>. And somehow, you ended up creating a poor model which always predicts \u201c+ve\u201d due to the imbalanced train set. Now when you predict your test set labels, it will always predict \u201c+ve.\u201d So out of 1000 test set points, you get 1000 \u201c+ve\u201d predictions. Then your accuracy would come,<\/p>\n<p>990\/1000 = 99%<\/p>\n<p>Whoa! Amazing! You are happy to see such an awesome accuracy score.<\/p>\n<p>But, you should know that your model is really poor because it always predicts \u201c+ve\u201d label.<\/p>\n<p><strong>Very Important: Also, we cannot compare two models that return probability scores and have the same accuracy.<\/strong><\/p>\n<p>There are certain models that give the probability of each data point for belonging to a particular class like that in Logistic Regression. Let us take this case:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/480\/1*lYZhk5SGdPPr7yIDh-cLkw.png\" width=\"384\" height=\"421\"><\/p>\n<p><em>Table 1.<\/em><\/p>\n<p>As you can see,\u00a0<strong>If P(Y=1) &gt; 0.5, it predicts class 1.<\/strong>\u00a0When we calculate accuracy for both M1 and M2, it comes out the same, but it is quite evident that<strong>\u00a0M1 is a much better model than M2 by taking a look at the probability scores.<\/strong><\/p>\n<p>This issue is beautifully dealt with by\u00a0<strong>Log Loss<\/strong>, which I explain later in the blog.<\/p>\n<p>\u00a0<\/p>\n<h3>Precision &amp; Recall<\/h3>\n<p>\u00a0<\/p>\n<p><strong>Precision:<\/strong>\u00a0It is the ratio of True Positives (TP) and the total positive predictions. Basically, it tells us how many times your positive prediction was actually positive.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/528\/1*1ppii45Tvam8yThMre3qgQ.png\" width=\"90%\"><\/p>\n<p><strong>Recall :<\/strong>\u00a0It is nothing but\u00a0TPR\u00a0(True Positive Rate explained above). It tells us about out of all the positive points how many were predicted positive.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/458\/1*8K4vavrgn5cozzXcTwjdeQ.png\" width=\"366\" height=\"133\"><\/p>\n<p><strong>F-Measure:<\/strong>\u00a0Harmonic mean of precision and recall.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/625\/1*-vGyENTrxPbvHOHH1wrcpw.png\" width=\"90%\"><\/p>\n<p>To understand this, let\u2019s see this example: When you ask a query in google, it returns 40 pages, but only 30 were relevant. But your friend, who is an employee at Google, told you that there were 100 total relevant pages for that query. So it\u2019s precision is 30\/40 = 3\/4 = 75% while it\u2019s recall is 30\/100 = 30%. So, in this case, precision is \u201chow useful the search results are,\u201d and recall is \u201chow complete the results are.\u201d<\/p>\n<p>\u00a0<\/p>\n<h3>ROC &amp; AUC<\/h3>\n<p>\u00a0<\/p>\n<p><strong>Receiver Operating Characteristic Curve (ROC):<\/strong><\/p>\n<p>It is a\u00a0<strong>plot between TPR (True Positive Rate) and FPR (False Positive Rate)<\/strong>\u00a0calculated by taking multiple threshold values from the reverse sorted list of probability scores given by a model.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/800\/1*yfUqYIZniosGD-3oVvQQ9A.png\" width=\"90%\"><\/p>\n<p><em>A typical ROC curve.<\/em><\/p>\n<p>Now, how do we plot ROC?<\/p>\n<p>To answer this, let me take you back to Table 1 above. Just consider the M1 model. You see, for all x values, we have a probability score. In that table, we have assigned the data points that have a score of more than 0.5 as class 1. Now sort all the values in descending order of probability scores and one by one take threshold values equal to all the probability scores. Then we will have threshold values = [0.96,0.94,0.92,0.14,0.11,0.08]. Corresponding to each threshold value, predict the classes, and calculate TPR and FPR. You will get 6 pairs of TPR &amp; FPR. Just plot them, and you will get the ROC curve.<\/p>\n<p><strong>Note: Since the maximum TPR and FPR value is 1, the area under the curve (AUC) of ROC lies between 0 and 1.<\/strong><\/p>\n<p>The area under the blue dashed line is 0.5. AUC = 0 means very poor model, AUC = 1 means perfect model. As long as your model\u2019s AUC score is more than 0.5. your model is making sense because even a random model can score 0.5 AUC.<\/p>\n<p><strong>Very Important:<\/strong>\u00a0You can get very high AUC even in a case of a dumb model generated from an imbalanced data set. So always be careful while dealing with imbalanced data set.<\/p>\n<p><strong>Note:<\/strong>\u00a0AUC had nothing to do with the numerical values probability scores as long as the order is maintained. AUC for all the models will be the same as long as all the models give the same order of data points after sorting based on probability scores.<\/p>\n<p>\u00a0<\/p>\n<h3>Log Loss<\/h3>\n<p>\u00a0<\/p>\n<p>This performance metric checks the deviation of probability scores of the data points from the cut-off score and assigns a penalty proportional to the deviation.<\/p>\n<p>For each data point in a binary classification, we calculate it\u2019s log loss using the formula below,<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/558\/1*m2Lx-9-q9EIf-NcgnGU7bQ.png\" width=\"90%\"><\/p>\n<p><em>Log Loss formula for a Binary Classification.<\/em><\/p>\n<p>where p = probability of the data point to belong to class 1 and y is the class label (0 or 1).<\/p>\n<p>Suppose if p_1 for some x_1 is 0.95 and p_2 for some x_2 is 0.55 and cut off probability for qualifying for class 1 is 0.5. Then both qualify for class 1, but the log loss of p_2 will be much more than the log loss of p_1.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/875\/1*cdMTLQmABkMlAltajEQW_Q.png\" width=\"90%\"><\/p>\n<p>As you can see from the curve, the range of log loss is [0, infinity).<\/p>\n<p>For each data point in multi-class classification, we calculate it\u2019s log loss using the formula below,<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/366\/1*evhAy9ERWjUNpiogxCYGHA.png\" width=\"293\" height=\"91\"><\/p>\n<p><em>Log Loss formula for multi-class classification.<\/em><\/p>\n<p>where y(o,c) = 1 if x(o,c) belongs to class 1. The rest of the concept is the same.<\/p>\n<p>\u00a0<\/p>\n<h3>Coefficient of Determination<\/h3>\n<p>\u00a0<\/p>\n<p>It is denoted by\u00a0<em>R\u00b2. <\/em>While predicting target values of the test set, we encounter a few errors (e_i), which is the difference between the predicted value and actual value.<\/p>\n<p>Let\u2019s say we have a test set with n entries. As we know, all the data points will have a target value, say [y1,y2,y3\u2026\u2026.yn]. Let us take the predicted values of the test data be [f1,f2,f3,\u2026\u2026fn].<\/p>\n<p>Calculate the\u00a0<strong>Residual Sum of Squares, <\/strong>which is the sum of all the errors (e_i) squared,<strong>\u00a0<\/strong>by using this formula where fi is the predicted target value by a model for i\u2019th data point.<\/p>\n<p><img class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/600\/1*P9YvjU1FPIa5xos8-RmGBw.png\" width=\"90%\"><\/p>\n<p><em>Total Sum of Squares.<\/em><\/p>\n<p>Take the mean of all the actual target values:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/305\/1*lw05JqXgH2p1O8tTNdIOyw.png\" width=\"244\" height=\"127\"><\/p>\n<p>Then calculate the\u00a0<strong>Total Sum of Squares, <\/strong>which is proportional to the variance of the test set target values:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/423\/1*bdzzQnpHe1Vao3BMYAz6uQ.png\" width=\"338\" height=\"92\"><\/p>\n<p>If you observe both the formulas of the sum of squares, you can see that the only difference is the 2nd term, i.e., y_bar and fi. The total sum of squares somewhat gives us an intuition that it is the same as the residual sum of squares only but with predicted values as [\u0233, \u0233, \u0233,\u2026\u2026.\u0233 ,n times]. Yes, your intuition is right. Let\u2019s say there is a very simple mean model that gives the prediction of the average of the target values every time irrespective of the input data.<\/p>\n<p>Now we formulate R\u00b2 as:<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter size-large\" src=\"https:\/\/miro.medium.com\/max\/388\/1*fqYSfGQQF0Rs3WbxOEkNBw.png\" width=\"310\" height=\"108\"><\/p>\n<p>As you can see now, R\u00b2 is a metric to compare your model with a very simple mean model that returns the average of the target values every time irrespective of input data. The comparison has 4 cases:<\/p>\n<p><strong>case 1: SS_R = 0<\/strong><\/p>\n<p>(R\u00b2 = 1) Perfect model with no errors at all.<\/p>\n<p><strong>case 2: SS_R &gt; SS_T<\/strong><\/p>\n<p>(R\u00b2 &lt; 0) Model is even worse than the simple mean model.<\/p>\n<p><strong>case 3: SS_R = SS_T<\/strong><\/p>\n<p>(R\u00b2 = 0) Model is same as the simple mean model.<\/p>\n<p><strong>case 4: SS_R &lt; SS_T<\/strong><\/p>\n<p>(0&lt; R\u00b2 &lt;1) Model is okay.<\/p>\n<p>\u00a0<\/p>\n<h3>Summary<\/h3>\n<p>\u00a0<\/p>\n<p>So, in a nutshell, you should know your data set and problem very well, and then you can always create a confusion matrix and check for its accuracy, precision, recall, and plot the ROC curve and find out AUC as per your needs. But if your data set is imbalanced, never use accuracy as a measure. If you want to evaluate your model even more deeply so that your probability scores are also given weight, then go for Log Loss.<\/p>\n<p>Remember, always evaluate your training!<\/p>\n<p><a href=\"https:\/\/medium.com\/swlh\/how-to-evaluate-the-performance-of-your-machine-learning-model-40769784d654\" target=\"_blank\" rel=\"noopener noreferrer\">Original<\/a>. Reposted with permission.<\/p>\n<p>\u00a0<\/p>\n<p><b>Related:<\/b><\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/www.kdnuggets.com\/2020\/09\/performance-machine-learning-model.html<\/p>\n","protected":false},"author":0,"featured_media":944,"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\/943"}],"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=943"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/943\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/944"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}