赞
踩
做Kmeans时想要把原数据与标签对映起来,不知道有没有类似库,懒得查了,就写了个函数:
- trainData = ["类0","类1","类2","噪声","类1","类2","类1","类2","类3","类1","类2","类1","噪声","类2","类2","噪声"]
- label = [0,1,2,-1,1,2,1,2,3,1,2,1,-1,2,2,-1]
- def label2cluster(trainData, labels):
- my_dict = {i: [] for i in labels}
- for i, j in enumerate(trainData):
- my_dict[labels[i]].append(j)
- return my_dict
- cluster = label2cluster(trainData,label)
结果如下:
- {0: ['类0'],
- 1: ['类1', '类1', '类1', '类1', '类1'],
- 2: ['类2', '类2', '类2', '类2', '类2', '类2'],
- -1: ['噪声', '噪声', '噪声'],
- 3: ['类3']}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。