TDM (Term Document Matrix)
A Term Document Matrix (TDM) is the transpose of the Document Term Matrix - here, each row represents a unique term and each column represents a document, with cell values showing how often each term appears in each document.
DTM vs TDM
| DTM | TDM | |
|---|---|---|
| Rows | Documents | Terms |
| Columns | Terms | Documents |
Both matrices hold the same underlying information - the choice between them usually comes down to which orientation is more convenient for the analysis or library being used.
Creating a TDM 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())
tdm = dtm.T # transpose to get a Term Document Matrix
print(tdm)
When TDM Is Preferred
A TDM is often more convenient when the analysis focuses on terms themselves - such as finding which terms are most frequent overall, or computing term-to-term similarity across a corpus.
Like the DTM, a TDM is typically sparse and can grow very large with bigger vocabularies - dimensionality reduction techniques are often applied before further analysis.