How to choose a good evaluation metric for your Machine learning model
How accurate is your model?
2. Why do we need to evaluate our model?
3. Supervised learning and classification problems.
7. Accuracy and Recall using Venn Diagram
8. Accuracy vs Recall using Example.
10. Confusion matrix
11. Evaluating for regression problems
12. Mean Absolute Error
13. Mean Square Error
14. Root Mean Square Error
15. What is considered as a good metric value for your model?
What is the process of evaluating your model?
Why do we need to evaluate our model?In general, it is a good metric to come up with when we are talking about your model with the guys in other teams who don’t understand tech. No model is 100% correct and there is no perfect score you want to achieve. It simply depends on case to case basis. Also, sometimes giving the wrong answers is better than giving a very wrong answer. For example, if you are building a cancer detector depending upon the test reports of the patient. You might want to tell that the patient might have
cancerand finally get off with the real cancer test, rather than giving
no canceroutput from your model when in reality the person had cancer.
Supervised learning and classification problems.
Supervised Learningare the problems where the outcomes of the model are already known. For example a data set of housing prices of an area.
Classification Problemis a subset of supervised learning where the outcomes are generally divided into two or more parts. For example, whether a person is having cancer or not. All these models can be evaluated on the following parameters.
Test train split using sklearnGenerally, we divide the total dataset into two parts. The first dataset is known as the training data and the other is known as the test data. The idea behind such a division of the data is to use test data just for evaluation purposes. To find if the model we are trying to use for the given dataset is good enough, or we want to use a different one. Here is a code sample that can help you to divide your data frame into several parts using scikit-learn.
import pandas as pd from sklearn.model_selection import train_test_split df = pd.read_csv('Ecommerce Customers') X = df[['Avg. Session Length', 'Time on App', 'Time on Website', 'Length of Membership']] y = df['Yearly Amount Spent'] df = pd.read_csv('Ecommerce Customers') X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
test_sizeis the amount of data that you want to your test split to be.
0.3means that the test split would be 30%.
AccuracyAccuracy is the simple calculation where you divide the number of data points evaluated correctly by the number of total data points.
predictionscorresponding to the given
X_test, finally, we compare these predictions with the
y_testwhich are the real outcomes to the corresponding parameters. We will talk more about how we calculate these values in later posts. Keep track of the progress by subscribing. Accuracy is one of the easiest ways to evaluate the performance of your model. Note: This type of evaluation model is not the best thing to use when the data available to you is unbalanced.
Unbalanced data is the type of dataset in which you have more outcomes for one type of data and fewer outcomes for others.For example In a cat-dog images dataset, out of 100 pictures, you have 90 pictures of dog and 10 pictures of cat. In such case, if you are checking the accuracy for dog pictures and your model always return
dog, no matter what picture is thrown at it, the accuracy in any case will be
It is the ability of your model to find all the relevant cases in your model.Most of these evaluation models are used when the data in mainly imbalanced.
recallis great, it goes with the value of
precisionand in most cases both of their value is considered.
Ability of a model to identify only the relevant data points.
Accuracy and Recall using Venn DiagramConsider that we were playing a simple game online. The rules of the game are pretty simple, you will have to shoot the space ships of the opposition constantly, until you see a survivor space ship coming toward you.
Total time you Stopped shooting: x Total time you Stopped shooting with survivor ship ahead you: y Total time you Stopped shooting with Opposition Space ship ahead you: z
PrecisionPrecision is the value which gives the value of number of times you took the correct action of all the times you took an action. So, precision is given by the intersection divided by the blue part of the Venn diagram. i.e.
RecallRecall is the value which gives the value of all the times you were supposed to take an action of all the times you took an action. So, Recall is given by the intersection divided by the orange part of the Venn diagram.
Accuracy vs Recall using Example.Choosing what you want as your evaluation metric really depends upon the problem that you are trying to solve. I will take a few examples that will help you decide when to use the first and when to use the next.
Case 1: Disease detection (Covid 19)Let’s say you are working on a model that is trying to detect whether the person is having COVID 19. In this particular case, the cost of
False Negativeis more than the cost of
False Positive. Hence, if you predict that someone is not having a disease when someone is actually carrying one will lead to some bad circumstances. They might go out and start spreading it. On the other hand, if you predict someone not really carrying the disease when in reality he/she is a disease carrier, it is not a big of an issue, because we will obviously do further tests on them. In this case, we will try to lower the result of our evaluation metric when the number of false negatives is increasing. Looking at the formulas of
Accuracy, we know that the thing that we want to use is,
Case 2: Email Detection (Spam or Not spam)In this problem, the cost of
False Positiveis more than the cost of
False Negative. If your models start predicting important mails as spam and start sending them to the spam folder, it would be really bad. So, in this case, you will want to choose a metric that will lower its value when the number of False positives starts increasing. Again we know the right metric for such a problem is
It is the harmonic mean of precision and recall.F1-score helps us to consider both the values of precision and recall while evaluating our model.
Confusion matrixConfusion matrix is a Matrix in which we evaluate all the positives and negatives like:
- True Positive
- False Positive
- True Negative
- False Negative
confusion matrixfor a case where patient’s data was tested for a specific disease. Out of total 165 patients our model produced the following results. True Positives: Our model predicted that 100 patients were carrying the disease, and they were actually carrying the disease. True Negatives: Our model predicted that 50 patients were not carrying the disease, and they actually were not carrying it. False Positives: Our model predicted that 10 patients were carrying the disease, and they actually were not carrying it. This is also known as Type-I error. False Negatives: Our model predicted that 5 patients were not carrying the disease, and they actually were carrying it. This is also known as Type-II error. We can go forward and calculate all the values for Accuracy, Recall, Precision and F1-Score from this confusion matrix. To print the confusion matrix of a model in
sklearnuse the following code.
from sklearn.metrics import confusion_matrix print(confusion_matrix(y_test, predictions)) # where y_test is the data frame of test values # and predictions are the model predicted values
precisionetc. using the following code,
from sklearn.metrics import classification_report print(classification_report(y_test, predictions))
Evaluating for regression problemsRegression problems are a little different from the categorization problems as the output is just not a single value. Here you can actually see how off your predicted value was from the real value. Here are the three ways in which regression models can be evaluated.
Mean Absolute ErrorIts a simple difference between the predicted and the actual values. Here is the simplest way in which you can calculate Mean Absolute error for your model.
sklearnusing the following code.
from sklearn import metrics metrics.mean_absolute_error(y_test, predictions)
Mean Square ErrorMean square error is the sum of squared errors of the predicted and actual values.
sklearnusing the following code.
from sklearn import metrics metrics.mean_squared_error(y_test, predictions)
100 square unitaccurate.
Root Mean Square ErrorTo solve the problem with the
MSE, we use
RMSEjust by square rooting the values of
MSE. We can then simply tell about the proficiency of our model from the value derived from this.
from sklearn import metrics np.sqrt(metrics.mean_squared_error(y_test, predictions))
What is considered as a good metric value for your model?As we have already discussed a good metric value of the model really depends upon the type model you are evaluating. You will always have to check with the peers who really are going to use your model. Hope you liked the post, do leave your thoughts on what you use to evaluate your model and what do you think is a good metric value for your model.
Did you enjoy reading or think it can be improved? Don’t forget to leave your thoughts in the comments section below! If you liked this article, please share it with your friends, and read a few more!