# Model Evaluation - Precision

First published on February 2, 2022

Last updated at April 15, 2022

3 minute read

Nathaniel Tjandra

Growth

## TLDR

In this Mage Academy lesson on model evaluation, we’ll learn how to calculate precision for your machine learning models.

### Glossary

Definition

Calculation

How to code

### Definition

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.

High precision

While a low precision means that the model’s results are scattered from each other. This distance can also be described as

uncertainty

.

Low precision

### Calculation

The number of positive predictions a model gets correct is represented by quadrant 1 in our

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

Example data

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
5
```

true_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
5
```

pos = 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
3
```

print("True Positives :", true_pos)
print("Total Positives:", pos)
print("Precision:", true_pos / pos)

SKLearn or SciKitLearn, is a Python library that handles calculating the precision of a model using the method

precision_score.

```
1
2
3
4
```

from 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%.

## Start building for free

No need for a credit card to get started.

Trying out Mage to build ranking models won’t cost a cent.

No need for a credit card to get started. Trying out Mage to build ranking models won’t cost a cent.