【发布时间】:2018-03-17 18:32:15
【问题描述】:
我使用sklearn获取tf-idf值如下。
from sklearn.feature_extraction.text import TfidfVectorizer
myvocabulary = ['life', 'learning']
corpus = {1: "The game of life is a game of everlasting learning", 2: "The unexamined life is not worth living", 3: "Never stop learning"}
tfidf = TfidfVectorizer(vocabulary = myvocabulary, ngram_range = (1,3))
tfs = tfidf.fit_transform(corpus.values())
现在我想在矩阵中查看我计算的 tf-idf 分数,如下所示。
我尝试如下。
idf = tfidf.idf_
dic = dict(zip(tfidf.get_feature_names(), idf))
print(dic)
但是,我得到如下输出。
{'life': 1.2876820724517808, 'learning': 1.2876820724517808}
请帮帮我。
【问题讨论】:
-
您从
tfidf.fit_transform()获得的实际输出只是这种形式。唯一需要的是您从tfidf.get_feature_names()获得的列名。只需将这两个包装到一个数据框中。
标签: python scikit-learn tf-idf