简体   繁体   English

计算numpy数组中点内的欧几里德距离

[英]Calculate Euclidean Distance within points in numpy array

I have 3D array as 我有3D阵列

 A = [[x1 y1 z1]
      [x2 y2 z2]
      [x3 y3 z3]]

I have to find euclidean distance between each points so that I'll get output with only 3 distance between (row0,row1) , (row1,row2) and (row0,row2) . 我必须找到每个点之间的欧几里德距离,这样我才能得到输出(row0,row1)(row1,row2)(row0,row2)之间只有3个距离。

I have some code 我有一些代码

dist = scipy.spatial.distance.cdist(A,A, 'euclidean')

but it will give distance in matrix form as 但它会以矩阵形式给出距离

dist= [[0  a   b]
       [a  0   c]
       [b  c   0]]

I want results as [abc] . 我希望结果为[abc]

Consider using scipy.spatial.distance.pdist . 考虑使用scipy.spatial.distance.pdist

You can do like this. 你可以这样做。

>>> A = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
>>> scipy.spatial.distance.pdist(A)
array([  5.19615242,  33.67491648,  28.93095228])

But be careful the order of the output distance is (row0,row1),(row0,row2) and (row1,row2). 但要注意输出距离的顺序是(row0,row1),(row0,row2)和(row1,row2)。

You can do something like this: 你可以这样做:

>>> import numpy as np
>>> from itertools import combinations
>>> A = np.array([[1, 2, 3], [4, 5, 6], [10, 20, 30]])
>>> [np.linalg.norm(a-b) for a, b in combinations(A, 2)]
[5.196152422706632, 33.674916480965472, 28.930952282978865]

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

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