python如何添加标签_如何在python中为t-SNE添加标签(How to add labels to t-SNE in python)...

本文介绍了如何在Python中使用t-SNE进行数据可视化时为点添加标签,解决颜色与标签对应不明确的问题。通过将数据分组并分别绘制,利用Matplotlib生成颜色和图例,使得每个类别具有清晰的标识。此外,还探讨了其他可视化工具如Bokeh和ggplot2的解决方案。
摘要由CSDN通过智能技术生成

如何在python中为t-SNE添加标签(How to add labels to t-SNE in python)

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

e6a4eda2cbb9eab64c33a431da42f199.png

我正在使用字典将颜色分配给情节中的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个标签,所以这不是我想手动处理的东西。

b5faa33bc9fc288a3404182ab4c00b5d.png

I'm using t-SNE to searching for relations on a dataset which have seven features.

e6a4eda2cbb9eab64c33a431da42f199.png

I'm using a dictionary to assing colors to the y labels on the plot:

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()

The problem here is that is not clear which color corresponds to which label. The dataset actually has over 100 labels, so it's not something I'd like to handle manually.

b5faa33bc9fc288a3404182ab4c00b5d.png

原文:https://stackoverflow.com/questions/46819664

更新时间:2019-12-10 15:59

最满意答案

您可以在相同的轴上单独绘制每个类别,并让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 ,这给出了

2a7d8b9c57d91a9798bd20220e5f2e59.png

You can plot each category separately on the same axes, and let Matplotlib generate the colors and legend:

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()

For randomly generated X, this gives

2a7d8b9c57d91a9798bd20220e5f2e59.png

2017-10-18

相关问答

为组合框创建一个项类,声明它是否是可选项。 (您还可以为此添加其他有用的API,例如它所代表的时间量的方便访问器。) 然后使用一个单元工厂来禁用表示不可选项的单元格: import javafx.application.Application;

import javafx.scene.Scene;

import javafx.scene.control.ComboBox;

import javafx.scene.control.ListCell;

import javafx.scene.layou

...

我认为最简单/最干净的ggplot方法是将所有需要的信息存储在data.frame ,然后绘制它。 从上面粘贴的代码中,这应该有效: library(ggplot2)

tsne_plot

ggplot(tsne_plot) + geom_point(aes(x=x, y=y, color=col))

我使用常规plot功能的plot是:

...

检查它会做的伎俩: from bokeh.plotting import figure, ColumnDataSource, output_notebook, show

from bokeh.models import HoverTool, WheelZoomTool, PanTool, BoxZoomTool, ResetTool, TapTool, SaveTool

from bokeh.palettes import brewer

output_notebook()

#preproces

...

您可以在相同的轴上单独绘制每个类别,并让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

...

由于您的node_list由整数组成,因此您的节点将这些整数的字符串表示形式作为标签。 但是你的节点可以是任何可哈希对象,而不仅仅是整数。 所以最简单的做法是让你的node_list成为parlist项目的字符串表示。 ( parlist中的项是列表,这些列表是可变的,因此不可parlist ,这就是为什么我们不能只使用parlist作为我们的node_list 。) 还有一个函数nx.relabel_nodes ,我们可以用它来代替,但我认为只要给节点首先放置正确的标签就简单了。 import

...

来自作者本人( https://lvdmaaten.github.io/tsne/ ): 一旦我有了t-SNE地图,我怎样才能在该地图中嵌入传入的测试点? t-SNE学习非参数映射,这意味着它不会学习将输入空间中的数据映射到映射的显式函数。 因此,不可能在现有地图中嵌入测试点(尽管您可以在整个数据集上重新运行t-SNE)。 处理这个问题的一种可能的方法是训练一个多变量回归器以根据输入数据预测地图位置。 或者,你也可以让这样一个回归者直接减少t-SNE的损失,这正是我在本文中所做的( https:/

...

你有一个错字; 它应该是ax.set_xticks(xinterval) ,没有= 。 不需要set_xticklabels命令。 You have a typo; it's supposed to be ax.set_xticks(xinterval), without the =. No need for the set_xticklabels command.

维基百科没有将t-sne归类为监督学习,而是将维度降低(现在我正在写答案)。 而且,据我所知,它根本不是一种监督方法。 其目的是简化数据可视化,降低维度,还可以用作聚类技术( 无监督分类)。 Wikipedia does not classify t-sne as supervised learning but as dimensionality reduction (at the moment I am writing the answer). And, as far as I know, it

...

是的,有一个并行版本的t-SNE的barnes-hutt实现。 https://github.com/DmitryUlyanov/Multicore-TSNE 现在还有一种新的tSNE实现,它使用快速傅立叶变换函数来显着加速卷积步骤。 它还使用ANNOY库来执行最近邻搜索,默认的基于树的方法也在那里,并且都利用并行处理。 原始代码可在此处获取: https : //github.com/KlugerLab/FIt-SNE 和R包版本: https : //github.com/JulianSpag

...

对于标签,请尝试注释。 你必须计算四分位数并且意味着自己定位标签。 简单的例子: import plotly.plotly as py

from plotly.graph_objs import *

data = Data([

Box(

y=[0, 1, 1, 2, 3, 5, 8, 13, 21],

boxpoints='all',

jitter=0.3,

pointpos=-1.8

)

])

layout = L

...

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值