Reputation: 407
I have 8 points in a list and I need to calculate euclidean distance between all possible pairs. I could write a for loop and go on calculating the distance but is there a better approach / method available with python / numpy / others?
Coordinates points:
x1,y1 , x2,y2, x3,y3, .... .. ,, .. ,xn,yn
Upvotes: 2
Views: 3489
Reputation: 1992
Yes, you can use euclidean_distances
from sklearn. Usage (from the manual)
>>> from sklearn.metrics.pairwise import euclidean_distances
>>> X = [[0, 1], [1, 1]]
>>> # distance between rows of X
>>> euclidean_distances(X, X)
array([[0., 1.],
[1., 0.]])
>>> # get distance to origin
>>> euclidean_distances(X, [[0, 0]])
array([[1. ],
[1.41421356]])
Upvotes: 1