0% found this document useful (0 votes)
43 views5 pages

Get Accuracy of Predictions in Python With Sklearn - Data Science Parichay

The document discusses accuracy as a metric to evaluate classification models and shows how to calculate it from scratch and using Sklearn's accuracy_score function in Python. Accuracy tells the fraction of correctly classified labels. The document provides an example calculating accuracy from true and predicted labels both manually and with Sklearn.

Uploaded by

pratham agrasen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views5 pages

Get Accuracy of Predictions in Python With Sklearn - Data Science Parichay

The document discusses accuracy as a metric to evaluate classification models and shows how to calculate it from scratch and using Sklearn's accuracy_score function in Python. Accuracy tells the fraction of correctly classified labels. The document provides an example calculating accuracy from true and predicted labels both manually and with Sklearn.

Uploaded by

pratham agrasen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

This website uses cookies to improve your experience.

We'll assume you're okay with this, but you can


opt-out if you wish.

Cookie settings ACCEPT

Get Accuracy of Predictions in


Python with Sklearn

Measuring the performance of your model using the correct metric is a very
important step in the data science process. In this tutorial, we’ll look at how to
compute the accuracy of your predictions from scratch and with sklearn in
Python.

What is accuracy?
All Information Find Schools

Accuracy is one of the most common metrics used to judge the performance of
classification models. Accuracy tells us the fraction of labels correctly classified
by our model. For example, if out of 100 labels our model correctly classified 70,
we say that the model has an accuracy of 0.70

Accuracy score in Python from scratch


Let’s write a function in python to compute the accuracy of results given that we
have the true labels and the predicted labels from scratch.
This website uses cookies to improve your experience. We'll assume you're okay with this, but you can
1. def compute_accuracy(y_true, y_pred):
opt-out if you wish.
2. correct_predictions = 0
3. # iterate over each label and check
Cookie settings
4.
ACCEPT
for true, predicted in zip(y_true, y_pred):
5. if true == predicted:
6. correct_predictions += 1
7. # compute the accuracy
8. accuracy = correct_predictions/len(y_true)
9. return accuracy

The above function takes in values for the true labels and the predicted labels as
arguments and returns the accuracy score. Here, we count the total number of
correct predictions by iterating over each true and predicted label combination in
parallel and compute the accuracy by dividing the number of correct predictions
by the total labels.

Let’s try the above function on an example.

Highlighted programs for you


SPONSORED ℹ

Purdue Global
Cloud Computing &
Solutions LEARN MORE

Bachelor's

Information
Technology LEARN MORE

Bachelor's

Arizona State University Online


Information
Security LEARN MORE

Bachelor's

Biomedical
Engineering LEARN MORE
Bachelor's

Liberty University
Information
This website uses cookies to improve your experience. We'll assume you're okay with this, but you can
opt-out if you wish.
Systems LEARN MORE

CookieAssociate's
settings ACCEPT

STEM
Mathematics LEARN MORE
Associate's

1. # sample labels
2. y_true = [1, 0, 0, 1, 1]
3. y_pred = [1, 1, 1, 1, 1]
4. # get the accuracy
5. compute_accuracy(y_true, y_pred)

Output:

1. 0.6

We get 0.6 as the accuracy because three out of five predictions are correct.

Note that, the above function can be optimized by vectorizing the equality
computation using numpy arrays.

Accuracy using Sklearn’s accuracy_score()

You can also get the accuracy score in python using sklearn.metrics’
accuracy_score() function which takes in the true labels and the predicted labels
as arguments and returns the accuracy as a float value. sklearn.metrics comes
with a number of useful functions to compute common evaluation metrics. For
example, let’s compute the accuracy score on the same set of values as above
but this time with sklearn’s accuracy_score() function.

1. from sklearn.metrics import accuracy_score


2. accuracy_score(y_true, y_pred)

Output:
This website uses cookies to improve your experience. We'll assume you're okay with this, but you can
1. 0.6
opt-out if you wish.

Cookie settings ACCEPT


You can see that we get an accuracy of 0.6, the same as what we got above using
the scratch function. It is recommended that you use the sklearn’s function
as it not only is optimized for performance but also comes with additional
parameters that might be helpful.

For more on the sklearn’s accuracy_score() function, refer to its documentation.

Find Schools

With this, we come to the end of this tutorial. The code examples and results
presented in this tutorial have been implemented in a Jupyter Notebook with a
python (version 3.8.3) kernel having numpy version 0.23.1

Subscribe to our newsletter for more informative guides and tutorials.


We do not spam and you can opt out any time.

email address

SUBSCRIBE

Author

Piyush Raj
Piyush is a data professional passionate about using data to understand
things better and make informed decisions. He has experience working as
a Data Scientist in the consulting domain and holds an engineering degree
from IIT
This website Roorkee.
uses His
cookies to hobbies
improve yourinclude watching
experience. cricket,
We'll assume you'rereading,
okay withand
this, but you can
opt-out if you wish.
working on side projects.
Cookie settings ACCEPT
View all posts  

Tags

metrics sklearn

© Data Science Parichay


Contact • Disclaimer • Privacy Policy

You might also like