我在网上找到了下面这段计算TFIDF的代码:
https://github.com/timtrueman/tf-idf/blob/master/tf-idf.py
我在函数 def idf(word, documentList) 中添加了“1+”,这样我就不会除以 0 错误:
return math.log(len(documentList) / (1 + float(numDocsContaining(word,documentList))))
但我对两件事感到困惑:
- 在某些情况下我得到负值,这是正确的吗?
- 我对第 62、63 和 64 行感到困惑。
代码:
documentNumber = 0
for word in documentList[documentNumber].split(None):
words[word] = tfidf(word,documentList[documentNumber],documentList)
是否应该只在第一个文档上计算 TFIDF?
最佳答案
- 没有。 tf-idf 是 tf,一个非负值,乘以 idf,一个非负值,所以它永远不会是负数。这段代码似乎正在实现 erroneous definition of tf-idf多年来一直在维基百科上(同时它是 fixed)。
关于python - TFIDF 计算困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16648599/