In this Mage Academy lesson on model evaluation, we’ll learn how to calculate precision for your machine learning models.
How to code
Precision is a classification metric used to measure your model’s performance. It’s represented by the total number of true positives compared to the total number of predicted positives.
Precision shows how far off each individual “prediction” is. A high precision means that predictions are relatively close to each other.
While a low precision means that the model’s results are scattered from each other. This distance can also be described as
The number of positive predictions a model gets correct is represented by quadrant 1 in ourConfusion Matrix
. While the total number of predicted positives will be quadrants 1 and 2 (True and False Positives).
Using this example, we can calculate it as (515 / 515+5). Hence we get an precision of 515/520 = 99.03%
How to code
Precision can be calculated by scratch or using a library called SKLearn.
Let’s start by calculating the number of true positives. This is when the values match and the result is positive. In our example, 1 will be positive and 0 will be negative.
To calculate the total number of true positives, we’ll go through the values and see when the result matched and was 1 (positive).
1 2 3 4 5true_pos = 0 for i in range(0, len(y_pred)): # Is a match and positive if (y_pred[i] == y_truei]) and (y_true[i] == 1) true_pos += 1
Next, we’ll calculate when our predictions returned positive.
1 2 3 4 5pos = 0 for i in range(0, len(y_pred)): # Prediction is positive if (y_pred[i] == 1) pos += 1
Finally, we’ll divide the true positives by total positives to get our precision.
1 2 3print("True Positives :", true_pos) print("Total Positives:", pos) print("Precision:", true_pos / pos)
1 2 3 4from sklearn.metrics import precision_score y_pred = [1, 0, 1, 0 ] y_true = [0, 1, 1, 0] precision_score(y_true, y_pred)
Here we get the same result, a precision score of 0.5 or 50%.