当前位置:   article > 正文

聚类标签对应数据_kmeans如何将真实标签和聚类结果进行映射

kmeans如何将真实标签和聚类结果进行映射

Kmeans时想要把原数据与标签对映起来,不知道有没有类似库,懒得查了,就写了个函数:

  1. trainData = ["类0","类1","类2","噪声","类1","类2","类1","类2","类3","类1","类2","类1","噪声","类2","类2","噪声"]
  2. label = [0,1,2,-1,1,2,1,2,3,1,2,1,-1,2,2,-1]
  3. def label2cluster(trainData, labels):
  4. my_dict = {i: [] for i in labels}
  5. for i, j in enumerate(trainData):
  6. my_dict[labels[i]].append(j)
  7. return my_dict
  8. cluster = label2cluster(trainData,label)

结果如下:

  1. {0: ['类0'],
  2. 1: ['类1', '类1', '类1', '类1', '类1'],
  3. 2: ['类2', '类2', '类2', '类2', '类2', '类2'],
  4. -1: ['噪声', '噪声', '噪声'],
  5. 3: ['类3']}

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/129005
推荐阅读
相关标签
  

闽ICP备14008679号