【发布时间】:2021-06-28 17:12:42
【问题描述】:
1.对于下面的测试文本,
test=['test test', 'test toy']
tf-idf 分数 [没有归一化 (smartirs: 'ntn')] 是
[['test', 1.17]]
[['test', 0.58], ['toy', 1.58]]
这似乎与我通过直接计算得到的不符
tfidf (w, d) = tf x idf
where idf(term)=log (total number of documents / number of documents containing term)
tf = number of instances of word in d document / total number of words of d document
例如
doc 1: 'test test'
for "test" word
tf= 1
idf= log(2/2) = 0
tf-idf = 0
有人可以用我上面的测试文本告诉我计算吗?
2)当我更改为余弦归一化(smartirs:'ntc')时,我得到
[['test', 1.0]]
[['test', 0.35], ['toy', 0.94]]
有人也可以告诉我计算吗?
谢谢
import gensim
from gensim import corpora
from gensim import models
import numpy as np
from gensim.utils import simple_preprocess
test=['test test', 'test toy']
texts = [simple_preprocess(doc) for doc in test]
mydict= corpora.Dictionary(texts)
mycorpus = [mydict.doc2bow(doc, allow_update=True) for doc in texts]
tfidf = models.TfidfModel(mycorpus, smartirs='ntn')
for doc in tfidf[mycorpus]:
print([[mydict[id], np.around(freq, decimals=2)] for id, freq in doc])
【问题讨论】: