Reading the Document-Term Matrix
Understand rows, columns, and sparsity.
Reading the Document-Term Matrix is a free NLP Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NLP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Is the DTM?
A document-term matrix is a grid of counts. Each row is a document and each column is a word from your vocabulary. 🧮
Rows Are Documents
One row holds the full count vector for a single document, summarizing how many times each word appeared in it.
Columns Are Terms
Each column tracks one word across every document, so reading down a column shows where that word shows up.
A Cell Is a Count
The value at row i, column j is how many times word j appeared in document i, a single count.
Check the Shape
The matrix shape tells you how many documents and how many unique words you are working with.
print(X.shape) # (n_documents, n_words)Most Cells Are Zero
Any single document uses only a few of the thousands of words, so the matrix is mostly zeros, called sparse.
Why Sparsity Matters
Storing only non-zero values keeps huge matrices in memory. This is why scikit-learn returns a sparse format.
Label the Columns
Pair the array with feature names to make it readable. A DataFrame turns raw counts into a clear table.
import pandas as pd
df = pd.DataFrame(X.toarray(), columns=names)Read One Document
Look at a single row to see that document profile: which words it uses and how often. That row is its fingerprint.
Compare Two Documents
Similar documents have similar rows. Comparing vectors lets you measure how close two texts are in content.
From Matrix to Model
This matrix is the input you feed a classifier. The DTM is the features, and your labels are the targets.
Quick Check
In a document-term matrix, what does one cell hold?
Recap: The DTM
You read the document-term matrix: rows as documents, columns as words, cells as counts, and saw why it is sparse. 🎉
Frequently asked questions
Is the “Reading the Document-Term Matrix” lesson free?
Yes — the full text of “Reading the Document-Term Matrix” is free to read here on the web, and the NLP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NLP Academy course, upgrade to CoddyKit PRO.
What will I learn in “Reading the Document-Term Matrix”?
Understand rows, columns, and sparsity. You practise NLP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NLP Academy?
No prior experience is required. NLP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading the Document-Term Matrix” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NLP Academy lesson?
Yes. Every NLP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Why Models Need Numbers, Not Words
- Building a Vocabulary
- Counting With CountVectorizer
- Reading the Document-Term Matrix