Last active
August 22, 2022 16:27
-
-
Save okanyenigun/00e41f564801014d7884b954cdb17812 to your computer and use it in GitHub Desktop.
knn scratch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from collections import Counter | |
class Knn: | |
def __init__(self, k=5): | |
self.k = k | |
def fit(self,X,y): | |
self.X_train = X | |
self.y_train = y | |
def predict(self, X): | |
y_pred = [self.__repredict(x) for x in X] | |
return np.array(y_pred) | |
def __repredict(self,x): | |
#distances | |
distances = [self.calculate_euclidean_distance(x, x_train) for x_train in self.X_train] | |
#sort | |
k_idx = np.argsort(distances)[: self.k] | |
#extract labels | |
labels = [self.y_train[i] for i in k_idx] | |
#vote | |
common = Counter(labels).most_common(1) | |
return common[0][0] | |
def calculate_euclidean_distance(self,x1,x2): | |
return np.sqrt(np.sum(x1-x2)**2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment