Skip to content

Instantly share code, notes, and snippets.

Last active Nov 3, 2015
What would you like to do?
kNN Classification of digits using scikit-learn
from sklearn.metrics import confusion_matrix, accuracy_score
from sklearn import cross_validation
# load sample dataset of digits
digits = datasets.load_digits()
# prepare datasets from training and for validation
X_train, X_test, y_train, y_test = cross_validation.train_test_split(,, test_size=0.4, random_state=0)
# runs the kNN classifier for even number of neighbors from 1 to 10
for n in range(1, 10, 2):
clf = neighbors.KNeighborsClassifier(n)
# instance based learning, y_train)
# our 'ground truth'
y_true = y_test
# predict
y_pred = clf.predict(X_test)
# learning metrics
cm = confusion_matrix(y_true, y_pred)
acc = accuracy_score(y_true, y_pred)
print "Neighbors: %d" % n
print "Confusion Matrix"
print cm
print "Accuracy score: %f" % accuracy_score(y_true, y_pred)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment