
Using Sparse Matrices for Text
A document-word matrix records word counts per document, and sparse storage keeps only nonzero counts as coordinate triples. This lesson works a small example and shows when sparse storage pays off.
উৎস
সম্পূর্ণ পাঠ সারাংশ
A document-word matrix is a grid where each row is a document and each column is a word from a shared vocabulary. A cell holds the count of how many times that word appears in that document. Most cells are zero because any single document uses only a few of the possible words. Take three short messages and a vocabulary of five words including cat, dog, walk, rain, sun. Message A is "cat cat walk", so its row is 2, 0, 1, 0, 0. Message B is "dog walk rain", so its row is 0, 1, 1, 1, 0. Message C is "sun sun sun", so its row is 0, 0, 0, 0, 3. The full grid has 15 cells, but only 6 hold nonzero counts. A dense array would store all 15 numbers, including 9 zeros. A sparse format stores only the 6 nonzero values plus their row and column coordinates, such as (0, 0, 2), (0, 2, 1), (1, 1, 1), (1, 2, 1), (1, 3, 1), (2, 4, 3). Everything not listed is understood to be zero.
That understanding is the core of sparse storage. The zeros are implicit, never written down, yet the array behaves as if they were there. The cost is coordinate overhead. Each stored value carries its row and column, so a sparse format spends extra integers to save many zeros. When zeros dominate, the trade is excellent. When a matrix is mostly filled with counts, the coordinates add weight without saving anything, and a dense array is simpler and often faster.
An implicit zero is not the same as a missing value. In the grid above, a zero means the word was absent from that document. But some operations need to tell a genuine zero apart from an unknown. Imagine a link between two places with weight zero versus no link at all. Sparse tools can keep an explicit zero stored in the data, and a routine that removes zeros will erase that distinction. Absence and missing data are different ideas, and the storage format quietly encodes the difference.
Sparsity is also fragile. Not every operation preserves it. Adding two sparse matrices usually stays sparse, and so does multiplying a sparse matrix by a dense vector. But multiplying two sparse matrices can fill in new nonzero cells, sometimes dramatically. Students should test this rather than assume it.
Give groups the three-message example on cards and ask them to build both a dense table and a list of coordinate triples. Then add a fourth message, "cat rain rain," and update both representations. Ask each group to count stored numbers in each version and explain the difference aloud. Finally, ask them to mark one cell as unknown rather than zero and discuss what their list would need to show. Listening to those explanations reveals whether students grasp implicit zeros, coordinate overhead, and the gap between absence and missing data.
Sparse storage earns its place when zeros dominate a large grid, as they do in most text collections. Learning to read the coordinates, not just the counts, is the real skill.


