python - 可视化文本分类

标签 python deep-learning jupyter-notebook data-visualization visualization

假设我有一个单词句子,其中每个单词(或字符)都有一个关联的数值或颜色。例如,这可能来自 RNN sentiment classifier ,这会产生类似的东西:

enter image description here

我正在寻找一种轻量级的方法来使用 Python 在 Jupyter 中可视化句子中的单词/字符。有没有一种优雅的方法可以在笔记本中内联执行此操作?我大部分时间都是在单独的 html 文件中使用其他 javascript 完成的。请注意,我可以只使用静态可视化。我见过你can change the color of the font每个字母的,但我更愿意只操纵背景颜色(填充?),同时保持文本黑色。我只是不确定这指的是什么。

最佳答案

不确定是否有实现此目标的首选方法;这是一个使用微型 html 模板和 IPython.display.display_html 的快速方法:

from IPython.display import display_html

def to_html(text, r, g, b):
    return "<var style='background-color:rgb({}, {}, {});'>{} </var>".format(
        r, g, b, text
    )

example = "A quick brown fox jumps over the lazy dog.".split()
res = ''.join(to_html(word, *np.random.randint(0,256,size=3)) for word in example)
display_html(res, raw=True)

结果是这样的:

<var style='background-color:rgb(144, 237, 221);'>A </var><var style='background-color:rgb(28, 208, 84);'>quick </var><var style='background-color:rgb(142, 241, 214);'>brown </var><var style='background-color:rgb(67, 199, 115);'>fox </var><var style='background-color:rgb(121, 120, 116);'>jumps </var><var style='background-color:rgb(251, 46, 48);'>over </var><var style='background-color:rgb(128, 147, 44);'>the </var><var style='background-color:rgb(48, 215, 5);'>lazy </var><var style='background-color:rgb(239, 90, 48);'>dog. </var>

关于python - 可视化文本分类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53771720/

相关文章:

Javascript 和 Django 'static' 模板标签

python - 使用 tf.nn.atrous_conv2d 将膨胀率单独应用于每个维度?

python - Tensorflow 中的 Conv1D 混淆

python - Google Collab 如何显示作业的值(value)?

jupyter-notebook - 单元格的 ipython 笔记本背景颜色

python - Django 设置 ‘SECRET_KEY’ 的目的是什么?

python - 将python模块保存在内存中

python - 将现有模型迁移到 Django 内置用户模型

python - 如何在tensorflow中制作自定义激活函数

python - ipython notebook pandas 最大允许列数