Quantcast
Channel: improve linear search for KNN efficiency w/ NumPY - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Answer by lejlot for improve linear search for KNN efficiency w/ NumPY

$
0
0

This is because you naively iterate over your data, and loops are slow in python. Instead, use sklearn pairwise distance functions, or even better - use sklearn efficient nearest neighbour search (like BallTree or KDTree). If you do not want to use sklearn, there is also a module in scipy. Finally you can do "matrix tricks" to compute this, since

|| x - y ||^2 = <x-y, x-y> = <x,x> +<y,y> - 2<x,y>

you can do (assuming your data is in matrix form given as X and Y):

X2 = (X**2).sum(axis=1).reshape((-1, 1))Y2 = (Y**2).sum(axis=1).reshape((1, -1))distances = np.sqrt(X2 + Y2 - 2*X.dot(Y.T))

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>