liveBook · Manning

Document-term matrix

As explained in Natural Language Processing in Action, Second Edition

A term-document matrix is a matrix whose rows represent terms and whose columns represent documents, with entries describing term-document associations.

A document-term matrix turns a text collection into numbers: its rows represent vocabulary terms, its columns represent documents, and each entry records a term–document association. TF–IDF supplies one common weighting scheme, making the structure useful for search and topic modeling. section 4.4 section 3.6.3

A toxic-comment corpus

A comment corpus produces a TF–IDF representation with shape (5000, 19169): 5,000 messages and 19,169 dimensions in the displayed dataset. The vocabulary is large relative to the available messages, making the representation cumbersome and increasing overfitting risk. Similar meanings such as “leave this page” and “go away” can still receive low similarity because their words differ. section 4.4

The matrix supports several practical choices:

  • Use it as a traditional information-retrieval representation, then compare TF–IDF weighting with BM25 or BM25F when ranking relevance. section 3.6.3
  • Apply dimensionality reduction such as PCA, SVD, or LSA to make document relationships and latent topics easier to analyze. section 4.4
>>> tfidf_docs.shape
(5000, 19169)

Inspecting the matrix shape exposes the number of documents and feature dimensions before modeling. section 4.4

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Defines the matrix and inspects its dimensions and modeling limitations, while deferring detailed construction and weighting derivations.
Examples
Uses a 5,000-message toxic-comment corpus, a 19,169-dimensional TF–IDF representation, and contrasting phrases with low lexical similarity.
What it assumes
Assumes familiarity with vocabulary-based text features and develops connections to TF–IDF, dimensionality reduction, PCA, SVD, and LSA.
What you can do after
Evaluate/understand feature dimensionality, lexical-similarity limits, overfitting risk, and alternative ranking or reduction strategies.
How it connects
Connects TF–IDF, information retrieval, BM25, latent semantic analysis, dimensionality reduction, PCA, SVD, similarity, and overfitting.

Also explained in

Compare how 2 books explain document-term matrix →

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.