

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
Inverse document frequency is a way of handling common words that are specific to a given dataset, not just generic stop words.
Consider a search query for our movie dataset:
"A movie about an actor who becomes a coding instructor"
actor might be a great keyword in some datasets, but tons of movie descriptions mention actors! A match on actor isn't very helpful for distinguishing relevant documents from irrelevant ones.
In its basic form, document frequency (DF) measures how many documents in a dataset contain a term. The more documents a term appears in, the higher its DF. actor would have a high DF in a dataset about movies.
So why would we use inverse document frequency (IDF)? Because we actually want rare terms to get higher scores.
Say we have 100 documents in total:
bear, appears in 95 documents; IDF ≈ 0.05 ← Low scorecyborg, appears in 2 documents; IDF ≈ 3.52 ← High scoremovie, appears in all 100 documents; IDF = 0 ← Zero scoreIf a search query mentions movie, a match on that term means effectively nothing. But if a query mentions cyborg, a matching document is far more likely to be relevant.
IDF is calculated as follows:
math.log((total_doc_count + 1) / (term_match_doc_count + 1))
The +1 values prevent division by zero when a term doesn't appear in any documents.
print(f"Inverse document frequency of '{args.term}': {idf:.2f}")
Run and submit the CLI tests.
Your IDF results will depend on the exact contents of movies.json and stopwords.txt. If your values are close but not identical to the tests, make sure you haven't changed those files.