Multidimensional Euclidean distance in Python

I want to calculate the Euclidean distance in several dimensions (24 measurements) between two arrays. I am using Numpy-Scipy.

Here is my code:

import numpy,scipy;

A=numpy.array([116.629, 7192.6, 4535.66, 279714, 176404, 443608, 295522, 1.18399e+07, 7.74233e+06, 2.85839e+08, 2.30168e+08, 5.6919e+08, 168989, 7.48866e+06, 1.45261e+06, 7.49496e+07, 2.13295e+07, 3.74361e+08, 54.5, 3349.39, 262.614, 16175.8, 3693.79, 205865]);

B=numpy.array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 151246, 6795630, 4566625, 2.0355328e+08, 1.4250515e+08, 3.2699482e+08, 95635, 4470961, 589043, 29729866, 6124073, 222.3]);

However, I used scipy.spatial.distance.cdist(A[numpy.newaxis,:],B,'euclidean')to calculate the Euclidan distance.

But it gave me a mistake

raise ValueError('XB must be a 2-dimensional array.');

I don’t seem to understand this.

I looked scipy.spatial.distance.pdistbut do not understand how to use it?

Is there any other better way to do this?

+8
source share
6 answers

Maybe ? scipy.spatial.distance.euclidean

Examples

>>> from scipy.spatial import distance
>>> distance.euclidean([1, 0, 0], [0, 1, 0])
1.4142135623730951
>>> distance.euclidean([1, 1, 0], [0, 1, 0])
1.0
+13
source

Use

numpy.sqrt(numpy.sum((A - B)**2))

or more simply

numpy.linalg.norm(A - B)
+11
source

A B - 2 24-D . scipy.spatial.distance.euclidean.

scipy.spatial.distance.euclidean(A, B)
+7

, , :

scipy.spatial.distance.cdist([A], [B], 'euclidean')

scipy.spatial.distance.cdist(np.atleast_2d(A), np.atleast_2d(B), 'euclidean')

1 × 1 np.ndarray, L2.

+4

Since all of the above answers relate to numpy and / or scipy, I just wanted to point out that something really simple can be done with a decrease here

def n_dimensional_euclidean_distance(a, b):
   """
   Returns the euclidean distance for n>=2 dimensions
   :param a: tuple with integers
   :param b: tuple with integers
   :return: the euclidean distance as an integer
   """
   dimension = len(a) # notice, this will definitely throw a IndexError if len(a) != len(b)

   return sqrt(reduce(lambda i,j: i + ((a[j] - b[j]) ** 2), range(dimension), 0))

This sums all the pairs (a [j] - b [j]) ^ 2 for all j in the number of dimensions (note that for simplicity this does not support n <2-dimensional distance).

+3
source

Starting Python 3.8, you can use the standard library mathand its new function dist, which returns the Euclidean distance between two points (specified in the form of lists or tuples of coordinates). ):

from math import dist

dist([1, 0, 0], [0, 1, 0]) # 1.4142135623730951
+1
source

All Articles