行向量矩阵之间的python numpy欧几里得距离计算
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4370975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python numpy euclidean distance calculation between matrices of row vectors
提问by pacodelumberg
I am new to Numpy and I would like to ask you how to calculate euclidean distance between points stored in a vector.
我是 Numpy 的新手,我想问你如何计算存储在向量中的点之间的欧几里德距离。
Let's assume that we have a numpy.array each row is a vector and a single numpy.array. I would like to know if it is possible to calculate the euclidean distance between all the points and this single point and store them in one numpy.array.
假设我们有一个 numpy.array,每行是一个向量和一个 numpy.array。我想知道是否可以计算所有点和这个单点之间的欧几里德距离并将它们存储在一个 numpy.array 中。
Here is an interface:
这是一个界面:
points #2d list of row-vectors
singlePoint #one row-vector
listOfDistances= procedure( points,singlePoint)
Can we have something like this? Or is it possible to have one command to have the single point as a list of other points and at the end we get a matrix of distances?
我们可以有这样的东西吗?或者是否有可能有一个命令将单个点作为其他点的列表,最后我们得到一个距离矩阵?
Thanks
谢谢
采纳答案by Joe Kington
While you can use vectorize, @Karl's approach will be rather slow with numpy arrays.
虽然您可以使用矢量化,但@Karl 的方法对于 numpy 数组会相当慢。
The easier approach is to just do np.hypot(*(points - single_point).T). (The transpose assumes that points is a Nx2 array, rather than a 2xN. If it's 2xN, you don't need the .T.
更简单的方法是直接做np.hypot(*(points - single_point).T)。(转置假设点是 Nx2 数组,而不是 2xN。如果是 2xN,则不需要.T.
However this is a bit unreadable, so you write it out more explictly like this (using some canned example data...):
但是,这有点难以理解,因此您可以像这样更明确地写出它(使用一些罐头示例数据...):
import numpy as np
single_point = [3, 4]
points = np.arange(20).reshape((10,2))
dist = (points - single_point)**2
dist = np.sum(dist, axis=1)
dist = np.sqrt(dist)
回答by Karl Knechtel
To apply a function to each element of a numpy array, try numpy.vectorize.
要将函数应用于 numpy 数组的每个元素,请尝试numpy.vectorize。
To do the actual calculation, we need the square root of the sum of squares of differences (whew!) between pairs of coordinates in the two vectors.
要进行实际计算,我们需要两个向量中的坐标对之间的差值平方和(哇!)的平方根。
We can use zipto pair the coordinates, and sumwith a comprehension to sum up the results. That looks like:
我们可以用zip来配对坐标,并sum用推导来总结结果。看起来像:
sum((x - y) ** 2 for (x, y) in zip(singlePoint, pointFromArray)) ** 0.5
回答by Dima
import numpy as np
def distance(v1, v2):
return np.sqrt(np.sum((v1 - v2) ** 2))
回答by Christian
To get the distance you can use the norm method of the linalg module in numpy:
要获得距离,您可以使用 numpy 中 linalg 模块的 norm 方法:
np.linalg.norm(x - y)
回答by rombi
import numpy as np
single_point = [3, 4]
points = np.arange(20).reshape((10,2))
distance = euclid_dist(single_point,points)
def euclid_dist(t1, t2):
return np.sqrt(((t1-t2)**2).sum(axis = 1))

