DTM (Document Term Matrix)
A Document Term Matrix (DTM) is a matrix representation of a text corpus where each row corresponds to a document and each column corresponds to a unique term (word) in the vocabulary - with each cell holding the frequency of that term in that document.
Structure of a DTM
- Rows - individual documents
- Columns - unique terms across the entire corpus
- Cell values - how many times a term appears in a document (or a weighted value like TF-IDF)
Building a DTM in Python
from sklearn.feature_extraction.text import CountVectorizer
import pandas as pd
docs = ["data science is fun", "python for data science"]
vectorizer = CountVectorizer()
matrix = vectorizer.fit_transform(docs)
dtm = pd.DataFrame(matrix.toarray(), columns=vectorizer.get_feature_names_out())
print(dtm)
Why the DTM Matters
The DTM is the standard input format for many text mining and machine learning techniques - it converts a collection of documents into a numeric table that algorithms like clustering, classification, and topic modeling can work with directly.
DTMs are usually very sparse (mostly zeros), since most documents only use a small fraction of the total vocabulary - sparse matrix formats are used in practice to save memory.