Write KNN from scratch

Created
TagsML Coding

https://towardsdatascience.com/create-your-own-k-nearest-neighbors-algorithm-in-python-eb7093fc6339

import numpy as np
class KnnClassifier(object):
    def __init__(self, X, y):
        self.samples = X
        self.labels = y

    def classify(self, point, k = 3):

        dist = np.array([self.L2(point, s) for s in self.samples])

        idx = dist.argsort()[:k]

        votes = {}
        for id in idx:
            label = self.labels[id]
            votes.setdefault(label, 0)
            votes[label] += 1
        return max(votes.items(), key=lambda x: x[1])[0]
    
    def L2(self, point1, point2):
        return np.sqrt(np.sum((point1 - point2)**2))
    

    def L1(self, point1, point2):
        return np.sum(np.abs(point1 - point2))

yf implement, tested .

import numpy as np
class KnnClassifier():
    def __init__(self, X, y):
        self.samples = [np.array(X[i]) for i in range(len(X))]
        self.labels = y
    

    def classify(self, point, k = 3):
        point = np.array(point)
        list_dist_y = [[self.l2norm(point, self.samples[i]), self.labels[i]] for i in range(len(self.samples))]
        sorted_dist_y = sorted(list_dist_y)[:k]
        votes = [item[1] for item in sorted_dist_y]
        count = {}
        res = votes[0]
        for vote in votes:
            if vote in count:
                count[vote] += 1
            else:
                count[vote] = 1
            if count[vote] > count[res]:
                res = vote
        return res

    def l2norm(self, point1, point2):
        return np.sqrt(np.sum((point1 - point2)**2))

    def l1norm(self, point1, point2):
        return np.sum(np.abs(point1 - point2))

# test
X = [[1,1],[2,2],[3,3],[-3,-3],[-4,-4]]
y = [0, 0, 0, 1, 1]
myknn = KnnClassifier(X = X, y = y)
point = [2.5, 2.5]
print("result =", myknn.classify(point))
point = [-2.5, -2.5]
print("result =", myknn.classify(point))

yf implement, tested, no numpy

class KnnClassifier():
    def __init__(self, X, y):
        self.samples = X
        self.labels = y
    
    def classify(self, point, k = 3):
        list_dist_y = [[self.l2norm(point, self.samples[i]), self.labels[i]] for i in range(len(self.samples))]
        sorted_dist_y = sorted(list_dist_y)[:k]
        votes = [item[1] for item in sorted_dist_y]
        count = {}
        res = votes[0]
        for vote in votes:
            if vote in count:
                count[vote] += 1
            else:
                count[vote] = 1
            if count[vote] > count[res]:
                res = vote
        return res

    def l2norm(self, point1, point2):
        res = 0
        for i in range(len(point1)):
            res += point1[i] * point1[i] + point2[i] * point2[i]
        return res

    def l1norm(self, point1, point2):
        res = 0
        for i in range(len(point1)):
            res = max(res, abs(point1[i]- point2[i]))
        return res
# test
X = [[1,1],[2,2],[3,3],[-3,-3],[-4,-4]]
y = [0, 0, 0, 1, 1]
myknn = KnnClassifier(X = X, y = y)
point = [2.5, 2.5]
print("result =", myknn.classify(point))
point = [-2.5, -2.5]
print("result =", myknn.classify(point))