user8566323
user8566323

Reputation:

How to print tf-idf scores matrix in sklearn in python

I using sklearn to obtain tf-idf values as follows.

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())

Now I want to view my calculated tf-idf scores in a matrix as follows. tf-idf matrix

I tried to do it as follows.

idf = tfidf.idf_
dic = dict(zip(tfidf.get_feature_names(), idf))
print(dic)

However, then I get the output as follows.

{'life': 1.2876820724517808, 'learning': 1.2876820724517808}

Please help me.

Upvotes: 6

Views: 13659

Answers (3)

Ashish C.
Ashish C.

Reputation: 21

I found another possible approach using toarray() function

import pandas as pd
print(tfidf.get_feature_names())
print(tfs.toarray())
print(pd.DataFrame(tfs.toarray(), 
columns=tfidf.get_feature_names(), 
index=['doc1','doc2','doc3'])) `

Upvotes: 1

Madhu Babu
Madhu Babu

Reputation: 119

The Answer provided by the questioner is right , I would like to make one adjustment. The above code gives

         Doc1     Doc2

feature1

feature2

The matrix should be looking like this

         feature1     feature2

Doc1

Doc2

so you can make a simple change to get it

df = pd.DataFrame(tfs.todense(), index=corpus_index, columns=feature_names)

Upvotes: 3

user8566323
user8566323

Reputation:

Thanks to σηγ I could find an answer from 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)

Upvotes: 7

Related Questions