我认为.idf_
是inverse document frequency,也就是说它将是
idf_(t) = log( N/ D(t)) with N=Documents in the corpus and D(t)=number of documents which have the term t
所以对于两个文档
[“ foo bar bar ist cool und so weiter”,“ Ich habe hier nichts so gesagt”]
对于所有内容,我都会期望0.69 = math.log( 2 / 1)
的价值,而对于0 = math.log(2 / 2)
来说,它的价值将是最大的。
但是sklearn似乎使用了不同的方式,因为我得到了1.406
和1.0
。
from sklearn.feature_extraction.text import TfidfVectorizer
transformer = TfidfVectorizer()
transformer.fit(["foo bar bar ist cool und so weiter", "Ich habe hier nichts so gesagt"])
print(transformer.vocabulary_)
print(transformer.idf_)
这给
{'foo': 2, 'bar': 0, 'ist': 7, 'cool': 1, 'und': 10, 'so': 9, 'weiter': 11, 'ich': 6, 'habe': 4, 'hier': 5, 'nichts': 8, 'gesagt': 3}
[1.40546511 1.40546511 1.40546511 1.40546511 1.40546511 1.40546511
1.40546511 1.40546511 1.40546511 1. 1.40546511 1.40546511]