Get Accuracy of Predictions in Python With Sklearn - Data Science Parichay
Get Accuracy of Predictions in Python With Sklearn - Data Science Parichay
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
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.
Purdue Global
Cloud Computing &
Solutions LEARN MORE
Bachelor's
Information
Technology LEARN MORE
Bachelor's
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.
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.
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.
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
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