-2

I would like to find the euclidian distance between two numpy.ndarray.

lower_boundary = 0
upper_boundary = 1
n = 4 # dimension
sample_size = 3

np.random.seed(9001) # set the seed to yield reproducible results

X2 = np.random.uniform( low=lower_boundary, high=upper_boundary, size=(sample_size, n) )
Y2 = np.random.uniform( low=lower_boundary, high=upper_boundary, size=(sample_size, n) )

print( 'X2: ', X2 )
print( 'Y2: ', Y2 )
  1. How can i implement this calculation from scratch, by using np.sum and np.sqrt instead of importing euclidean_distances from sklearn.metrics.pairwise

Thanks for all

Thierry K.
  • 97
  • 1
  • 6
  • 1
    Do you know how to do this by hand? Say you have two 3x3 matrices. What is the formula for the distance between them? – Code-Apprentice Jul 26 '18 at 15:02
  • How do you define the distance between matrices of dimension greater than one? – Ben Jones Jul 26 '18 at 15:06
  • What have you tried? This is a fairly common problem, have you found any other stackoverflow questions that address it? – user2699 Jul 26 '18 at 15:23
  • 2
    Possible duplicate of [How can the Euclidean distance be calculated with NumPy?](https://stackoverflow.com/questions/1401712/how-can-the-euclidean-distance-be-calculated-with-numpy) – Ben Jones Jul 26 '18 at 15:24

1 Answers1

0

Eucledian Distance be D(i,j). Then D(i,j) corresponds to the pairwise distance between row i in X and row j in Y. In this case, the size of distance matrix will be 3 by 3.

final_sum=np.zeros([sample_size,sample_size])
for row_inX in range(0, sample_size):
for row_inY in range(0, sample_size):
    final_sum[row_inX][row_inY]= np.sqrt(np.sum((X2[row_inX]- Y2[row_inY])**2))
print(final_sum)