liveBook · Manning

Tf-idf-to-topic transformation

As explained in Natural Language Processing in Action, Second Edition

A TF-IDF-to-topic transformation is a linear mapping that converts a document's TF-IDF vector into a lower-dimensional topic vector.

A TF-IDF-to-topic transformation maps a document’s high-dimensional TF-IDF vector into a smaller topic vector. Think of it as mixing word counts into semantic colors: each topic assigns weights to vocabulary terms, then matrix multiplication combines them. section 4.1.3

Three topics from six words

For petness, animalness, and cityness, a 3×6 weight matrix connects six terms—cat, dog, apple, lion, NYC, and love—to three topics. Cat and dog receive equal petness weights of .3, while NYC receives a negative petness weight of −.2. Multiplying this matrix by a 6×1 TF-IDF vector produces a 3×1 topic vector. section 4.1.3

Listing 4.1 Sample weights for your topics
>>> import numpy as np

>>> topic = {}
>>> tfidf = dict(list(zip('cat dog apple lion NYC love'.split(),
...     np.random.rand(6))))                    #1
>>> topic['petness'] = (.3 * tfidf['cat'] +\
...                     .3 * tfidf['dog'] +\
...                      0 * tfidf['apple'] +\
...                      0 * tfidf['lion'] -\
...                     .2 * tfidf['NYC'] +\
...                     .2 * tfidf['love'])     #2
>>> topic['animalness']  = (.1 * tfidf['cat']  +\
...                         .1 * tfidf['dog'] -\
...                         .1 * tfidf['apple'] +\
...                         .5 * tfidf['lion'] +\
...                         .1 * tfidf['NYC'] -\
...                         .1 * tfidf['love'])
>>> topic['cityness']    = ( 0 * tfidf['cat']  -\
...                         .1 * tfidf['dog'] +\
...                         .2 * tfidf['apple'] -\
...                         .1 * tfidf['lion'] +\
...                         .5 * tfidf['NYC'] +\
...                         .1 * tfidf['love'])

Inspect the NumPy-style weighted sums that construct topic values from individual TF-IDF terms. section 4.1.3

The weights are illustrative rather than a real algorithm. A practical method would compute them from many documents, token counts, and TF-IDF vectors, including relationships such as apple and NYC. section 4.1.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains weighted matrix multiplication through a thought experiment but stops short of a real algorithm for computing weights.
Examples
Uses petness, animalness, and cityness topics with cat, dog, apple, lion, NYC, and love.
What it assumes
Assumes TF-IDF vectors and vocabulary terms, while illustrating topic weights and matrix dimensions from the ground up.
What you can do after
Evaluate how weighted topic mixtures can compress TF-IDF vectors into interpretable semantic dimensions.
How it connects
Connects TF-IDF vectors, topic modeling, semantic analysis, matrix multiplication, and synonymy.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.