Skip to content
DeepForgeby svx
All problems
EasyNLPnlp-038

Inverse Document Frequency

Problem

Return the inverse document frequency math.log(n_docs / df), where df is the number of documents that contain the term.

Return 0.0 when df is 0.

Hint

A term appearing in every document has idf 0.

Test cases

case 1
in: [2,4]
expected: 0.6931471805599453
case 2
in: [4,4]
expected: 0
case 3
in: [0,5]
expected: 0
case 4
in: [1,1]
expected: 0
case 5
in: [1,10]
expected: 2.302585092994046
Starter codePython
import math
def idf(df, n_docs):
    # Your code here
    pass

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