可视化文本分类

Ale*_* R. 6 python visualization data-visualization deep-learning jupyter-notebook

假设我有一个单词的句子,其中每个单词(或字符)都有一个相关的数值或颜色.例如,这可能来自RNN情绪分类器,它将产生如下内容:

在此输入图像描述

我正在寻找一种可视化Jupyter中使用python的句子中的单词/字符的轻量级方法.是否有一种优雅的方式在笔记本中内联?我在一个单独的html文件中主要看到这个用附加的javascript完成.请注意,我只使用静态可视化.我已经看到你可以改变每个字母的字体颜色,但我更喜欢只操纵背景颜色(填充?),同时保持文字黑色.我只是不确定这是指什么.

hil*_*lem 1

不确定是否有更好的方法来实现这一目标;这是一种使用小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)
Run Code Online (Sandbox Code Playgroud)

结果是这样的:

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)
Run Code Online (Sandbox Code Playgroud)