我的数据集中有一个文本列,使用该列我想为所有存在的单词计算一个 IDF。scikit 中的 TFID 实现,比如tfidf
vectorize,直接给我 TFIDF 值,而不是单词 IDF。有没有办法让单词 IDF 给出一组文档?
问问题
3395 次
1 回答
9
您可以将 TfidfVectorizer 与 use_idf=True (默认值)一起使用,然后使用 idf_ 进行提取。
from sklearn.feature_extraction.text import TfidfVectorizer
my_data = ["hello how are you", "hello who are you", "i am not you"]
tf = TfidfVectorizer(use_idf=True)
tf.fit_transform(my_data)
idf = tf.idf_
[BONUS] 如果您想获取特定单词的 idf 值:
# If you want to get the idf value for a particular word, here "hello"
tf.idf_[tf.vocabulary_["hello"]]
于 2018-01-26T10:18:32.043 回答