0

I have a list of lists. I want to find the euclidean distance between all the pairs and itself and create a 2D numpy array. The distance between itself will have 0 in the place and the value when the pairs are different. Example of List of Lists:[[0, 42908],[1, 3],[1, 69],[1, 11],[0, 1379963888],[0, 1309937401],[0, 1],[0, 3],[0, 3],[0, 77]] The result I want is

  0 1 2 3 4 5 6 7 8
0 0 x x x x x x x x
1   0 x x x x x x x
2     0 x x x x x x 
3       0 x x x x x
4 .................
5 .................
6 .................
7 .................
8 .................

x represents the values of differences. The periods mean the result shall follow as it shows in the matrix. I need help with the code in python. The number of 0,1,2 etc in rows and columns define the inner list index.

user6565467
  • 49
  • 1
  • 9
  • 1
    You could use [https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html) to compute distance between all pairs. In your case Xa & Xb will be the same dataset. – DarrylG Jan 28 '20 at 02:30
  • Does this answer your question? [Fastest pairwise distance metric in python](https://stackoverflow.com/questions/20277982/fastest-pairwise-distance-metric-in-python) – Joe Jan 28 '20 at 06:13
  • You can use scipy.spatial.distance.pdist for that. It will return a condensed array as half of the combinations is redundant. But you can create the full square matrix by using scipy.spatial.distance.squareform. – Joe Jan 28 '20 at 06:13

2 Answers2

2

You can use numpy directly to calculate the distances:

pts = [[0, 42908],[1, 3],[1, 69],[1, 11],[0, 1379963888],[0, 1309937401],[0, 1],[0, 3],[0, 3],[0, 77]]
x = np.array([pt[0] for pt in pts])
y = np.array([pt[1] for pt in pts])
np.sqrt(np.square(x - x.reshape(-1,1)) + np.square(y - y.reshape(-1,1)))
BMW
  • 509
  • 3
  • 15
0

There is the excellent answer of BMW. Another possible solution that uses list comprehension is the following:

import numpy as np
a=[[0, 42908],[1, 3],[1, 69],[1, 11],[0, 1379963888],[0, 1309937401],[0, 1],[0, 3],[0, 3],[0, 77]]
# generate all the distances with a list comprehension
b=np.array([  ((a[i][0]-a[j][0])**2 + (a[i][1]-a[j][1])**2)**0.5 for i in range(len(a)) for j in range(i,len(a))])

n = len(b)
# generate the indexes of a upper triangular matrix
idx = np.triu_indices(n)
# initialize a matrix of n*n with zeros
matrix = np.zeros((n,n)).astype(int)
# assign to such matrix the results of b
matrix[idx] = b
Fabrizio
  • 927
  • 9
  • 20