使用gensim计算tf-idf
Tf-idf calculation using gensim
我有一个来自 ISI 论文的 tf-idf 示例。我试图通过这个例子来验证我的代码。但是我的 code.I 得到了不同的结果,不知道是什么原因!
论文中的术语文档矩阵:
acceptance [ 0 1 0 1 1 0
information 0 1 0 1 0 0
media 1 0 1 0 0 2
model 0 0 1 1 0 0
selection 1 0 1 0 0 0
technology 0 1 0 1 1 0]
论文中的 Tf-idf 矩阵:
acceptance [ 0 0.4 0 0.3 0.7 0
information 0 0.7 0 0.5 0 0
media 0.3 0 0.2 0 0 1
model 0 0 0.6 0.5 0 0
selection 0.9 0 0.6 0 0 0
technology 0 0.4 0 0.3 0.7 0]
我的 tf-idf 矩阵:
acceptance [ 0 0.4 0 0.3 0.7 0
information 0 0.7 0 0.5 0 0
media 0.5 0 0.4 0 0 1
model 0 0 0.6 0.5 0 0
selection 0.8 0 0.6 0 0 0
technology 0 0.4 0 0.3 0.7 0]
我的代码:
tfidf = models.TfidfModel(corpus)
corpus_tfidf=tfidf[corpus]
我试过另一个这样的代码:
transformer = TfidfTransformer()
tfidf=transformer.fit_transform(counts).toarray() ##counts is term-document matrix
但是我没有得到合适的答案
你说的结果差异的原因是论文中有很多计算TF-IDF的方法。如果你阅读 Wikipedia TF-IDF page 它提到 TF-IDF 计算为
tfidf(t,d,D) = tf(t,d) . idf(t,D)
tf(t,d) 和 idf(t,D) 都可以用不同的函数计算,这将改变 TF_IDF 值的最后结果。其实功能在不同的应用中是不同的。
Gensim TF-IDF Model 可以计算文档中提到的 tf(t,d) 和 idf(t,D) 的任何函数。
Compute tf-idf by multiplying a local component (term frequency) with
a global component (inverse document frequency), and normalizing the
resulting documents to unit length. Formula for unnormalized weight of
term i in document j in a corpus of D documents:
weight_{i,j} = frequency_{i,j} * log_2(D / document_freq_{i})
or, more generally:
weight_{i,j} = wlocal(frequency_{i,j}) * wglobal(document_freq_{i}, D)
so you can plug in your own custom wlocal and wglobal functions.
Default for wlocal is identity (other options: math.sqrt, math.log1p,
...) and default for wglobal is log_2(total_docs / doc_freq), giving
the formula above.
现在如果你想准确的达到论文的结果,你必须知道它用什么函数来计算TF-IDF矩阵。
Gensim google group 中还有一个很好的示例,展示了如何使用自定义函数计算 TF-IDF。
我有一个来自 ISI 论文的 tf-idf 示例。我试图通过这个例子来验证我的代码。但是我的 code.I 得到了不同的结果,不知道是什么原因!
论文中的术语文档矩阵:
acceptance [ 0 1 0 1 1 0
information 0 1 0 1 0 0
media 1 0 1 0 0 2
model 0 0 1 1 0 0
selection 1 0 1 0 0 0
technology 0 1 0 1 1 0]
论文中的 Tf-idf 矩阵:
acceptance [ 0 0.4 0 0.3 0.7 0
information 0 0.7 0 0.5 0 0
media 0.3 0 0.2 0 0 1
model 0 0 0.6 0.5 0 0
selection 0.9 0 0.6 0 0 0
technology 0 0.4 0 0.3 0.7 0]
我的 tf-idf 矩阵:
acceptance [ 0 0.4 0 0.3 0.7 0
information 0 0.7 0 0.5 0 0
media 0.5 0 0.4 0 0 1
model 0 0 0.6 0.5 0 0
selection 0.8 0 0.6 0 0 0
technology 0 0.4 0 0.3 0.7 0]
我的代码:
tfidf = models.TfidfModel(corpus)
corpus_tfidf=tfidf[corpus]
我试过另一个这样的代码:
transformer = TfidfTransformer()
tfidf=transformer.fit_transform(counts).toarray() ##counts is term-document matrix
但是我没有得到合适的答案
你说的结果差异的原因是论文中有很多计算TF-IDF的方法。如果你阅读 Wikipedia TF-IDF page 它提到 TF-IDF 计算为
tfidf(t,d,D) = tf(t,d) . idf(t,D)
tf(t,d) 和 idf(t,D) 都可以用不同的函数计算,这将改变 TF_IDF 值的最后结果。其实功能在不同的应用中是不同的。
Gensim TF-IDF Model 可以计算文档中提到的 tf(t,d) 和 idf(t,D) 的任何函数。
Compute tf-idf by multiplying a local component (term frequency) with a global component (inverse document frequency), and normalizing the resulting documents to unit length. Formula for unnormalized weight of term i in document j in a corpus of D documents:
weight_{i,j} = frequency_{i,j} * log_2(D / document_freq_{i})
or, more generally:
weight_{i,j} = wlocal(frequency_{i,j}) * wglobal(document_freq_{i}, D)
so you can plug in your own custom wlocal and wglobal functions.
Default for wlocal is identity (other options: math.sqrt, math.log1p, ...) and default for wglobal is log_2(total_docs / doc_freq), giving the formula above.
现在如果你想准确的达到论文的结果,你必须知道它用什么函数来计算TF-IDF矩阵。
Gensim google group 中还有一个很好的示例,展示了如何使用自定义函数计算 TF-IDF。