Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visualizing Text Classification

Suppose I have a sentence of words, where each word (or character) has an associated numeric value, or color. As an example, this could be coming from an RNN sentiment classifier, which would produce something like:

enter image description here

I'm looking for a lightweight way of visualizing words/characters in sentences in Jupyter with python. Is there an elegant way of doing this inline in a notebook? I've mostly seen this done with additional javascript, in a separate html file. Note that I'm fine with just a static visualization. I've seen you can change the color of the font of each letter, but I'd prefer to just manipulate the background color (fill?), while keeping the text black. I'm just not sure what this is referred to.

like image 913
Alex R. Avatar asked Dec 13 '18 23:12

Alex R.


People also ask

How do you visualize classification of data?

Some of the most common data visualization examples include charts, tables, graphs, timelines, and maps. Data visualization tools are ideal for identifying trends, deviations, and patterns in data sets easily and in a way that these patterns can also be understood by non-technical people.

How do you visualize text data?

Text visualization is mainly achieved through the use of graph, chart, word cloud, map, network, timeline, etc. It is these visualized results that make it possible for humans to read the most important aspects of a huge amount of information.

What are the 4 levels of visualization?

These stages are exploration, analysis, synthesis, and presentation.

Which are examples of visualizations for text data mining?

Examples of visualizations include word clouds, graphs, charts, and maps.


1 Answers

Not sure if there is a preferred way to achieve this; here is a quick approach that uses a tiny html template and 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)

Resulting in something like this:

<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>
like image 165
hilberts_drinking_problem Avatar answered Oct 10 '22 17:10

hilberts_drinking_problem