how to measure the accuracy of knn classifier in python

user1946217 picture user1946217 · Apr 4, 2013 · Viewed 26k times · Source

I have used knn to classify my dataset. But I do not know how to measure the accuracy of the trained classifier. Does scikit have any inbuilt function to check accuracy of knn classifier?

from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier()
knn.fit(training, train_label)    
predicted = knn.predict(testing)

Appreciate all the help. Thanks

Answer

Fred Foo picture Fred Foo · Apr 4, 2013

Use sklearn.metrics.accuracy_score:

acc = accuracy_score(test_label, predicted)