Skip to content

Instantly share code, notes, and snippets.

@JediKnightChan
Created April 17, 2018 21:20
Show Gist options
  • Save JediKnightChan/9641393f0f400f19e6331f26b6f8d1b5 to your computer and use it in GitHub Desktop.
Save JediKnightChan/9641393f0f400f19e6331f26b6f8d1b5 to your computer and use it in GitHub Desktop.
from sklearn import datasets
from sklearn.decomposition import PCA
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
import matplotlib.pyplot as plt
import numpy as np
digits = datasets.load_digits()
X = digits["data"]
Y = digits["target"]
pca = PCA(n_components=2)
X_pca = pca.fit_transform(X)
train_ind, test_ind, = train_test_split(range(len(X)), test_size = 0.25)
X_train = X[train_ind]
Y_train = Y[train_ind]
X_test = X[test_ind]
Y_test = Y[test_ind]
classifier = KNeighborsClassifier(n_neighbors=20)
classifier.fit(X_train, Y_train)
predicted = classifier.predict(X_test)
labels = np.unique(Y)
colors = ["violet", "blue", "red"]
for color, label in zip(colors, labels):
plt.plot(X_pca[train_ind][Y_train == label][:, 0], X_pca[train_ind][Y_train == label][:, 1], "o", c=color)
for color, label in zip(colors, labels):
plt.plot(X_pca[test_ind][predicted == label][:, 0], X_pca[test_ind][predicted == label][:, 1], "x", c=color)
#plt.show()
predict = classifier.predict(X_test)
correct_num = len(list(filter(lambda el: el[0] == el[1], zip(Y_test, predicted))))
print("Total:")
print(correct_num/len(predicted))
for label in labels:
print("Class {}:".format(label))
y_for_label = list(filter(lambda x: x == label, Y_test))
predicted_y_for_label = list(filter(lambda x: x == label, predicted))
len_ = len(y_for_label)
correct_ = len(list(filter(lambda el: el[0] == el[1], zip(y_for_label, predicted_y_for_label))))
print(correct_ / len_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment