Exercise: $k$ Nearest Neighbors

Task 1: Write a function that finds, given a finite set of points in the plane and an integer $k$, the $k$ nearest neighbors of each point in the set using numpy's vectorized facilities.

Task 2: Apply your function to this set of points with $k=3$. Plot an arrow from each point to its $k$-nearest neighbors.

In [1]:
import numpy as np

P = np.array([[0,0], [0.2, 0.22], [0.1, -0.1],
              [1,1], [1.1, 0.9], [0.8, 0.9], [1.1, 0.63],
              [0.58, -0.1], [0.63, 0.1], [0.67, -0.3], [0.8,-0.23],
              [0.8, 0.6]])
k = 3