Becoming Proficient in 12 Key Model Evaluation Metrics for Assessing Model Performance in Machine Learning

data science Jan 17, 2024
thumbnail image for model evaluation metrics blog from BigDataElearning

 Ever pondered what propels your car's efficiency?

It’s more than just the engine – it’s the intricate mix of metrics: speedometers, fuel gauges, and temperature indicators. They're the conductors of your vehicle's optimal performance.

Imagine taking it to a mechanic who delves into various metrics to gauge its performance and tweak as necessary, right?

In the world of machine learning, we encounter “model evaluation metrics” - the compass guiding our models to run efficiently, make precise predictions, and avoid spewing out nonsense.

But what exactly are these model evaluation metrics?

Why are they paramount? 

Let's plunge into the following topics together to break down model evaluation metrics in Machine Learning

Data Science Explained In 20 Infographics

“Data Science Made Simple: Learn It All Through 20 Engaging Infographics (Completely Free)"

 

What Is a Model Evaluation Metric in Machine Learning?

Think of machine learning models as if they're students, just like you and me, learning from data.

 It's a bit like how you learn different subjects in school and then take tests. These models are trained using data and then get evaluated.

Model evaluation metrics are like the grades you get on those tests. They're numbers that tell you how well the model can predict things compared to what actually happens. 

Just like a test checks how much you understand, these model evaluation metrics check if a machine learning model really gets what it was taught and if it can make good predictions with new stuff it hasn't seen before.

Without these metrics, you wouldn't know how well a model is doing or if you can trust what the model predicts.

The model evaluation metrics that are used to evaluate classification-type machine learning models are called "classification metrics". The one which is used to evaluate the regression-type machine learning models are called "regression metrics".

 

What Are the Metrics for Model Comparison?

There are all different types of models, each with its own special job. 

 Some are like those weather apps that predict the temperature for the week..

 while others are like those apps that can tell if a picture is of a cat or a dog..

 Now, because these models have their peculiarities, you should use different model metrics to see how well they're doing.

 Think of it like this: when you are timing a sprinter, we're all about seconds, right? 

 But when you are watching a high jumper, it's all about how high they can go.

So, with machine learning, you measure a regression metrics of model by how close its predictions are to the real values, just like timing a sprinter. 

 

But for a classification model, you would be interested in how accurate it is at putting things in the right categories, kind of like checking how high a high jumper can jump. 

 Different jobs, different ways to see if they're doing well!

 Now let’s check out some real-world examples of model metrics in action…

Model Evaluation Metrics Examples

Streaming Services: Netflix

You know when you're chilling and Netflix suggests this fantastic series, and you're hooked? 

 But hey, if it keeps throwing sci-fi at you when you’ve never ventured beyond rom-coms? 

 That’s a sign that the machine-learning model might need some tuning, don’t you think?

 This is where solid model evaluation metrics step in, helping Netflix keep us all happily binging.

 

Health Tech: Wearable Trackers

Consider wearable trackers like Fitbits and Apple Watches - not merely accessories, right?

 They're keeping tabs on your heart rate, your sleep, and yeah, your stress levels too. 

 They use machine learning to figure out the patterns and guess your health stats. 

 Now, imagine your tracker keeps getting your heart rate wrong. ๐Ÿ˜จ 

 That's more than a simple mistake, that's messing with your health. 

 So, you see why tight model evaluation is a big deal here?

E-commerce: Amazon

Ever marveled at how Amazon nails your preferences?

 It’s machine learning, analyzing your clicks, views, and lingering pauses over products. 

 A spot-on product suggestion? Bingo, the model is nailing it. 

 But if it's urging you to rebuy that blender? It might be time for a little tune-up, agreed?

 Good model metrics keep Amazon’s suggestions on point.

Finance: Credit Scoring

Moving to the finance realm: credit scoring.

 Credit scores aren’t just pulled out of thin air. 

 Banks have machine learning algorithms buzzing behind the scenes, studying your spending, debts, and all that jazz to figure out your credit score. 

 Ever heard of someone getting wrongly turned down for a loan? 

 That’s the mess. Regular check-ins, or evaluations, keep things fair and square in the credit world.

Autonomous Vehicles

And let’s venture into the future - self-driving cars!

Machine learning is at the wheel, processing a mind-blowing amount of data to make those drive-time decisions. 

One wrong call? It’s not just about rerouting — it’s a big-time safety blunder.

Hence, stern model performance metrics are the guardrail here, ensuring everything's safe and sound.

Confusion Matrix

Alright, diving into some tech talk but keeping it light. 

In machine learning, especially the classification problems, meet the big player — the Confusion Matrix. 

Picture a report card, it shows how many times the model aced the test or goofed up. 

It's usually a 2x2 grid, detailing four key outcomes: “True Positives”, “False Positives”, “True Negatives”, and “False Negatives”.

Let’s look at each of these briefly:

True Positives (TP)

  • Correctly identified positive cases.
  • When the model says it's a "yes" and it truly is a "yes."
  • Example: Predicting it will rain tomorrow, and it does.

False Positives (FP)

  • Incorrectly identified positive cases.

  • The model says it's a "yes," but in reality, it's a "no."

  • Example: Predicting it will rain tomorrow, but it's sunny.

True Negatives (TN)

  • Correctly identified negative cases.

  • The model predicts a "no," and it's indeed a "no."

  • Example: Predicting it won't rain tomorrow, and it doesn't.

False Negatives (FN)

  • Incorrectly identified negative cases.
  • The model thinks it's a "no," but it's actually a "yes."

Example: Predicting it won't rain tomorrow, but it pours.

We strongly recommend reviewing more about this in Confusion Matrix explained

This is why the Confusion Matrix is clutch for a quick peek at how a model is doing and where it needs a little nudge in the right direction.

Types of Model Performance Metrics

Let’s explore the metrics you’ll use to improve your own machine-learning models:

1. Mean Squared Error (MSE)

What is it? It measures the average squared difference between actual and predicted values. Think of it as the average squared distance each arrow lands from the bullseye in an archery contest.

  

  • Formula: MSE = (1/N) * Σ(actual - predicted)^2
  • Advantages: Easily interpretable and widely used. More sensitive to larger errors because they're squared.
  • Drawbacks: Since errors are squared, it gives more weight to outliers.

When to use: In regression problems, especially when larger errors matter more.

Python Example Code

from sklearn.metrics import mean_squared_error

mse = mean_squared_error(y_true, y_pred)

2. Mean Absolute Error (MAE)

  • What is it? It represents the average of the absolute differences between predicted and actual values. It's like calculating the straight-line distance from the target, disregarding direction.

The only difference between the previous MSE and this is that , MSE calculates the average squared distance between the target and predicted value, whereas the MAE calculates the absolute distance between the target and the predicted value.

 

  • Formula: MAE = (1/N) * Σ|actual - predicted|
  • Advantages: Gives a linear penalty to errors, making it less sensitive to outliers than MSE.
  • Drawbacks: May not be as informative as other metrics for models where the scale of the error matters.
  • When to use: In regression problems when you want a simple measure of model accuracy.

Python Example Code

from sklearn.metrics import mean_absolute_error

mae = mean_absolute_error(y_true, y_pred)

3. Classification Accuracy

  • What is it? Let's say you're building a model to identify whether a credit card transaction is fraudulent or not.

You have a dataset of 12 transactions, with 8 legitimate transactions and 4 fraudulent transactions. We are taking a simplified example for understanding purposes.

You train your model on this dataset and then use it to predict the class of new transactions.

In an ideal scenario, a machine learning model would accurately predict all 8 legitimate cases as legitimate and all 4 fraudulent cases as fraudulent, right?
 

Unfortunately, no model is perfect and there will always be some level of errors. The objective of machine learning is to achieve the highest possible accuracy while minimizing errors.

Let’s say we identified the “true positives”, “true negatives”, “false positives”, and “false negatives” as below, and plotted on the confusion matrix as below.

Let’s visualize it in image.

With the above images, we have plotted into the confusion matrix.  Now it will be easy to understand all the metrics.

Accuracy is the ratio of correct predictions to total predictions.

This measures the overall performance of the model. It's calculated as (TP + TN) / (TP + TN + FP + FN). In our example, the accuracy would be (3+7) / (3+7+4+1) = 10/15 = 0.66 or 66%.

In simple terms, this is the total number of true’s (TP+TN) your model can predict out of the total instances.

  • Formula: Accuracy = (Number of correct predictions) / (Total predictions)
  • Advantages: Simplest metric to understand and compute.
  • Drawbacks: Can be misleading, especially for imbalanced datasets.
  • When to use: When the dataset is balanced and misclassification costs are uniform.

Python Example Code

from sklearn.metrics import accuracy_score

acc = accuracy_score(y_true, y_pred)

4. Specificity

  • What is it? Specificity measures the proportion of actual negatives that were correctly identified. 

In other words, Specificity is the ratio of predicted true negatives among all actual negative instances. Specificity is also called the True Negative Rate (TNR). 

It's calculated as (TN) / (TN + FP). 

In our example, 

The specificity would be (7) / (7+4) = 7/11 = ~0.63 or 63%.

 Visually this is what we are doing

 

  • Formula: Specificity = (True Negatives) / (True Negatives + False Positives)
  • Advantages: Provides insight into the model's ability to correctly predict negative cases.
  • Drawbacks: Does not give a full picture of the model's performance if used alone.
  • When to use: When you need to measure the model's ability to correctly identify true negatives, especially if the cost or consequence of false positives is high.

Python Example Code

from sklearn.metrics import confusion_matrix

 # Sample data

y_true = [0, 1, 0, 0, 1, 1]

y_pred = [0, 0, 0, 1, 1, 1]

 tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()

specificity = tn / (tn + fp)

print(f"Specificity: {specificity:.2f}")

5. Precision

  • What is it? Precision is the ratio of true positives to the total number of positive predictions.

 In other words it determines how many were correct among all the instances that your model predicted as positive. 

 In other words, TP/(TP + FP)

 In our example, 

Precision would be 3/(3+4) = 3/7 = 0.43 or ~43%

Visually 

You can remember “precision” as a ratio to determine how many predicted positives are actually positives?

  • Formula: Precision = (True Positives) / (True Positives + False Positives)
  • Advantages: Useful when the cost of a false positive is high.
  • Drawbacks: Doesn't account for false negatives.
  • When to use: When it's costly to have false positives, e.g., email spam detection.

Python Example Code

from sklearn.metrics import precision_score

precision = precision_score(y_true, y_pred)

6. Recall or Sensitivity

  • What is it? Recall is the ratio of true positives to the total number of actual positive instances, or TP/(TP+FN). 

In our example, 

Recall is 3/(3+1) = ¾ = 0.75, or 75%. 

 Visually

You can remember recall as a metric that shows how many of the actual positives were correctly predicted as positive by your model. 

  • Formula: Recall = (True Positives) / (True Positives + False Negatives) 
  • Advantages: Useful when the cost of a false negative is high. 
  • Drawbacks: Doesn't account for false positives. 
  • When to use: When it's costly to have false negatives, e.g., cancer diagnosis.

Python Example Code

from sklearn.metrics import recall_score

recall = recall_score(y_true, y_pred)

 7. F1 Score

  • What is it? The F1-score is the harmonic mean of precision and recall, and it is a measure of the balance between precision and recall.  

F1 = 2 * (precision * recall) / (precision + recall) 

Precision = 43% or 0.43 

Recall = 75% or 0.75 

2 * (0.43 * 0.75) / (0.43 + 0.75) 

F1 Score = 0.546 

The F1-score ranges between 0 and 1, with 1 being the best possible score. 

  • Formula: F1 = 2 * (precision * recall) / (precision + recall) 
  • Advantages: Useful for imbalanced datasets. Takes both false positives and false negatives into account. 
  • Drawbacks: May not be ideal if one of precision or recall is more important than the other. 
  • When to use: In classification problems where the balance between precision and recall is crucial.

Python Example Code

from sklearn.metrics import f1_score

f1 = f1_score(y_true, y_pred) 

8. Area Under Curve (AUC)

  • What is it? : The AUC refers to the space under the ROC curve, shedding light on how adept the model is at distinguishing between positive and negative classes. 

Before diving in, let's revisit the concepts of threshold, sensitivity, specificity, and the False Positive Rate (FPR). 

Threshold: Think of it as a filter guiding the model's cautiousness in labeling something as spam or not. 

Recall: Recall is the ratio of predicted true positives among all the actual positive instances. Recall is also known as the True Positive Rate (TPR)

Specificity : Specificity is the ratio of predicted true negatives among all actual negative instances. Specificity is also called the True Negative Rate (TNR).  

FPR, or False Positive Rate : FPR equals 1-specificity. 

Picture yourself assessing a model's ability to differentiate between cat and dog images. The ROC curve visualizes how well the model performs this task. 

Recall (TPR) signifies the model correctly spotting cat images, while Specificity gauges its accuracy in identifying dog pictures.

Plotting these measures on a graph demonstrates the trade-off between sensitivity (catching all cats) and specificity (avoiding wrong dog identifications).

The graph illustrates FPR (also 1-Specificity) on the x-axis and TPR (Recall) on the y-axis.

An ideal model sits at the top-left, excelling in both sensitivity and specificity.

A random guess would form a diagonal line, scoring 0.5 on the curve.

The closer the curve edges towards the top-left, the better the model distinguishes cats from dogs.

 AUC, akin to a performance grade, evaluates how well the model separates cats from dogs overall. 

 A score of 1 signifies consistent accuracy, while 0.5 reflects chance-like predictions.

  • Formula: Area under the plot of true positive rate vs. false positive rate across various threshold values.
  • Advantages: Shows model performance across all classification thresholds. Not affected by imbalanced classes.
  • Drawbacks: Only applicable for binary classification.
  • When to use: When evaluating the discriminatory power of binary classification models.

Python Example Code

from sklearn.metrics import roc_auc_score

auc = roc_auc_score(y_true, y_scores)

9. Receiver Operating Characteristics (ROC) Curve

  • What is it? This is very simple.  ROC Curve is nothing but the graphical representation of the above.

So the graph below is nothing but the ROC curve, whereas the AUC is the AREA under the curve.

  • Formula: Plot with true positive rate (sensitivity) on the y-axis and false positive rate (1-specificity) on the x-axis.
  • Advantages: Useful for visualizing and comparing the performance of different models.
  • Drawbacks: Only applicable for binary classification.
  • When to use: To evaluate and compare binary classification models across various thresholds.

Python Example Code

from sklearn.metrics import roc_curve

fpr, tpr, thresholds = roc_curve(y_true, y_scores)

10. Logarithmic Loss (Log Loss)

  • What is it? Logarithmic Loss performance metric is like a judge for how good your classifier is in guessing stuff.

Let’s take an example of catching sneaky fraudulent transactions.

So, when your model's trying to decide if a transaction's legit or fishy, it doesn't just say "fraud" or "not fraud." It gives a probability score, like how confident it feels about its guess, on a scale from 0 to 1. 

Kinda like saying, "I'm pretty sure this one's legit, maybe around 0.8 confident."

This “Log Loss” gig takes those guesses and looks at how close they were to the real deal, factoring in that confidence score. 

If your model was super sure about a wrong guess, it gets a bigger slap on the wrist :-) 

But if it wasn't too sure about a miss, it's a gentler rap on the knuckles. You get the point?

Basically, Log Loss isn't just about right or wrong—it cares about how confident your model was about those guesses. It nudges your model to be both accurate and sure of itself.

When the Log Loss score is lower, it means your model's guesses are closer to reality. So, when it comes to spotting fishy transactions, a low Log Loss means your model's doing a better job at sniffing out the real scams, which is mega important to avoid financial mess-ups from false alarms.

Look at the graph below: 

As that "log loss function value" drops down on the y-axis, it means your model's pretty darn good at guessing the probability right there on the "x-axis." 

 

  • Formula: Log Loss = - (1/N) * Σ[y * log(y_hat) + (1-y) * log(1-y_hat)]
  • Advantages: More informative than just accuracy, especially for probabilistic predictions.
  • Drawbacks: Can be heavily impacted by a single wrong prediction with high confidence.
  • When to use: When you're interested in the probabilistic performance of classification models.

Python Example Code

from sklearn.metrics import log_loss

loss = log_loss(y_true, y_probs)

11. Jaccard Index

  • What is it? It measures the similarity between predicted and actual labels in a classification problem.

So, you've built a machine learning model to spot fake transactions. 

The Jaccard Index helps you measure how close your model's guesses are to reality.

You've got your predictions and the actual outcomes. 

Now, let's imagine your model said three transactions were fraudulent, and it turned out only two of them were. 

The Jaccard Index would look at these lists and tell you how well they match. The closer they match, the higher the Jaccard Index.

It's like a handy tool to check how good your model is at spotting the right labels. 

If the Jaccard Index is high, it means your model's doing a decent job of identifying those fraudulent transactions, even if it's not perfect.

  • Formula: Jaccard Index = (Intersection of Actual and Predicted) / (Union of Actual and Predicted)
  • Advantages: Directly interpretable as a measure of overlap between two sets. Useful for multi-label classification.
  • Drawbacks: Sensitive to small sample sizes. May not be suitable when there are huge imbalances between classes.
  • When to use: In classification problems where set similarity is of interest.

Python Example Code

from sklearn.metrics import jaccard_score

j_score = jaccard_score(y_true, y_pred)

12. Gini Coefficient

  • What is it? Measures inequality among values of a frequency distribution. 

Imagine you're trying to sort out your stuff, like books or toys. You want to make sure each group you make has a similar type of item.

The Gini Coefficient is like checking how mixed up your groups are. If everything is perfectly sorted (all books together, all toys together), the Gini Coefficient is low because there's no mixing.

Now, with the fraud transaction example: Your model is splitting transactions into groups, deciding which ones are more likely to be fraud and which ones aren't. The Gini Coefficient checks if these groups are neatly separated based on their legitimacy.

If your model is really good at separating these transactions (like putting all the fraudulent ones in one group and legitimate ones in another), the Gini Coefficient will be low. 

But if there's a mix-up, meaning both types are jumbled in each group, the Gini Coefficient goes up.

It's like a measure of how well your model can cleanly split the transactions based on their legitimacy, kind of like sorting your stuff into neat piles.

 

  • Formula: Gini(p) = 1 - Σ(p_i^2)
  • Advantages: Can help in deciding the best feature upon which to split in tree-based algorithms.
  • Drawbacks: Less sensitive to changes in node probabilities than other metrics, like entropy.
  • When to use: Primarily in the context of decision trees and random forests to evaluate the quality of a split.

Python Example Code

The Gini coefficient is typically computed internally in tree-based algorithms but can be computed manually using the formula provided.

Next, we’ll go through the process of choosing the correct evaluation metric for your particular needs.

How Do You Choose the Best Model Evaluation Metrics?

Picking the right metric to judge your machine-learning model is like grabbing the perfect spices for a recipe. 

Your final result heavily relies on making the right choice. 

So how do you sift through the options and sprinkle just the right stuff?

 Let’s take a closer look at the strategies of selection:

Strategy #1 : Understanding the Domain

First off, dive into the realm of your problem. 

For “continuous values” predictions..

like guessing the ups and downs of the stock market or.. 

forecasting weather temperatures, 

you might want to buddy up with metrics like MSE or MAE.

Now, when you're tackling binary classification problems..

Like diagnosing diseases, it's essential to go for metrics that have your back when it comes to false positives and false negatives. 

Precision, recall, and the F1 score can be your pals here.

Strategy #2 : Thinking About the 'What Ifs'

Ponder over the cost of goofing up. 

In the world of medical diagnosis, missing out on detecting a disease (hello, false negatives) can be a major bummer, making “recall” stand out as a crucial metric. 

On the flip side, in the universe of email, marking a real email as spam (oh no, false positives) can be quite the hassle, making “precision” your go-to model metric.

Strategy #3 : Is Your Data Doing a Balancing Act?

Check if your dataset is playing fair or if one side is tipping the scales. 

 For those tricky imbalanced datasets, straight-up accuracy might just throw you off. 

 The AUC-ROC curve, F1 score, or Jaccard Index could offer a clearer picture.

Strategy #4 : Expert Advice

 Have a chat with the domain experts. Their wisdom can light the path to choosing the right metrics.

Strategy #5 : Mix of Different Metrics 

Don't put all your eggs in one basket. A mix of different metrics can give you a 360-view of your model's performance.

As you can see, model evaluation metrics are the compass that guides machine learning practitioners like you and me!

They not only measure the performance but also illuminate areas for improvement, ensuring that the models we build are both robust and reliable. 

Properly selecting and interpreting these metrics ensures that the solutions we use have the intended impact in real-world scenarios.

 

The Data Science Aspirant's 90-Day Proven Roadmap

Get INSTANT ACCESS to This Proven Roadmap To Become a Data Scientist in 90 Days,

Even Without Prior Data Science Experience - Guaranteed.

 


Conclusion

Unlocking the true potential of machine learning models is like mastering the controls of a complex spacecraft. Let's ensure we're ready for takeoff by revisiting the critical checkpoints.

  • Model Evaluation Metrics: First, we established the analogy of machine learning models as students learning from data and how model evaluation metrics resemble grades from tests, measuring the model's predictive capabilities.
  • Types of Models and Corresponding Metrics: Next, we discussed the diverse types of models, likening them to various tasks such as weather predictions or image classifications. Different models require different evaluation metrics—akin to timing a sprinter versus assessing a high jumper.
  • Examples of Model Evaluation in Real-World Scenarios: We illustrated the significance of model evaluation metrics in practical applications like Netflix's content suggestions, health tech wearables such as Fitbit, personalized recommendations on Amazon, credit scoring in finance, and safety considerations in autonomous vehicles.
  • Understanding Confusion Matrix and Model Performance Metrics: We delved into technical aspects, introducing the Confusion Matrix—a critical tool in classification problems. Then, we outlined 12 essential model performance metrics like MSE, MAE, Accuracy, Specificity, Precision, Recall, F1 Score, AUC, ROC Curve, Log Loss, Jaccard Index, and Gini Coefficient.
  • Strategies for Metric Selection: We explored strategies for choosing the most suitable metric based on 
    • domain understanding, 
    • cost implications of errors, 
    • dataset balance, 
    • expert advice, and 
    • the recommendation to use a mix of metrics for a comprehensive evaluation. 

So, did you learn something new today? Let's test it out!

 

Question for You

In a scenario where both false positives and false negatives have significant consequences, which metric would be especially useful?

  1. A) Mean Squared Error (MSE)
  2. B) Classification Accuracy
  3. C) F1 Score
  4. D) Mean Absolute Error (MAE)

Let me know your answer in the comments below!

Stay connected with weekly strategy emails!

Join our mailing list & be the first to receive blogs like this to your inbox & much more.

Don't worry, your information will not be shared.