I have a homework on nearest neighbor algorithm using python. I have a given code in pure python that contains a loop. I have to rewrite and configure the function only using numpy and without loops.
I have an unlabeled point u, that needs to be classified, a distance function and a training set (X, Y). The function that I have to write should return the label of the point that has the smallest distance to u.
Here is the function written in pure python that I have to rewrite:
def pynearest(u, X, Y, distance=pydistance):
xbest = None
ybest = None
dbest = float('inf')
for x, y in zip(X, Y):
d = distance(u, x)
if d < dbest:
ybest = y
xbest = x
dbest = d
return ybest