How to Calculate Precision? - Classification Error Metric With Examples

Hey, folks! In this article, we will learn how to calculate precision in Python which is a Classification Error Metric.

So, let us begin!

What is Precision?

Let us understand the need for Error Metrics in Classification or Regression Algorithms.

Error Metrics helps us analyze the accuracy of a particular machine learning model over a dataset or set of data values. There are different error metrics for different types of machine learning algorithms.

Error metrics for Regression data–

  • Mean Square Error
  • Root Mean Square Error
  • R square
  • Adjusted R square, etc

Error metrics for Classification —

  • Confusion Matrix
  • Accuracy
  • Precision
  • Recall
  • f1 Score, etc

Precision identifies the correctly classified positive labels from the classified data values.

With Precision, we tend to measure the positive labels that are predicted correctly and are actually correct!

Have a look at the below formula–

Precision = True Positives / (True Positives + False Positives)

Here, the True Positive and False Positive values can be calculated through the Confusion Matrix. The value of Precision ranges between 0.0 to 1.0 respectively.

By True positive, we mean the values which are predicted as positive and are actually positive. While False Positive values are the values which are predicted as positive but are actually negative.

Let us now implement this in the upcoming section through an example.

Implementing Precision with a Classification Algorithm

We have tried implementing Precision as a measure with Decision Tree Algorithms.

Let us start implementing the same!!

In this example, we have used Bank Loan Defaulter dataset. This problem refers to the prediction of the loan defaulters from the bank’s dataset.

1. Load the dataset

Here, we have used Bank Loan Dataset and imported the same into the environment using pandas.read_csv() function.

2. Splitting the dataset

Splitting of the dataset into training and testing set is performed using train_test_split() function as shown below–

3. Defining Error Metrics

We have defined the Confusion Matrix and Precision calculation to be used for the evaluation of the model.

4. Modelling

We have applied the Decision Tree Algorithm to identify the loan defaulters from the data.

5. Evaluation of model

Finally, we have evaluated the model by calling the defined confusion matrix and precision format.


So, the output states that 25% of the values predicted as positive by the model are actually positive.


By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question. Till then, Happy Learning! 🙂

By admin

Leave a Reply

%d bloggers like this: