问题描述
我是Python的新手,所以这个问题看起来很琐碎.但是,我没有找到与我类似的案例.我有20个节点的坐标矩阵.我想从该集合计算所有节点对之间的欧几里得距离,并将它们存储在成对矩阵中.例如,如果我有20个节点,则我希望最终结果是(20,20)的矩阵,每对节点之间的欧氏距离值.我尝试使用for循环遍历坐标集的每个元素并按如下方式计算欧几里得距离:
I am new to Python so this question might look trivia. However, I did not find a similar case to mine. I have a matrix of coordinates for 20 nodes. I want to compute the euclidean distance between all pairs of nodes from this set and store them in a pairwise matrix. For example, If I have 20 nodes, I want the end result to be a matrix of (20,20) with values of euclidean distance between each pairs of nodes. I tried to used a for loop to go through each element of the coordinate set and compute euclidean distance as follows:
ncoord=numpy.matrix('3225 318;2387 989;1228 2335;57 1569;2288 8138;3514 2350;7936 314;9888 4683;6901 1834;7515 8231;709 3701;1321 8881;2290 2350;5687 5034;760 9868;2378 7521;9025 5385;4819 5943;2917 9418;3928 9770')
n=20
c=numpy.zeros((n,n))
for i in range(0,n):
for j in range(i+1,n):
c[i][j]=math.sqrt((ncoord[i][0]-ncoord[j][0])**2+(ncoord[i][1]-ncoord[j][1])**2)
但是,我收到了以下错误消息:输入必须是平方数组.我想知道是否有人知道这里发生了什么.谢谢
How ever, I am getting an error of "input must be a square array". I wonder if anybody knows what is happening here.Thanks
推荐答案
为此,使用嵌套for
循环的替代方法很多很多.我将向您展示两种不同的方法-第一种是更通用的方法,它将向您介绍广播和矢量化,第二种使用更方便的scipy库函数.
There are much, much faster alternatives to using nested for
loops for this. I'll show you two different approaches - the first will be a more general method that will introduce you to broadcasting and vectorization, and the second uses a more convenient scipy library function.
我建议做的第一件事是切换到使用np.array
而不是np.matrix
.数组是许多原因的首选,最重要的是因为它们可以具有> 2个维,而且它们使按元素乘法变得不那么尴尬了.
One of the first things I'd suggest doing is switching to using np.array
rather than np.matrix
. Arrays are preferred for a number of reasons, most importantly because they can have >2 dimensions, and they make element-wise multiplication much less awkward.
import numpy as np
ncoord = np.array(ncoord)
通过数组,我们可以通过插入新的单例维度和for循环. rel ="noreferrer">广播对其进行减法运算
With an array, we can eliminate the nested for
loops by inserting a new singleton dimension and broadcasting the subtraction over it:
# indexing with None (or np.newaxis) inserts a new dimension of size 1
print(ncoord[:, :, None].shape)
# (20, 2, 1)
# by making the 'inner' dimensions equal to 1, i.e. (20, 2, 1) - (1, 2, 20),
# the subtraction is 'broadcast' over every pair of rows in ncoord
xydiff = ncoord[:, :, None] - ncoord[:, :, None].T
print(xydiff.shape)
# (20, 2, 20)
这等效于使用嵌套的for循环遍历每对行,但速度要快得多!
This is equivalent to looping over every pair of rows using nested for loops, but much, much faster!
xydiff2 = np.zeros((20, 2, 20), dtype=xydiff.dtype)
for ii in range(20):
for jj in range(20):
for kk in range(2):
xydiff[ii, kk, jj] = ncoords[ii, kk] - ncoords[jj, kk]
# check that these give the same result
print(np.all(xydiff == xydiff2))
# True
其余的我们也可以使用向量化操作来完成:
The rest we can also do using vectorized operations:
# we square the differences and sum over the 'middle' axis, equivalent to
# computing (x_i - x_j) ** 2 + (y_i - y_j) ** 2
ssdiff = (xydiff * xydiff).sum(1)
# finally we take the square root
D = np.sqrt(ssdiff)
整个事情可以像这样一行完成:
The whole thing could be done in one line like this:
D = np.sqrt(((ncoord[:, :, None] - ncoord[:, :, None].T) ** 2).sum(1))
2.懒惰的方式,使用pdist
事实证明,已经存在用于计算所有成对距离的快速便捷功能: scipy.spatial.distance.pdist
.
2. The lazy way, using pdist
It turns out that there's already a fast and convenient function for computing all pairwise distances: scipy.spatial.distance.pdist
.
from scipy.spatial.distance import pdist, squareform
d = pdist(ncoord)
# pdist just returns the upper triangle of the pairwise distance matrix. to get
# the whole (20, 20) array we can use squareform:
print(d.shape)
# (190,)
D2 = squareform(d)
print(D2.shape)
# (20, 20)
# check that the two methods are equivalent
print np.all(D == D2)
# True
这篇关于在Python中计算numpy的欧几里得距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!