Python 用聚类矩阵计算向量间的欧氏距离
我的数组由3个向量组成,代表3个对象Python 用聚类矩阵计算向量间的欧氏距离,python,cluster-analysis,Python,Cluster Analysis,我的数组由3个向量组成,代表3个对象 X2=array([[ 5.43840675, -1.05259078, -0.21793506, 8.56686818, -2.58056957, -0.07310339, -0.31181501, 0.02696586], [ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189, -0.04897565, -0.34271698,
X2=array([[ 5.43840675, -1.05259078, -0.21793506, 8.56686818, -2.58056957,
-0.07310339, -0.31181501, 0.02696586],
[ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189,
-0.04897565, -0.34271698, -0.0339766 ],
[ 5.93081714, -1.52272427, 0.40706477, 8.56256569, -3.216366 ,
-0.0108426 , -0.57434619, -0.18952662]])
model1 = KMedoids(n_clusters=2, random_state=0).fit(X2)
它们的簇标签是[1,0,0]
水母是
medoids=array([[ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189,
-0.04897565, -0.34271698, -0.0339766 ],
[ 5.43840675, -1.05259078, -0.21793506, 8.56686818, -2.58056957,
-0.07310339, -0.31181501, 0.02696586]])
我想计算(X2)中每个对象与每个簇(0,1)的距离,例如,对象[1]与簇(0)的距离
距离(a)应为零,因为它们之间没有差异
a=euclidean_distances(X2[1].reshape(-1, 1), X2[model1.medoid_indices_][0].reshape(-1, 1))
知道问题出在哪里吗?欧几里德距离函数正在按预期工作,因为它正在计算两个数组中每个项目之间的距离。在这方面,欧几里德距离矩阵是对称的
import numpy as np
from sklearn_extra.cluster import KMedoids
from sklearn.metrics.pairwise import euclidean_distances
X2=np.array([[ 5.43840675, -1.05259078, -0.21793506, 8.56686818, -2.58056957,
-0.07310339, -0.31181501, 0.02696586],
[ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189,
-0.04897565, -0.34271698, -0.0339766 ],
[ 5.93081714, -1.52272427, 0.40706477, 8.56256569, -3.216366 ,
-0.0108426 , -0.57434619, -0.18952662]])
model1 = KMedoids(n_clusters=2, random_state=0).fit(X2)
medoids=np.array([[ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189,
-0.04897565, -0.34271698, -0.0339766 ],
[ 5.43840675, -1.05259078, -0.21793506, 8.56686818, -2.58056957,
-0.07310339, -0.31181501, 0.02696586]])
X2[1]=([ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189,
-0.04897565, -0.34271698, -0.0339766 ])
medoids[0]=[ 5.72318296, -0.99665473, -0.14540062, 8.32051008, -3.36201189,
-0.04897565, -0.34271698, -0.0339766 ]
a = (X2[1].reshape(-1, 1))
b = (X2[model1.medoid_indices_][0].reshape(-1, 1))
# dist(x, y) = sqrt(dot(x, x) - 2 * dot(x, y) + dot(y, y))
dist =euclidean_distances(a, b)
print(dist)
这就是你会看到的:
[[ 0. 6.71983769 5.86858358 2.59732712 9.08519485 5.77215861
6.06589994 5.75715956]
[ 6.71983769 0. 0.85125411 9.31716481 2.36535716 0.94767908
0.65393775 0.96267813]
[ 5.86858358 0.85125411 0. 8.4659107 3.21661127 0.09642497
0.19731636 0.11142402]
[ 2.59732712 9.31716481 8.4659107 0. 11.68252197 8.36948573
8.66322706 8.35448668]
[ 9.08519485 2.36535716 3.21661127 11.68252197 0. 3.31303624
3.01929491 3.32803529]
[ 5.77215861 0.94767908 0.09642497 8.36948573 3.31303624 0.
0.29374133 0.01499905]
[ 6.06589994 0.65393775 0.19731636 8.66322706 3.01929491 0.29374133
0. 0.30874038]
[ 5.75715956 0.96267813 0.11142402 8.35448668 3.32803529 0.01499905
0.30874038 0. ]]
[[ 0. 6.71983769 5.86858358 2.59732712 9.08519485 5.77215861
6.06589994 5.75715956]
[ 6.71983769 0. 0.85125411 9.31716481 2.36535716 0.94767908
0.65393775 0.96267813]
[ 5.86858358 0.85125411 0. 8.4659107 3.21661127 0.09642497
0.19731636 0.11142402]
[ 2.59732712 9.31716481 8.4659107 0. 11.68252197 8.36948573
8.66322706 8.35448668]
[ 9.08519485 2.36535716 3.21661127 11.68252197 0. 3.31303624
3.01929491 3.32803529]
[ 5.77215861 0.94767908 0.09642497 8.36948573 3.31303624 0.
0.29374133 0.01499905]
[ 6.06589994 0.65393775 0.19731636 8.66322706 3.01929491 0.29374133
0. 0.30874038]
[ 5.75715956 0.96267813 0.11142402 8.35448668 3.32803529 0.01499905
0.30874038 0. ]]