

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Inverted Index
incomplete
2: Use the Index
incomplete
3: Boolean Search
incomplete
4: Term Frequency
incomplete
5: Inverse Document Frequency (IDF)
incomplete
6: TF-IDF
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
Click to play video
TF-IDF combines term frequency and inverse document frequency into one relevance score.
The formula is simple:
TF-IDF = TF * IDF
Suppose we search a movie dataset for cyborg bear and score the following documents.
Document 1: "The Terminator – A cyborg from the future"
cyborg: TF = 1, IDF = 3.9 → TF-IDF = 1 × 3.9 = 3.9bear: TF = 0, IDF = 0.05 → TF-IDF = 0 × 0.05 = 0Document 2: "Ted – A talking bear who loves honey and bear friends"
cyborg: TF = 0, IDF = 3.9 → TF-IDF = 0 × 3.9 = 0bear: TF = 2, IDF = 0.05 → TF-IDF = 2 × 0.05 = 0.1Document 3: "Cyborg Bear – A robotic bear saves the city"
cyborg: TF = 1, IDF = 3.9 → TF-IDF = 1 × 3.9 = 3.9bear: TF = 2, IDF = 0.05 → TF-IDF = 2 × 0.05 = 0.1Final Rankings:
cyborg term.bear term.The movie that's actually about a cyborg bear ranks highest, and cyborg matters more than bear in a dataset where bear is common.
print(f"TF-IDF score of '{args.term}' in document '{args.doc_id}': {tf_idf:.2f}")
Run and submit the CLI tests.