Getting TF-IDF Scores Of Words Using Gensim

user799188 picture user799188 · Apr 15, 2016 · Viewed 8.4k times · Source

I am trying to find the most important words in a corpus based on their TF-IDF scores.

Been following along the example at https://radimrehurek.com/gensim/tut2.html. Based on

>>> for doc in corpus_tfidf:
...     print(doc)

the TF-IDF score is getting updated in each iteration. For example,

  • Word 0 ("computer" based on https://radimrehurek.com/gensim/tut1.html), has a TF-IDF score of 0.5773 (Doc #1), 0.4442 (Doc #2).
  • Word 10 ("graph") has a TF-IDF score of 0.7071 (Doc #7), 0.5080 (Doc #8), 0.4588 (Doc #9)

So here's how I am currently getting the final TF-IDF score for each word,

tfidf = gensim.models.tfidfmodel.TfidfModel(corpus)
corpus_tfidf = tfidf[corpus]
d = {}
for doc in corpus_tfidf:
    for id, value in doc:
        word = dictionary.get(id)
        d[word] = value

Is there a better way?

Thanks in advance.

Answer

satojkovic picture satojkovic · May 3, 2016

How about using dictionary comprehensions?

d = {dictionary.get(id): value for doc in corpus_tfidf for id, value in doc}