简体   繁体   English

Python中的多维欧氏距离

[英]Multidimensional Euclidean Distance in Python

I want to calculate the Euclidean distance in multiple dimensions (24 dimensions) between 2 arrays. 我想计算2个数组之间多维(24维)的欧几里德距离。 I'm using numpy-Scipy. 我正在使用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 calcuate the eucleidan distance. 但是,我使用scipy.spatial.distance.cdist(A[numpy.newaxis,:],B,'euclidean') euclidean scipy.spatial.distance.cdist(A[numpy.newaxis,:],B,'euclidean')来计算eucleidan距离。

But it gave me an error 但它给了我一个错误

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

I don't seem to understand it. 我好像不明白。

I looked up scipy.spatial.distance.pdist but don't understand how to use it? scipy.spatial.distance.pdistscipy.spatial.distance.pdist但不明白如何使用它?

Is there any other better way to do it? 还有其他更好的方法吗?

Perhaps scipy.spatial.distance.euclidean ? 也许是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 

Use either 使用其中之一

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

or more simply 或者更简单

numpy.linalg.norm(A - B)

A and B are 2 points in the 24-D space. AB在24-D空间中是2个点。 You should use scipy.spatial.distance.euclidean . 你应该使用scipy.spatial.distance.euclidean

Doc here 在这里

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

Apart from the already mentioned ways of computing the Euclidean distance, here's one that's close to your original code: 除了已经提到的计算欧几里德距离的方法之外,这里有一个与原始代码接近的方法:

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

or 要么

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

This returns a 1×1 np.ndarray holding the L2 distance. 这将返回一个保持L2距离的1×1 np.ndarray

Since all of the above answers refer to numpy and or scipy, just wanted to point out that something really simple can be done with reduce here 由于以上所有答案都涉及numpy和scipy,只是想指出一些非常简单的事情可以通过减少来完成

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 will sum all pairs of (a[j] - b[j])^2 for all j in the number of dimensions (note that for simplicity this doesn't support n<2 dimensional distance). 这将对维度中所有j的所有对(a [j] - b [j])^ 2求和(注意,为简单起见,这不支持n <2维距离)。

Starting Python 3.8 , you can use standard library's math module and its new dist function, which returns the euclidean distance between two points (given as lists or tuples of coordinates): 启动Python 3.8 ,您可以使用标准库的math模块及其新的dist函数,它返回两点之间的欧氏距离(以列表或坐标元组的形式给出):

from math import dist

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM