2017-10-18 88 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