我使用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())
我尝试按如下方式进行.
idf = tfidf.idf_
dic = dict(zip(tfidf.get_feature_names(), idf))
print(dic)
但是,我得到如下输出.
{'life': 1.2876820724517808, 'learning': 1.2876820724517808}
请帮我.
最佳答案 感谢σηγ,我可以从
this question找到答案
feature_names = tfidf.get_feature_names()
corpus_index = [n for n in corpus]
import pandas as pd
df = pd.DataFrame(tfs.T.todense(), index=feature_names, columns=corpus_index)
print(df)