TF-IDF scores you can check by hand
N = 8 documents. Scores are rounded to four decimals before sorting, so the order is the same in all three languages.
An inverted index lists, for every word, the documents that contain it. TF-IDF scores a match higher when the word is frequent in the document and rare across all documents. This simple search is a strong baseline that real systems still use.
Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.
Ready for more? This phase has 6 hands-on exercises with automatic checks.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Write idf(n_docs, df) returning ln(n_docs / df), and print it to 4 decimals for df = 1, 2, 4 and 8 with 8 documents.
Write tf_idf(word, words, n_docs, df) for one word in one document (tf times idf). Use 8 documents; the word appears twice in the document and in only 1 document overall. Print to 4 decimals.
Explain it without notes
What is an inverted index and why is it faster than scanning every document?
Explain tf and idf, and why their product is a useful score.
Name two limits of plain keyword search and one fix for each.
Why do you need an explicit tie-breaker when sorting by score?
Expected output
query: how many days for shipping
how: in 0 of 8 documents, idf 0.0000
many: in 0 of 8 documents, idf 0.0000
days: in 3 of 8 documents, idf 0.9808
for: in 2 of 8 documents, idf 1.3863
shipping: in 1 of 8 documents, idf 2.0794
6.1205 shipping
2.3671 returns
1.3863 payment
0.9808 repairs