Demo entry 6717741



Submitted by anonymous on Feb 28, 2018 at 02:18
Language: Python 3. Code size: 1.9 kB.

import keras
import sklearn
import sklearn.datasets
import sklearn.cross_validation
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
from sklearn.model_selection import cross_val_score
import sklearn.decomposition
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, cohen_kappa_score
from sklearn.cross_validation import train_test_split
from keras.models import load_model
from sklearn.neighbors import KNeighborsClassifier
from sklearn import neighbors, datasets
from sklearn import metrics
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import numpy as np
from sklearn.neighbors import kneighbors_graph

image_data = np.load('images.npy')
label_data = np.load('labels.npy')

x = image_data.reshape([6500,784])
y = keras.utils.to_categorical(label_data, num_classes=10)

#Splitting Data into train, val, and test

x_train, x_rest, y_train, y_rest = train_test_split(x, y, test_size=0.40)
x_val, x_test, y_val, y_test = train_test_split(x_rest, y_rest, test_size=0.625)

# Model Template

acc = []
bcc = []

for k in range(2000, 2001):
	clf = neighbors.KNeighborsClassifier(n_neighbors = k), y_train)

	y_expect = y_test
	y_pred = clf.predict(x_test)
	data = metrics.classification_report(y_expect, y_pred)

	#print(clf.kneighbors_graph(n_neighbors=3, mode='connectivity'))

	print(accuracy_score(y_test, y_pred))

	print('\nConfusion Matrix')
	print(confusion_matrix(y_test.argmax(axis=1), y_pred.argmax(axis=1)))
	acc.append(accuracy_score(y_expect, y_pred))

plt.plot(bcc, acc)
plt.xlabel('Value of K')
plt.ylabel('Accuracy of KNN')

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).