0

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
senad
  • 1
  • What have you tried so far? – user2653663 Apr 28 '19 at 12:57
  • I have tried using the argpartition function to sort the array and find the indices of the nearest values but I haven't gotten somewhere with it, I am trying to figure out how to combine distance function with it. – senad Apr 28 '19 at 16:01

1 Answers1

0

The process of rewriting for loops in Python is called vectorization. To solve this, you generally have to use indexes to retrieve the values in your matrix. I would suggest you to look at this questions: Vectorizing for loops NumPy vectorizing a for loop in numpy/scipy? vectorizing a for loop in python

enrico_ay
  • 76
  • 6