TF-IDF Vectors: Rare Words Weigh More

NLPHard+30 XP+20

TF-IDF Vectors: Rare Words Weigh More

Counting words treats "the" and "thrombosis" as equally informative. TF-IDF fixes that by multiplying how often a term appears in a document by how rare it is across the collection.

Use exactly these definitions:

1tf(t, d) = count of t in d / number of tokens in d
2idf(t) = ln(N / df(t)) N = number of documents
3 df(t) = documents containing t
4value = tf(t, d) * idf(t)

Task: write tfidf(documents), where each document is a list of tokens. Return one vector per document, rounded to 4 decimal places.

  • The vocabulary is every distinct token across all documents, sorted alphabetically. Every vector uses that same order.
  • Natural log. No smoothing, no +1 anywhere, and no vector normalization — stick to the formulas above.

A term appearing in every document gets ln(N/N) = 0 and its whole column vanishes. That's the mechanism doing the real work: stopwords aren't removed by a list, they're mathematically zeroed out for being everywhere.

Examples

documents = [['a', 'b'], ['b', 'c']]
returns [[0.3466, 0.0, 0.0], [0.0, 0.0, 0.3466]]
documents = [['cat', 'cat', 'dog'], ['dog']]
returns [[0.4621, 0.0], [0.0, 0.0]]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run