我尝试运行这个K-Medoids python实现代码
from sklearn.metrics.pairwise import pairwise_distances
import numpy as np
import kmedoids
# 3 points in dataset
data = np.array([[1,1],
[2,2],
[10,10]])
# distance matrix
D = pairwise_distances(data, metric='euclidean')
# split into 2 clusters
M, C = kmedoids.kMedoids(D, 2)
print('medoids:')
for point_idx in M:
print( data[point_idx] )
print('')
print('clustering result:')
for label in C:
for point_idx in C[label]:
print('label {0}: {1}'.format(label, data[point_idx]))
(https://github.com/someus/kmedoids),它显示:
中心点划分:
[1 1]
[10 10]
聚类结果:
标签0:[1 1]
标签0:[2 2]
标签1:[10 10]
但我想在其聚类结果上插入索引,如下所示:
中心点划分:
[1 1],索引1
[10 10],指数3
聚类结果:
标签0:[1 1],索引1
标签0:[2 2],索引2
标签1:[10 10],索引3
有谁知道如何制作它? 谢谢
答案 0 :(得分:0)
你的意思是你想打印point_idx
吗?
print('label {0}: {1} {2}'.format(label, data[point_idx], point_idx))