Assuming you have some basic understandment of numpy, this is some old code of mine for a KNN_classifier, anyway if you have any trouble using it or understanding it i can kindly explain it to you tommorow when i'll have some free time in case no one responds till then.
import numpy as np
from sklearn.metrics import accuracy_score as accuracy
class Knn_classifier:
def __init__(self, train_images, train_labels):
self.train_images = train_images
self.train_labels = train_labels
def classify_image(self, test_image, num_neighbors=3, metric='l2'):
if metric == 'l2':
distances = np.sqrt(np.sum(
np.square(self.train_images - test_image),
axis = 1
))
indexes = np.argsort(distances)
indexes = indexes[:num_neighbors]
labels = self.train_labels[indexes]
label = np.argmax(np.bincount(labels))
else:
distances = np.sum(np.abs(self.train_images - test_image),axis = 1)
indexes = np.argsort(distances)
indexes = indexes[:num_neighbors]
labels = self.train_labels[indexes]
label = np.argmax(np.bincount(labels))
return label
def classify_images(self, test_images, num_neighbors=3, metric='l2'):
# write your code here
labels = []
for image in test_images:
labels.append(self.classify_image(image,num_neighbors,metric))
return labels
def accuracy_score(self,predicted, ground_truth):
return accuracy(predicted, ground_truth)*100
train_images = np.load('data/train_images.npy') # load training images
train_labels = np.load('data/train_labels.npy') # load training labels
test_images = np.load('data/test_images.npy') # load testing images
test_labels = np.load('data/test_labels.npy') # load testing labels
knn = Knn_classifier(train_images, train_labels)
predicted = knn.classify_images(test_images, metric='l1')
knn.accuracy_score(predicted,test_labels)
The shapes of my train.images and train_labels we're (1000, 784) and
(1000,)