Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find out the accuracy?

I've wondered if there is a function in sklearn which corresponds to the accuracy(difference between actual and predicted data) and how to print it out?

from sklearn import datasets 
iris = datasets.load_iris()
from sklearn.naive_bayes import GaussianNB
naive_classifier= GaussianNB()
y =naive_classifier.fit(iris.data, iris.target).predict(iris.data)
pr=naive_classifier.predict(iris.data)
like image 600
Andrew 76868 Avatar asked Feb 26 '17 16:02

Andrew 76868


People also ask

What is the formula for accuracy of a model?

We calculate accuracy by dividing the number of correct predictions (the corresponding diagonal in the matrix) by the total number of samples. The result tells us that our model achieved a 44% accuracy on this multiclass problem.


3 Answers

Most classifiers in scikit have an inbuilt score() function, in which you can input your X_test and y_test and it will output the appropriate metric for that estimator. For classification estimators it is mostly 'mean accuracy'.

Also sklearn.metrics have many functions available which will output different metrics like accuracy, precision, recall etc.

For your specific question you need accuracy_score

from sklearn.metrics import accuracy_score
score = accuracy_score(iris.target, pr)
like image 112
Vivek Kumar Avatar answered Oct 23 '22 13:10

Vivek Kumar


You can use accuracy_score, find documentation here.

Implement like this -

from sklearn.metrics import accuracy_score
accuracy = accuracy_score(prediction, labels_test)

This will return a float value. The float value describes (number of points classified correctly) / (total number of points in your test set)

like image 6
Ananth Avatar answered Oct 23 '22 15:10

Ananth


You have to import accuracy_score from sklearn.metrics. It should be like below,

from sklearn.metrics import accuracy_score
print accuracy_score(predictions,test set of labels)

The formula for accuracy is:

Number of points classified correctly / all the points in test set

like image 2
user3318064 Avatar answered Oct 23 '22 14:10

user3318064