Last active
September 19, 2017 12:20
-
-
Save tammoippen/991008387a67cd2a0b017dbc101e4886 to your computer and use it in GitHub Desktop.
Compute the inverse document frequency
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
from collections import Counter | |
import math | |
def idf(documents): | |
freq = Counter() | |
for doc in documents: | |
words = set(doc.split()) | |
freq.update(words) | |
res = dict() | |
for w, f in freq.items(): | |
res[w] = math.log(len(documents) / f) | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
inverse document frequency