Question: Write a function nearest_neighbors which takes in three arguments: a N x D Numpy array, a 1-dimensional array of length D point, and a distance

Write a function nearest_neighbors which takes in three arguments: a N x D Numpy array, a 1-dimensional array of length D point, and a distance threshold dist. It should return an M x D Numpy (M <= N) array of all points in the array which are within Euclidean distance dist of point. The output points should be sorted by the distance from the corresponding point, with closest elements coming first. (You will receive partial credit if the correct points are output but not sorted.)

Example (on Nx3 array):

array = np.array([[1, 1, 1], [2, 3, 5], [0, 1, 1], [1.5, 1, 1], [10, 9, 9]]) target_pt = np.array([0, 1, 1]) nearest_neighbors(array, target_pt, 3.0) # Should return [[0, 1, 1], [1, 1, 1], [1.5, 1, 1]]

Hint: You may be interested in the argsort function, which produces a Numpy array of indexes which will sort a given array.

For this assignment, you should not use loops! You should be using vectorized operations. Functional iterators are also not allowed. The following operations will be deducted: for, while, comprehensions (like [x for x in range(10)], map(), and filter().

use python 3.8 thank you!

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!