有没有办法使用 scikit 或任何其他 python 包只获取单词的 IDF 值?

Apo*_*orv 3 python nlp tf-idf scikit-learn tfidfvectorizer

我的数据集中有一个文本列,使用该列我想为所有存在的单词计算一个 IDF。scikit 中的 TFID 实现,如tfidfvectorize,直接为我提供 TFIDF 值,而不是仅提供单词 IDF。有没有办法让单词 IDF 给出一组文档?

thi*_*tbl 8

您可以使用带有 use_idf=True(默认值)的 TfidfVectorizer,然后使用 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_ 
Run Code Online (Sandbox Code Playgroud)

[奖励] 如果您想获取特定单词的 idf 值:

# If you want to get the idf value for a particular word, here "hello"    
tf.idf_[tf.vocabulary_["hello"]]
Run Code Online (Sandbox Code Playgroud)