2017-10-18 86 views
1

我正在使用t-SNE搜索具有七个功能的数据集上的关系。如何在Python中为t-SNE添加标签

enter image description here

我使用字典来assing颜色对情节y标签:

encoding = {'d0': 0, 'd1': 1, 'd2': 2, 'd3': 3, 'd4': 4, 'd5': 5, 'd6': 6, 'd7': 7} 

plt.scatter(X_tsne[:, 0], X_tsne[:, 1], c=y['label'].apply(lambda x: city_encoding[x])) 
plt.show() 

这里的问题是,目前尚不清楚哪种颜色对应于哪个标签。数据集实际上有超过100个标签,所以不是我想要手动处理的。

enter image description here

回答

2

可以分别绘制在同一坐标的每个类别,并让Matplotlib产生的颜色和图例:

fig, ax = plt.subplots() 

groups = pd.DataFrame(X_tsne, columns=['x', 'y']).assign(category=y).groupby('category') 
for name, points in groups: 
    ax.scatter(points.x, points.y, label=name) 

ax.legend() 

为随机生成的X,这给

enter image description here