gpt4 book ai didi

python - TFIDF 计算困惑

转载 作者:太空狗 更新时间:2023-10-30 00:36:33 26 4
gpt4 key购买 nike

我在网上找到了下面这段计算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))))

但我对两件事感到困惑:

  1. 在某些情况下我得到负值,这是正确的吗?
  2. 我对第 62、63 和 64 行感到困惑。

代码:

 documentNumber = 0
for word in documentList[documentNumber].split(None):
words[word] = tfidf(word,documentList[documentNumber],documentList)

是否应该只在第一个文档上计算 TFIDF?

最佳答案

  1. 没有。 tf-idf 是 tf,一个非负值,乘以 idf,一个非负值,所以它永远不会是负数。这段代码似乎正在实现 erroneous definition of tf-idf多年来一直在维基百科上(同时它是 fixed)。

关于python - TFIDF 计算困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16648599/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com