如何在python中为t-SNE添加标签

Lui*_*uez 5 python machine-learning matplotlib

我正在使用 t-SNE 在具有七个特征的数据集上搜索关系。

在此处输入图片说明

我正在使用字典为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()
Run Code Online (Sandbox Code Playgroud)

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

在此处输入图片说明

Igo*_*ush 8

您可以在相同的轴上分别绘制每个类别,并让 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()
Run Code Online (Sandbox Code Playgroud)

对于随机生成的X,这给出

在此处输入图片说明