Finding the closest point(s) by ranking them
I Have a N x D
d开发者_JS百科imensional features, which I need to rank according to their distance to a 1 x D
dimensional vector. Any fast way to implement that in python without recursively apply argmin
?
Thanks!
Something really simple is Squared Euclidean Distance, and it's implementation would be like:
In []: F= randn(5, 3)
In []: t= randn(1, 3)
In []: ((F- t)** 2).sum(1)
Out[]: array([ 8.80512, 4.61693, 2.6002, 3.3293, 12.41800])
Where F
are the features and t
the target vector. Thus the ranking would be:
In []: ((F- t)** 2).sum(1).argsort()
Out[]: array([2, 3, 1, 0, 4])
However if you are able to describe more on your case, there might exist more suitable measures, like Mahalanobis distance.
精彩评论