如何在python中的sklearn中打印TF-IDF分数矩阵

4 python tf-idf scikit-learn

我使用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())
Run Code Online (Sandbox Code Playgroud)

现在,我想在矩阵中查看计算出的tf-idf分数,如下所示。 tf-idf矩阵

我试图做到如下。

idf = tfidf.idf_
dic = dict(zip(tfidf.get_feature_names(), idf))
print(dic)
Run Code Online (Sandbox Code Playgroud)

但是,然后得到如下输出。

{'life': 1.2876820724517808, 'learning': 1.2876820724517808}
Run Code Online (Sandbox Code Playgroud)

请帮我。

小智 6

谢谢 ???我可以从这个问题中找到答案

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)
Run Code Online (Sandbox Code Playgroud)