Bag of words and TF-IDF
Four slips from the suggestion box. Can a computer find the one word each slip is really about — without reading it?
- Build a bag of words by hand
- Calculate TF-IDF
- Let Python find each slip's key word
Bag of words and TF-IDF
Four slips from the suggestion box. Can a computer find the one word each slip is really about — without reading it?
- Build a bag of words by hand
- Calculate TF-IDF
- Let Python find each slip's key word
Story
Learn it
A machine can't work with words; it needs numbers. The bag of words model turns cleaned text into counts: which words appear, and how often, ignoring their order. But raw counts favour words that are common everywhere. TF-IDF fixes that: a word scores high when it is frequent in one document and rare in the others. That is exactly the word a document is "about".
Words to know
a model that represents text by its word counts. Bag means order doesn't matter.
(or dictionary) — every unique word in the corpus, once.
one document's counts for every word in the vocabulary.
how often a word appears in one document.
in how many documents the word appears at all.
total documents ÷ DF.
TF × log(IDF), log to base 10. CBSE's uses: document classification, topic modelling, information retrieval (search), stop-word filtering.
Step by step: three cleaned complaint slips
Slip A: bus, late, bus, cold
Slip B: cold, room, heater
Slip C: cold, canteen, tea
- Vocabulary: bus · late · cold · room · heater · canteen · tea = 7 words.
- Document vectors: A = bus 2, late 1, cold 1 (the rest 0); B = cold 1, room 1, heater 1; C = cold 1, canteen 1, tea 1.
- DF: cold is in all 3 slips; bus in 1.
- TF-IDF of bus in slip A: TF 2 × log(3 ÷ 1) = 2 × 0.477 = 0.954.
- TF-IDF of cold in slip A: TF 1 × log(3 ÷ 3) = 1 × log(1) = 0. A word in every slip can't tell them apart, just like a stop word.
- Key word of slip A: bus, the highest score. The slip is about the bus.
Careful: the most frequent word is not the most important. Cold is in every slip, so it says nothing about any one of them. And IDF is N ÷ DF, not DF ÷ N, so rare words get the bigger values.
Your missions
programs/bow.py · datasets/suggestions.txtBag of words by hand
10 minin pairs- 1
Here are the four slips after normalisation (Lesson 5.3):
- Slip 1: canteen, runs, tea, break, make, tea
- Slip 2: library, open, break, want, books
- Slip 3: tap, ground, broken, fix, tap
- Slip 4: keep, library, open, school, books
- 2
In Calc, list the vocabulary across row 1 — each word once. How many words is it?
- 3
Fill one row per slip with the count of each word. That is the document vector table.
TF-IDF
8 min- 4
Under the table, add a row for DF: in how many slips each word appears. Use
=COUNTIF(B2:B5;">0"). - 5
Predict: which word on slip 1 will have the highest TF-IDF? And on slip 3?
Only you can see this - 6
Make a second table: TF-IDF for every cell,
=B2*LOG10(4/B$7)(if DF is in row 7). Were you right?
Let Python find the key words
5 min- 7
Copy
bow.pynext tosuggestions.txt. Run it. It prints the vocabulary, the four document vectors, and each slip's key word. Does it agree with your table?
Slips 2 and 4 come out with key words want and keep. Add them — and make
— to the stop words in bow.py, and run it again. Are the new key words better?
No laptop today?
Build the document-vector table together on the board, one slip per row of desks. Then work out TF-IDF for tea, break and tap only, using log(4) = 0.602 and log(2) = 0.301.
Debate it
Search engines rank pages partly with ideas like TF-IDF.
- If a page repeats a word a hundred times to rank higher, does TF-IDF catch it?
- Slips written in Kashmiri or Urdu would share no words with the English slips. What happens to them in this bag of words?
Check yourself · practice, not a test
In the app, students answer these one at a time and each answer is checked straight away. It's practice, never a mark — anything they miss comes back later for another go.
1.In the bag of words model, the list of every unique word in the corpus is the ______.
2.A word appears in all 4 of 4 documents. Its TF-IDF is:
- A4
- B1
- C0
- D0.602
3.Tap appears twice in slip 3 and in no other slip. With 4 slips, its TF-IDF is:
- A0.301
- B0.602
- C1.204
- D2
4.In a bag of words, the order of the words matters.
TrueFalse5.Match each term to its meaning:
- Term frequency
- Document frequency
- How many documents a word appears in
- How often a word appears in one document
Common here, rare everywhere else — that's the word that matters.
Typing practice
Skill: a formula with LOG10 (5 min)
-
On typing.com: the symbols lessons.
-
Type three times, eyes on the screen:
=B2*LOG10(4/C2) -
Goal this term: 38–44 WPM at 95%.
Written and designed by Faizan Ayoub. © 2026 Faizan Ayoub. All rights reserved.