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