Metric Calculation

Tutorial 2 of 4

1. Introduction

  • Goal of this tutorial: This tutorial aims to guide you through the calculation of various evaluation metrics for machine learning models, namely Accuracy, Precision, Recall, and F1 score.
  • What you will learn: By the end of this tutorial, you will understand these key metrics and how to calculate them using Python. You will also learn how to interpret these metrics to evaluate the model performance.
  • Prerequisites: Basic knowledge of Python and an understanding of machine learning concepts will be beneficial.

2. Step-by-Step Guide

  • Accuracy is the most intuitive performance measure. It is simply a ratio of correctly predicted observation to the total observations.
  • Precision is the ratio of correctly predicted positive observations to the total predicted positive observations. High precision relates to the low false positive rate.
  • Recall (Sensitivity) - the ratio of correctly predicted positive observations to the all observations in actual class.
  • F1 Score is the weighted average of Precision and Recall. This score tries to find the balance between precision and recall.

3. Code Examples

Here are some examples of how to calculate these metrics using Python and the scikit-learn library:

from sklearn import metrics

# actual values
y_true = [1, 1, 0, 1, 0, 0, 1, 0, 0, 1]
# predicted values
y_pred = [1, 0, 0, 1, 0, 0, 1, 1, 1, 0]

# print metrics
print("Accuracy:",metrics.accuracy_score(y_true, y_pred))
print("Precision:",metrics.precision_score(y_true, y_pred))
print("Recall:",metrics.recall_score(y_true, y_pred))
print("F1 Score:",metrics.f1_score(y_true, y_pred))

This code snippet calculates the Accuracy, Precision, Recall and F1 Score of a binary classification problem. The metrics module from the sklearn library provides the necessary functions for calculating these metrics.

4. Summary

In this tutorial, we covered the calculation of several key metrics used to evaluate machine learning models: Accuracy, Precision, Recall and F1 Score. We also provided Python code examples for calculating these metrics using the scikit-learn library. As next steps, consider learning about other evaluation metrics such as the Area Under the ROC curve (AUC-ROC).

5. Practice Exercises

  1. Exercise 1: Given the actual values [1, 0, 0, 1, 1, 1, 0, 1, 1, 0] and predicted values [0, 0, 1, 1, 1, 0, 1, 1, 1, 0], calculate the Accuracy, Precision, Recall and F1 Score.
  2. Exercise 2: Write a function in Python that takes in actual and predicted values as input and outputs the Accuracy, Precision, Recall and F1 Score.
  3. Exercise 3: Using the function from Exercise 2, calculate the metrics for a different set of actual and predicted values.

Solutions:

Here are the solutions for the above exercises:

# Solution for Exercise 1:
y_true = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
y_pred = [0, 0, 1, 1, 1, 0, 1, 1, 1, 0]
print("Accuracy:",metrics.accuracy_score(y_true, y_pred))
print("Precision:",metrics.precision_score(y_true, y_pred))
print("Recall:",metrics.recall_score(y_true, y_pred))
print("F1 Score:",metrics.f1_score(y_true, y_pred))

# Solution for Exercise 2:
def calculate_metrics(y_true, y_pred):
    print("Accuracy:",metrics.accuracy_score(y_true, y_pred))
    print("Precision:",metrics.precision_score(y_true, y_pred))
    print("Recall:",metrics.recall_score(y_true, y_pred))
    print("F1 Score:",metrics.f1_score(y_true, y_pred))

# Solution for Exercise 3:
y_true = [0, 1, 1, 0, 0, 0, 1, 0, 1, 1]
y_pred = [0, 0, 1, 1, 0, 1, 0, 1, 0, 1]
calculate_metrics(y_true, y_pred)

The first solution computes the metrics for the given values. The second solution encapsulates the metric computation inside a function. The third solution uses the function to compute the metrics for a new set of actual and predicted values.