Skip to content
DeepForgeby svx
All problems
MediumNLPnlp-402

TF-IDF Document Vector

Problem

The TF-IDF vector multiplies each term frequency by its inverse document frequency, following the given vocabulary order.

Given the vocabulary, the document tokens, and an IDF table keyed by term strings, return the vector.

Hint

Count term frequencies, then scale each by the matching IDF entry.

Test cases

case 1
in: [["a","b"],["a","a","b"],{"a":1,"b":2}]
expected: [2,2]
case 2
in: [["x"],["x"],{"x":0.5}]
expected: [0.5]
case 3
in: [["p","q"],[],{"p":1,"q":1}]
expected: [0,0]
Starter codePython
def tf_idf_document_vector(vocab, tokens, idf):
    # Your code here
    pass

3 test cases · run your code against them in the editor