15

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)
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
Andrew 76868
  • 394
  • 1
  • 4
  • 14
  • In your case y and pr are same, ie predicted from the classifier on same data. To find the accuracy, you should not use same data in training and predicting – Vivek Kumar Feb 26 '17 at 17:18

7 Answers7

17

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)
radtek
  • 34,210
  • 11
  • 144
  • 111
Vivek Kumar
  • 35,217
  • 8
  • 109
  • 132
6

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)

Ananth
  • 2,597
  • 1
  • 29
  • 39
3

First you need to import the metrics from sklearn and in metrics you need to import the accuracy_score
Then you can get the accuracy score

The accuracy_score formula is
accuracy_score=correct_predictions/No of Predictions

from sklearn.metrics import accuracy_score
accuracy_score(y_actual,y_predicted)

PS. It works great for classification techniques

Varun Kumar
  • 153
  • 11
2

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

Miraj50
  • 4,257
  • 1
  • 21
  • 34
user3318064
  • 59
  • 1
  • 8
2

You can use score() function in GaussianNB directly. In this way you don't need to predict labels and then calculate accuracy.

from sklearn.naive_bayes import GaussianNB
gnb = GaussianNB()
gnb = gnb.fit(train_data, train_labels)
score = gnb.score(test_data, test_labels)
Majid A
  • 802
  • 9
  • 19
2

You can always use sklearn's metrics to get your model's accuracy you can either use accuracy_score(test_data,predictions) to get the difference between model's values and actual values, apart from this you can check for error rates in model metrics.mean_absolute_error(y_test,predictions) for mean absolute errors, metrics.mean_squared_error(y_test, predictions) for mean squared errors. etc

Shivam Paliya
  • 168
  • 2
  • 9
0

For Classification problems use "metrics.accuracy_score" and Regression use "metrics.r2_score".

CodeRed
  • 81
  • 6
  • (This post does not seem to provide a [quality answer](https://stackoverflow.com/help/how-to-answer) to the question. Please either edit your answer, or just post it as a comment to the question). – sɐunıɔןɐqɐp Jul 24 '18 at 07:35