Last active
November 22, 2022 19:43
-
-
Save antoinebrl/a36f1f360cb6f3375e5a96e15c0c8e06 to your computer and use it in GitHub Desktop.
Vectorized K-nearest neighbors (no for-loop)
This file contains hidden or 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 scipy.stats import mode | |
def euclidean_distance(x1, x2): | |
# x1 array of shape (N, D) | |
# x2 array of shape (N', D) | |
# output pairwise distances, array of shape (N, N') | |
return np.linalg.norm(x1[:, np.newaxis] - x2, axis=1) | |
class KNN: | |
def __init__(self, k=3): | |
self.k = k | |
def fit(self, x, y): | |
self.x_train = x | |
self.y_train = y | |
def predict(self, x): | |
distances = euclidean_distance(x, self.x_train) | |
topk_idxs = np.argsort(distances, axis=1)[:, :self.k] | |
topk_labels = self.y_train[topk_idxs] | |
return mode(topk_labels, axis=1) | |
if __name__ == "__main__": | |
n1 = 24 | |
n2 = 7 | |
d = 3 | |
k = 3 | |
x = np.random.rand(n1, d) | |
y = np.random.randint(0, 2, n1) | |
knn = KNN(k) | |
knn.fit(x, y) | |
x = np.random.rand(n2, d) | |
print(knn.predict(x)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment