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.
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).
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.