【问题标题】:Why did the tf-idf model in `gensim` throws away the terms and counts after i transform the corpus?为什么 `gensim` 中的 tf-idf 模型在我转换语料库后会丢弃术语和计数?
【发布时间】:2013-02-08 18:30:53
【问题描述】:

为什么gensim中的tf-idf模型在我对语料库进行转换后,会丢掉terms and counts?

我的代码:

from gensim import corpora, models, similarities

# Let's say you have a corpus made up of 2 documents.
doc0 = [(0, 1), (1, 1)]
doc1 = [(0,1)]
doc2 = [(0, 1), (1, 1)]
doc3 = [(0, 3), (1, 1)]

corpus = [doc0,doc1,doc2,doc3]

# Train a tfidf model using the corpus
tfidf = models.TfidfModel(corpus)

# Now if you print the corpus, it still remains as the flat frequency counts.
for d in corpus:
  print d
print 

# To convert the corpus into tfidf, re-initialize the corpus 
# according to the model to get the normalized frequencies.
corpus = tfidf[corpus]

for d in corpus:
  print d

输出:

[(0, 1.0), (1, 1.0)]
[(0, 1.0)]
[(0, 1.0), (1, 1.0)]
[(0, 3.0), (1, 1.0)]

[(1, 1.0)]
[]
[(1, 1.0)]
[(1, 1.0)]

【问题讨论】:

    标签: python nlp information-retrieval tf-idf gensim


    【解决方案1】:

    IDF 是通过将文档总数除以包含该术语的文档数,然后取该商的对数来获得的。在您的情况下,所有文档都有 term0,因此 term0 的 IDF 是 log(1),等于 0。所以在您的 doc-term 矩阵中,term0 的列全为零。

    出现在所有文档中的词的权重为零,它绝对不携带任何信息。

    【讨论】:

      猜你喜欢
      • 2014-04-21
      • 2019-10-18
      • 2017-05-30
      • 2014-09-01
      • 1970-01-01
      • 2020-03-16
      • 2015-01-26
      • 2018-03-21
      • 2019-04-14
      相关资源
      最近更新 更多