Fork me on GitHub

Digits Classification ExerciseΒΆ

A tutorial exercise regarding the use of classification techniques on the Digits dataset.

This exercise is used in the Classification part of the Supervised learning: predicting an output variable from high-dimensional observations section of the A tutorial on statistical-learning for scientific data processing.

Python source code:


from sklearn import datasets, neighbors, linear_model

digits = datasets.load_digits()
X_digits =
y_digits =

n_samples = len(X_digits)

X_train = X_digits[:.9 * n_samples]
y_train = y_digits[:.9 * n_samples]
X_test = X_digits[.9 * n_samples:]
y_test = y_digits[.9 * n_samples:]

knn = neighbors.KNeighborsClassifier()
logistic = linear_model.LogisticRegression()

print('KNN score: %f' %, y_train).score(X_test, y_test))
print('LogisticRegression score: %f'
      %, y_train).score(X_test, y_test))