There are various machine learning algorithms like KNN, Naive Bayes, Logistic Regression, Linear Regression, SVM, Ensemble models etc etc. But how do you measure the performance of these models ? Is there any one metric which will define how good a model is ? Well, in this blog I will discuss about the important KPIs (Key Performance Indicators) to measure the performance of a Machine Learning Model
- Confusion matrix
- Precision, Recall & F1 score
- Receiver Operating Characteristic Curve (ROC)
- R-Squared Coefficient
- Median absolute deviation (MAD)
- Error Distribution
Sample dataset: I will consider a sample test dataset to explain the metrices. The sample data contains 15 positive Y’s & 15 negative Y’s. It is a simple binary classification problem
P.S: We measure the performance of models based only on TEST data and NOT on train data/ cross-validation data.
Accuracy is the most easiest metric to understood & to implement as well. Accuracy is nothing but the ratio of correctly classified data points to the total number of data points.
Example: For the above sample dataset, if a model predicts 13 positive points & 14 negative points correctly, then
Accuracy is a bad metric for a highly imbalanced dataset: Consider a highly imbalanced dataset like credit card fraud detection, where >99% of the data points are negative. Lets say we have very dumb model which will always predict negative, irrespective of given data points (i.e., X). And if we calculate the Accuracy for this dumb model, it would be more than 0.99 that means even a dumb model is showing an accuracy of 0.99 which is obviously wrong.
Can not take probability scores: Accuracy don’t take probability scores into consideration. It will just consider the predicted Y values. For example
Lets say we have two Models M1 & M2.
Y : real Y values M1_prob: probability of Y = 1 for model M1 M2_prob: probability of Y = 1 for model M2 M1_pred: predicted Y values from model M1 M2_pred: predicted Y values from model M2
As we can see, both M1 & M2 models predicted the same Y’s & have same accuracy.But if you see probability scores of M1 & M2, the model M1 is performaning better than the model M2, since the probability scores are high for the M1 model, whereas for the model M2,the probability scores are almost half
Confusion matrix is a matrix used to describe the performance of a classification model. It solves the problem the accuracy metric had with the imbalanced dataset
Lets consider a binary classification problem i.e., Y belongs to 0 or 1. 0 being negative label & 1 being positive label. Let Y be the actual values & Y^ be the predicted values
TN (True Negative) : number of data points for which Y = 0 & Y^ = 0
FN (False Negative) : number of data points for which Y = 1 & Y^ = 0
FP (False Positive) : number of data points for which Y = 0 & Y^ = 1
TP (True Positive) : number of data points for which Y = 1 & Y^ = 1
Tip to remember: Here is a tip to remember which I learned from Applied AI course
Example: Lets consider the case Y = 0, Y^ = 0. Here predicted value is 0, i.e, Negative (N) & the it is correct (T) (since the actual value is also Negative). So it becomes True Negative (TN)
If we have a multi class classification, then confusion matrix size would be CxC, where C is the number of class labels.
If the model you trained is very good, then all the principle diagonal values will be high & non-principle diagonal values will be low or zero.
Let P & N be the total number of Positive & Negative test data points respectively, Then
In layman language,
TPR = Percentage of correctly classified positive points
TNR = Percentage of correctly classified negative points
FPR = Percentage of incorrectly classified positive points
FNR = Percentage of incorrectly classified negative points.
Example: Consider a highly imbalanced dataset like credit card fraud detection, where >99% of the data points are negative. Lets say we have very dumb model which will always predict negative, irrespective of given data points (i.e., X). And if we calculate the Confusion matrix for this dumb model, then TNR will be 100 & TPR will be zero but FNR will be high which indicats the model is very dumb
In Confusion matrix we have 4 numbers, how one will know which one is important? Well, it depends on the dataset.
Example: If the dataset is cancer detection/credit card fraud detection, then we want high TPR and very less or 0 FNR.
- Can not take probability scores: Confusion matrix don’t take probability scores into consideration. It will just consider the predicted Y values.
Precision, Recall, F1 score
Precision & Recall are used in information retrieval, pattern recognition & binary classification. Please refer Wiki for info. Precision & Recall considers only the positive class labels.
In layman language,
Precision: Of all the predicted POSITIVE labels, how many of them are actually POSITIVE
Recall: Of all the actual POSITIVE labels, how many of them are correctly predicted as POSITIVE.
We always want Precision & Recall to be high. Precision & Recall values ranges from 0 to 1
F1 Score: It is a single measurement, which considers both Precision & Recall to compute the score. It is basically the harmonic mean of Precision & Recall. The value of F1 score ranges from 0 to 1.
This metric is extensively used in Kaggle compitations. But Precision & Recall is more interpretable or I can say F1 score is difficult to interpret.
Modification of F1 Score For Multi label classification
In Multi-label Classification, multiple labels may be assigned to each instance and there is no constraint on how many of the classes the instance can be assigned to. Source: Wiki
There are two variations of F1 Scores
Micro F1 Score:
Calculate metrics globally by counting the total true positives, false negatives and false positives.
Lets say we have
C labels, then Micro F1 score is defined as
This is a better metric when we have class imbalance since it does take into account about the label frequency
Macro f1 score:
It is the simple average of F1 scores of all labels. This does not take into account about the label frequency.
For info on F1 score clickhere
Receiver Operating Characteristic Curve (ROC)
ROC curve is a curve which is created by plotting TPR agains FPR. ROC Curve metric is applicable only for binary classification. Here is step by step procedure to draw ROC curve.
Lets assume, the model which we are using gives some kind of scores like probability scores (pridict_prob) in such a way that increase in score indicates higher probability of data point belonging to class 1. Lets assume Y^ denotes the predicted probability scores
1 = POSITIVE class
0 = NEGATIVE class
STEP 1: Sort Y^ in descending order.
STEP 2: Thresholding ().
For each of the probability scores (lets say ) in Y^ columns, if Y^ >= , then predicted label would be positive(1).
|X||Y (actual)||Y^ prob(Y==1)||Y_predicted = 0.94||Y_predicted = 0.87||Y_predicted = 0.83||Y_predicted = 0.80|
If we have N data points, then we will have N thresholds.
STEP 3: For each of the Y_predicted (), calculate TPR & FPR.
In our example
If we have N data points, then we will have N TPR,FPR pairs.
STEP 4: Plot TPR vs FPR by taking N TPR, FPR pair. Typically the Graph look like this
P.S: The example (TPR,FPR) pairs have not been plotted in the above graph
Random Model: Model which generates randomly 0 & 1’s
Good Model: Model which has high TPR & low FPR
Area Under ROC Curve (AUC): AUC is the area under the ROC curve. More the area under the curve, the more good is the model. A random model will have an AUC of 0.5
Imbalanced dataset: For an imbalanced dataset, a dumb model might give high AUC, which may be misleading sometimes.
Depends on the ORDERing of the scores & doesn’t depends the scores itself.
Works only for binary classification problems
Converting a dumb model into a reasonable good model
A dumb model is one which has AUC an < 0.5 In that case just swap class labels i.e., change 1 to 0 & 0 to 1 (
Y_predicted = 1-Y_predicted) to make it a reasonably good model.
It is an important metric & it uses probability scores to calculate the loss. This metric can be used for both binary classification & multi class classification. And it ranges from 0 to (infinity). Log loss is a loss, so we always wants to minimize the losses. Lower the loss, the good is the model.
log = natural log
n = number of datapoints
c = number of class labels
= 1 if data point belongs to class j else 0
= probability of belonging to class j
Example: Lets consider a binary classification dataset
|log loss||log loss score|
As we can see from the above table, the data point is having higher probability of belonging to class label 1 & so its log loss is less comparitively, where as for the data point , the log loss is higher because its probability is almost half, i.e., the probability of belonging to 1 or 0 is almost same.
Difficult to interpret the log loss score.
This metric is used to measure the performance of a regression problem (Y belongs to real values). Lets Y^ denotes the predicted values & Y denotes the actual values.
Sum of Squares (total): The simplest regression model one can build is the average model i.e, predicted value will be equal to average of all actual Y values.
where, is the average of all y values
Sum of Squares (residual): Residual means the error, which is the difference between the predicted value & real value. Lets say y^ is the predicted value which is predicted using regression algorithms like linear regression.
R Squared: It combines both &
This is the best case ever could happen, i.e., predicted values are equal to actual values, so zero errors, therefore for the best case
This means our model is performing better than the average model. The values of ranges from 0 to 1.
This means our model is performing as good as the average model. So
This means our model is performing worst than the average model. So
is not very robust to outliers
Median absolute deviation (MAD)
It overcomes the problem we had with . Median is robust to outliers
Median: It is the central tendancy or the midpoint of observed values
e: It is the difference between the actual value & the predicted value i.e,
MAD: It is the median of the absolute difference between the error (e) and the median of errors for all data points
is the median of the errors
PDF: As we can see, most of the errors are having smaller values where as few errors are having larger values
CDF: 95% of the errors are having the values less than 0.1 & only 5% of the errors are having values > 0.1.
Compare two models using CDFs of errors
Model M1: 95% of the errors are having less than 0.1 value
Model M2: 90% of the errors are having less than 0.1 value
Since we always want errors to be zero or close to zero, Model M1 is better than Model M2, because more number of errors are close to zero in Model M1 than in Model M2
Thanks for reading. Please contact me regarding any queries or suggestions.