计算numpy数组中点内的欧几里得距离
问题内容:
我有3D阵列
A = [[x1 y1 z1]
[x2 y2 z2]
[x3 y3 z3]]
我必须找到每个点之间的欧氏距离,这样我会只之间的距离3获取输出(row0,row1)
,(row1,row2)
和(row0,row2)
。
我有一些代码
dist = scipy.spatial.distance.cdist(A,A, 'euclidean')
但是它将以矩阵形式给出距离为
dist= [[0 a b]
[a 0 c]
[b c 0]]
我希望结果为[a b c]
。
问题答案:
您可以执行以下操作:
>>> 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]