The purpose of this project was to present the inner workings of K Nearest Neighbor for better understanding using Python.
First, We need to install the numpy and Collection package using "Pip install numpy " and "Pip install Collection" in Command prompt. This line of code will import numpy and Collection.
KNN is a lazy learner, so we need to specify the number of the nearest neighbors. In the fit method, we will accept training lables and data.
self.k = k
self.X_train = X
self.Y_train = y
we need a helper function to calculate the similarity between two neighbors.
#Calculating euclidean distance
We'll perform the following steps for every element:
1) calculate the euclidean distance between with unknown class and known class
2) select neighbors with the smallest euclidean distance
3) choose the most common class.
y_predict = 
#We need to predict every value individually
for x in X_predict:
distances = [self._euclidean_distance(x, x_train) for x_train in self.X_train]
nearest_neighbor_indx = np.argsort(distances)[:self.k]
nearest_neighbor_labels = [self.Y_train[label_indx] for label_indx in nearest_neighbor_indx]
nearest_neighbor = Counter(nearest_neighbor_labels).most_common()
Submitted by Akhilesh Ketkar (AkhileshKetkar)
Download packets of source code on Coders Packet