From f68cce2a64eb4a46a459e0acef2c213d3ba5441c Mon Sep 17 00:00:00 2001 From: Rishabh <84309625+Rishabh4-2-2002@users.noreply.github.com> Date: Fri, 28 Oct 2022 19:15:48 +0530 Subject: [PATCH] Add files via upload --- knnAlgorithem.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 knnAlgorithem.py diff --git a/knnAlgorithem.py b/knnAlgorithem.py new file mode 100644 index 0000000..d76dc2b --- /dev/null +++ b/knnAlgorithem.py @@ -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 \ No newline at end of file