我尝试使用公式手动计算tfidf
值,但是得到的结果与使用scikit-learn实现时得到的结果不同。
from sklearn.feature_extraction.text import TfidfVectorizer
tv = TfidfVectorizer()
a = "cat hat bat splat cat bat hat mat cat"
b = "cat mat cat sat"
tv.fit_transform([a, b]).toarray()
# array([[0.53333448, 0.56920781, 0.53333448, 0.18973594, 0. ,
# 0.26666724],
# [0. , 0.75726441, 0. , 0.37863221, 0.53215436,
# 0. ]])
tv.get_feature_names()
# ['bat', 'cat', 'hat', 'mat', 'sat', 'splat']
我尝试手动计算文档的tfidf
,但结果与TfidfVectorizer.fit_transform
不同。
(np.log(2+1/1+1) + 1) * (2/9) = 0.5302876358044202
(np.log(2+1/2+1) + 1) * (3/9) = 0.750920989498456
(np.log(2+1/1+1) + 1) * (2/9) = 0.5302876358044202
(np.log(2+1/2+1) + 1) * (1/9) = 0.25030699649948535
(np.log(2+1/1+1) + 1) * (0/9) = 0.0
(np.log(2+1/1+1) + 1) * (1/9) = 0.2651438179022101
我应该得到的是:
[0.53333448, 0.56920781, 0.53333448, 0.18973594, 0, 0.26666724]
np.sqrt(np.sum(tfidf_vector ** 2))
- Eduardo SoaresTFIDFTransformer
的任何信息以及它与TFIDFVectorizer
有什么不同吗?这是我另一个关于此问题的问题。 - user_6396