This is a homework of Computer Vision class(2017) of Feifei Li, CS231n, Stanford University. I prefer to focus on single task of every assignment in one blog, so I will display my work for task1 in assignment1: create a function or class for K Nearest Neighbors algorithm by python and numpy library, here is my code.
import numpy as np
class KNN_classifier:
def __init__(self):
pass
def KNN_train(self, X, Y, k):
"""
X is trainning set attributes with size N*D
Y is training set class lable with size N*1
k is an integer of neighbors number when do classification
"""
self.X_train = X
self.Y_train = Y
self.k = k
sample_num = self.X_train.shape[0]
Dis_train = np.zeros(sample_num, sample_num)
K_neighbors = np.zeros(1, self.k)
Lable_train = np.zeros(sample_num, 1)
Correct_train = 0
Acc_train = 0.0
# calculate the distance of ith sample with all samples
for i in range(sample_num):
for j in range(sample_num):
Dis_train[i, j] = np.abs(self.X_train[i, :], self.X_train[j, :])
# sort the distance of ith sample by increasing
sort_INC_index = np.argsort(Dis_train[i, :])
# get the K closet neighbors' lable
for c in range(self.k):
K_neighbors[1, c] = self.Y_train[sort_INC_index[c], 1]
# decide ith sample's lable by it's neighbors
Lable_train[i, 1] = np.mode(K_neighbors)
# estimate k value by classfy accuracy
if(Lable_train[i, 1] == self.Y_train[i, 1]):
Correct_train += 1
# get training set accuracy in k neighbors
Acc_train = Correct_train / sample_num
return self.k, Acc_train
def KNN_test(self, x):
"""
x is sample to be classified with size M*D
"""
sample_num = x.shape[0]
test_space = self.X_train.shape[0]
Dis_test = np.zeros(sample_num, test_space)
K_neighbors = np.zeros(1, self.k)
Lable_test = np.zeros(sample_num, 1)
# calculate the distance of ith sample in test space
for i in range(sample_num):
for j in range(test_space):
Dis_train[i, j] = np.abs(x[i, :], self.X_train[j, :])
# sort the distance of ith sample by increasing
sort_INC_index = np.argsort(Dis_train[i, :])
# get the K closet neighbors' lable
for c in range(k):
K_neighbors[1, c] = self.Y_train[sort_INC_index[c], 1]
# decide ith sample's lable by it's neighbors
Lable_test[i, 1] = np.mode(K_neighbors)
return Lable_test