Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions knnAlgorithem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#Importing the required modules
import numpy as np
from scipy.stats import mode

#Euclidean Distance
def eucledian(p1,p2):
dist = np.sqrt(np.sum((p1-p2)**2))
return dist

#Function to calculate KNN
def predict(x_train, y , x_input, k):
op_labels = []

#Loop through the Datapoints to be classified
for item in x_input:

#Array to store distances
point_dist = []

#Loop through each training Data
for j in range(len(x_train)):
distances = eucledian(np.array(x_train[j,:]) , item)
#Calculating the distance
point_dist.append(distances)
point_dist = np.array(point_dist)

#Sorting the array while preserving the index
#Keeping the first K datapoints
dist = np.argsort(point_dist)[:k]

#Labels of the K datapoints from above
labels = y[dist]

#Majority voting
lab = mode(labels)
lab = lab.mode[0]
op_labels.append(lab)

return op_labels