
Bag-of-Words and Term Weighting
Count vectors turn documents into number lists, and document frequency shows which terms recur across a collection. A tiny gallery corpus reveals how weighting works and what word order we quietly discard.
المصادر
الملخص الكامل
A count vector is a simple ledger. Choose a vocabulary, then record how often each word appears in each document. Suppose an academy builds a tiny corpus of three exhibit descriptions. D1 reads "Gallery shows mosaics, gallery shows tiles." D2 reads "Gallery shows mosaics." D3 reads "Gallery shows engines." The vocabulary is gallery, shows, mosaics, tiles, engines, so D1 becomes (2, 2, 1, 1, 0), D2 becomes (1, 1, 1, 0, 0), and D3 becomes (1, 1, 0, 0, 1). Each row is a document and each column a term. Machines can compare these rows even though they cannot read.
Document frequency counts documents, not occurrences. Gallery and shows appear in all three documents, so their document frequency is 3. Mosaics appears in two, and tiles and engines in one each. Inverse document frequency rewards terms that appear in fewer documents. A common smoothed form is idf = ln((1 + N) / (1 + df)) + 1, with N = 3. Gallery and shows get ln(4/4) + 1 = 1. Mosaics gets ln(4/3) + 1, about 1.29. Tiles and engines get ln(4/2) + 1, about 1.69. Multiplying counts by these weights gives the tf-idf table. Gallery still contributes because it is frequent within documents, yet its weight per occurrence is modest.
Interpretation needs care. A rare term is not thereby a true or important term. Engines is weighted highly here only because the corpus is tiny and skewed. If future descriptions mention lanterns, a vectorizer fitted on this vocabulary will encode lanterns as absent, since unseen words fall outside the columns. Rarity measures distribution, not significance.
The deeper loss is order. D1 and a reordered description "shows gallery tiles, shows gallery mosaics" produce identical vectors. Negation and phrasing vanish too. "Mosaics in gallery" and "gallery in mosaics" share one vector. Bag-of-words earns its name because the bag holds words without arrangement.
A classroom task makes this visible. Give pairs of students the three descriptions on cards and ask them to build the count table by hand, then compute document frequencies with the smoothed formula on a calculator. Next, hand each pair a reordered version of D1 and ask whether their vector changes. Ask them to write one sentence explaining what the vector cannot capture. Collect the sentences and read several aloud. The room usually notices that counting preserves vocabulary but discards grammar.
Teachers can extend the task by adding a fourth description containing a new word and asking students to predict its column. Families can try the same exercise with grocery lists. The habit worth building is asking what a number represents before trusting it. Count vectors and term weights are useful summaries of word presence, and they are summaries with edges we can name.


