Exercise 3: Why Sparsity Is Essentially Unavoidable for Bag-of-Words Vectors — Possible Solution ==================================================================== WHAT SPARSITY MEANS, PER THIS CHAPTER ------------------------------ Per this chapter, "a real corpus can easily have a vocabulary of tens of thousands of unique words — but any single document only ever uses a tiny fraction of them. The resulting vectors are overwhelmingly zeros, a property called sparsity." A sparse vector is one where the vast majority of its own positions hold the value zero, with only a small number of positions holding any nonzero value at all. WHY THIS FOLLOWS DIRECTLY FROM HOW BAG-OF-WORDS VECTORS ARE BUILT ------------------------------ Per this chapter's own definition, every vocabulary word — every unique word that appears ANYWHERE across the entire corpus — gets its own fixed position in every single document's own vector, regardless of whether that particular document actually uses that word. A single ordinary document (a short review, an email, a single news article) realistically only contains a few dozen to a few hundred distinct words, drawn from a vocabulary that, across an entire corpus, can easily span tens of thousands of unique words. Every vocabulary word NOT among those few dozen or few hundred used in this one specific document gets a zero at its own position. WHY THIS IS A MATTER OF SCALE, NOT A RARE COINCIDENCE ------------------------------ If a corpus's own vocabulary has, say, 20,000 unique words, and a typical document in that corpus uses only 150 distinct words, then 19,850 out of 20,000 vector positions — more than 99% — are necessarily zero for that document, simply as an arithmetic consequence of how few of the corpus's own total unique words any single document could realistically contain. This isn't something that happens to occur occasionally; it's the mathematically guaranteed outcome of building a vector as large as the full corpus vocabulary while representing a document that only ever uses a small subset of it. WHY THIS IS ESSENTIALLY UNAVOIDABLE, NOT A FIXABLE DESIGN CHOICE ------------------------------ The vocabulary must, by the bag-of-words method's own definition, include every word that appears anywhere in the corpus — shrinking the vocabulary to avoid sparsity would mean discarding legitimate words that some OTHER document in the corpus genuinely needs represented. Given that requirement, and given that any single document only ever uses a small fraction of an entire corpus's own vocabulary, high sparsity is a direct, structural consequence of the method itself — not a symptom of a poorly-chosen or fixable parameter. WHY THIS MATTERS PRACTICALLY, PER THIS CHAPTER ------------------------------ Per this chapter, "most libraries (including CountVectorizer) store and compute with sparse vectors efficiently rather than wastefully storing every zero" — acknowledging sparsity as a genuine, expected property that real tooling is specifically built to handle efficiently, rather than an unusual failure state to be avoided. WHY THIS WORKS AS AN ANSWER ------------------------------ It explains precisely why every vocabulary word gets a position in every document's vector, explains why any one document's own limited word usage against a much larger corpus-wide vocabulary makes the overwhelming majority of those positions zero as a simple matter of scale, and explains why this is a structural consequence of the method rather than an avoidable edge case.