How to Find Precision in Python Using Sklearn?

Precision is an evaluation metric used in Python and Machine Learning to evaluate the performance of a classification model. A classification model is a model that is used to predict the output category among the listed ones. Some of the well-known classification algorithms are KNN, SVM, decision trees, and many others. In this post, we are going to discuss how to find precision in Python using the Sklearn module and using a custom method.

how to find the precision in python

How to Find Precision in Python?

Precision is an evaluation metric of the Machine Learning model, specifically the classification model. It helps to find the quality of positive predictions of the model. The formula of precision is very simple. It is the total number of true positives divided by the total number of true positive predictions. Here is the simple formula for Precision in Python.

Precision: TruePositives / (TruePositives + FalsePositives)

  • True Positive: A true positive is an outcome where the model correctly predicts the positive class.
  • False Positive: A false positive is an outcome where the model incorrectly predicts the positive class.

Why Choose Precision Over Accuracy?

Well, in some cases we have to give more preference to precision than accuracy for many reasons. Here we will list some of the features of precision and based on them you decide when to use it over accuracy.

  • Its emphasis is on positive predictions.
  • Used for both binary and multi-class classification
  • It gives importance to quality rather than quantity.
  • It is affected by an imbalanced dataset so make sure your data is balanced.
  • It is a complementary metric.
  • It is sensitive to the class of distribution.
  • Its interpretation is very easy.
  • Gives a value between 0 and 1.

Calculate the Precision in Sklearn

Sklearn is a Python module that is used widely among Machine Learning developers to train models and evaluate them. Before going to use the module, you need to install it using the pip command.

Once the installation is complete, you can then import the module and the precision method as shown here.

# importing the precision_score
from sklearn.mterics import precision_score

# finding precision score
print(precision_score(predictions, actual))

Notice that the precision method in the Sklearn module takes two mandatory arguments. You need to provide the actual and the predicted values so that it will compare them and give you the precision score.

Summary

Precision is an evaluation metric in Python that is used to see how precise the predictions are in the case of a classification model. The formula to find the Precision in Python is very simple which is TruePositives / (TruePositives + FalsePositives). Here, we discussed how we can find the precision in Python using the Sklearn module.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top