AI Glossary / Natural language and speech / Intermediate
Document-term matrix
Definition
A document-term matrix is a numerical table representing documents through selected text features. In the common orientation, each row represents a document, each column represents a term, and each cell records that term’s count, presence, or weight in that document. It is a foundation for many text-classification, retrieval, and clustering methods.
What it means
Text must often be converted into numbers before a statistical method can use it. A document-term matrix supplies a consistent vocabulary of features across a collection.
The term columns may represent words, phrases, character sequences, or other explicitly defined units. The cell values need not be raw counts: binary indicators and TF-IDF weights are common alternatives.
For ordinary word-count features, the matrix discards word order within a document. This makes it useful for some tasks but insufficient for distinctions that depend on sequence or grammatical relationships. A transposed version is often called a term-document matrix, so always check the declared orientation.
How it works
Choose the document unit and preprocessing rules: what counts as a token, whether case is normalized, and which features are retained. Build a vocabulary mapping each feature to a column.
For every document, calculate the value for each vocabulary feature. A count representation records frequency; other transformations can replace or rescale those counts.
Store the result sparsely when most features are absent from most documents. Sparse storage represents the nonzero entries without allocating a dense cell for every possible pair.
For supervised evaluation, learn vocabulary and corpus-dependent weights from the appropriate training data, then apply the same feature mapping to later inputs. Keep document identifiers and column names so numerical rows remain interpretable.
Three practical examples
- A support-ticket classifier uses word-count rows as input features for predicting issue categories.
- A search prototype compares TF-IDF document vectors with a similarly represented query.
- A research team inspects how often selected terms appear across a collection before performing clustering or topic analysis.
Think of it this way
Think of a checklist shared by every document, with a numeric entry beside each selected term. The analogy is limited because the table is only a chosen representation. It cannot recover discarded word order or infer that different words mean the same thing without additional modeling.
Worked example
Use three invented documents: D1 is “red red valve,” D2 is “blue valve,” and D3 is “red pump.” Define the vocabulary order as [red, blue, valve, pump], lowercase the text, and split on spaces.
The raw-count rows are D1=[2,0,1,0], D2=[0,1,1,0], and D3=[1,0,0,1]. The matrix therefore has three rows and four columns, giving twelve possible cells, of which six are nonzero.
If the representation changes to binary presence, D1 becomes [1,0,1,0]; the other rows remain unchanged. The repeated occurrence of red is no longer represented in that version.
The document frequency of red is two because it appears in two documents, even though its total count is three. Also, “valve red red” would receive the same unigram-count row as D1. These checks show why cell values, corpus statistics, and retained sequence information must be distinguished.
Illustrative case study
In a fictional project, Pine Service analyzes short maintenance notes. The team creates a count matrix and notices that “valve” and “Valve” occupy separate columns because case normalization was inconsistent.
It fixes the preprocessing specification and rebuilds both the vocabulary and the affected vectors together. It then evaluates whether phrase features are needed to distinguish expressions whose individual words are similar but whose meanings differ.
The imagined workflow illustrates representation design. It does not imply that lowercasing is always appropriate or that adding more columns automatically improves a downstream model.
Common misconceptions and mistakes
A document-term matrix is not automatically TF-IDF; its values must be specified.
Term frequency is not document frequency. Repeating a word five times in one document increases its within-document count but still contributes only one document to document frequency.
The matrix dimensions do not tell you the amount of dense memory actually allocated when sparse storage is used.
Identical unigram rows do not prove identical sentence meanings.
Vocabulary columns must remain aligned across training and prediction; silently changing their order changes the meaning of the numbers.
Business use
The representation offers a transparent baseline for text analytics. Teams can inspect important columns, trace preprocessing choices, and compare simple models with more complex language representations.
Define what a document means for the business task: a whole report, paragraph, message, or another unit. Check rare features, spelling variation, and domain abbreviations before interpreting patterns. Preserve the fitted vocabulary with the model and evaluate on unseen material. If the task depends on negation, order, or contextual meaning, assess whether the selected features retain enough information.
People also ask
Must every matrix value be an integer?
No. Counts are integers, but weighting transformations such as TF-IDF commonly produce real-valued features.
Is a term always one word?
No. It can be a chosen n-gram or other textual feature, provided the column definition is clear.
What happens to an unseen word?
That depends on the representation. A fixed vocabulary may omit it, while other feature schemes handle new strings differently; the policy should be explicit.
Check your understanding
One word occurs seven times in a single document and nowhere else. What are its total corpus count and document frequency?
Answer: Its total count is seven and its document frequency is one. These statistics measure different aspects of occurrence.
Related terms
- Bag of words
- Sparse matrix
- Vocabulary
- Term frequency
- Document frequency
- TF-IDF
Learn next
Build a tiny count matrix by hand, then convert it to binary presence and compare what information is lost. Learn document frequency before studying TF-IDF. Finally, test two sentences with identical word counts but different meanings to understand when richer features are needed.
More in Natural language and speech
- Distributional semantics - Distributional semantics is an approach to representing meaning through patterns of language use, especially the contexts in which words or other...
- Document-rotation denoising - Document-rotation denoising is a language-model pretraining task in which a document is cyclically shifted to start at a randomly chosen token, and the...
- Document structuring - Document structuring is the natural-language-generation task of organizing selected information into a coherent document.
- Dot product - The dot product of two real-valued vectors is the sum of the products of their corresponding components.
- Dynamic programming - Dynamic programming is a method for solving a problem by combining solutions to smaller subproblems and reusing their results.
- Dynamic time warping - Dynamic time warping, or DTW, aligns two sequences while allowing differences in their local timing.
Sources
Scikit-learn official documentation, Text Feature Extraction, bag-of-words representations and vectorization: https://scikit-learn.org/stable/modules/feature_extraction.html#text-feature-extraction Manning, Raghavan, and Schütze, Introduction to Information Retrieval, term frequency and weighting: https://nlp.stanford.edu/IR-book/html/htmledition/term-frequency-and-weighting-1.html Independent source review: scikit-learn sections 8.2.3.1–8.2.3.3 were read for document rows, feature columns, count representations, order loss and sparse storage.
Previous: Document structuring Next: DOE mean plot