使用zip(X, Y)
得到坐标对,如果你想获得点之间的欧氏距离,它应该是(|x1-x2|^2+|y1-y2|^2)^0.5
,不(|x1-y1|^2 - |x2-y2|^2)^1/2
:
In [125]: coords=zip(X, Y)
In [126]: from scipy import spatial
...: dists=spatial.distance.cdist(coords, coords)
In [127]: dists
Out[127]:
array([[ 0. , 0.22248844, 0.09104884, 0.75377329, 0.10685954,
0.41534165, 0.5109039 , 0.15149362, 0.19490308, 0.58971785],
[ 0.22248844, 0. , 0.28973034, 0.9737061 , 0.23197262,
0.62852005, 0.73270705, 0.09751671, 0.39258852, 0.81219719],
[ 0.09104884, 0.28973034, 0. , 0.68642072, 0.19047682,
0.33880688, 0.45038919, 0.23539542, 0.1064197 , 0.53629553],
[ 0.75377329, 0.9737061 , 0.68642072, 0. , 0.79415038,
0.35411306, 0.24770988, 0.90290761, 0.59283795, 0.20443561],
[ 0.10685954, 0.23197262, 0.19047682, 0.79415038, 0. ,
0.47665258, 0.54665574, 0.13560014, 0.28381556, 0.61376196],
[ 0.41534165, 0.62852005, 0.33880688, 0.35411306, 0.47665258,
0. , 0.15477091, 0.56683251, 0.24003205, 0.25201351],
[ 0.5109039 , 0.73270705, 0.45038919, 0.24770988, 0.54665574,
0.15477091, 0. , 0.65808357, 0.36700881, 0.09751671],
[ 0.15149362, 0.09751671, 0.23539542, 0.90290761, 0.13560014,
0.56683251, 0.65808357, 0. , 0.34181257, 0.73270705],
[ 0.19490308, 0.39258852, 0.1064197 , 0.59283795, 0.28381556,
0.24003205, 0.36700881, 0.34181257, 0. , 0.45902146],
[ 0.58971785, 0.81219719, 0.53629553, 0.20443561, 0.61376196,
0.25201351, 0.09751671, 0.73270705, 0.45902146, 0. ]])
要获得此阵列的上三角,请使用numpy.triu
:
In [128]: np.triu(dists)
Out[128]:
array([[ 0. , 0.22248844, 0.09104884, 0.75377329, 0.10685954,
0.41534165, 0.5109039 , 0.15149362, 0.19490308, 0.58971785],
[ 0. , 0. , 0.28973034, 0.9737061 , 0.23197262,
0.62852005, 0.73270705, 0.09751671, 0.39258852, 0.81219719],
[ 0. , 0. , 0. , 0.68642072, 0.19047682,
0.33880688, 0.45038919, 0.23539542, 0.1064197 , 0.53629553],
[ 0. , 0. , 0. , 0. , 0.79415038,
0.35411306, 0.24770988, 0.90290761, 0.59283795, 0.20443561],
[ 0. , 0. , 0. , 0. , 0. ,
0.47665258, 0.54665574, 0.13560014, 0.28381556, 0.61376196],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0.15477091, 0.56683251, 0.24003205, 0.25201351],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0.65808357, 0.36700881, 0.09751671],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0.34181257, 0.73270705],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0.45902146],
[ 0. , 0. , 0. , 0. , 0. ,
0. , 0. , 0. , 0. , 0. ]])
听起来不错。什么是问题? –
@Jonathon Reinhart:我不知道要开始吗?任何帮助? –
叹了口气,你有没有考虑问[Google](http://www.google.com/search?q=numpy+euclidean+distance)?它直接导致你[这个成功回答的问题](http://stackoverflow.com/questions/1401712/calculate-euclidean-distance-with-numpy)。 –