Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting TF-IDF Scores Of Words Using Gensim

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.

like image 582
user799188 Avatar asked Apr 15 '16 17:04

user799188


People also ask

How do I get my TF-IDF score?

As its name implies, TF-IDF vectorizes/scores a word by multiplying the word's Term Frequency (TF) with the Inverse Document Frequency (IDF). Term Frequency: TF of a term or word is the number of times the term appears in a document compared to the total number of words in the document.

What are TF-IDF scores?

TF-IDF stands for term frequency-inverse document frequency and it is a measure, used in the fields of information retrieval (IR) and machine learning, that can quantify the importance or relevance of string representations (words, phrases, lemmas, etc) in a document amongst a collection of documents (also known as a ...

How is TF-IDF calculated in Sklearn?

The formula that is used to compute the tf-idf for a term t of a document d in a document set is tf-idf(t, d) = tf(t, d) * idf(t), and the idf is computed as idf(t) = log [ n / df(t) ] + 1 (if smooth_idf=False ), where n is the total number of documents in the document set and df(t) is the document frequency of t; the ...


1 Answers

How about using dictionary comprehensions?

d = {dictionary.get(id): value for doc in corpus_tfidf for id, value in doc}
like image 178
satojkovic Avatar answered Oct 22 '22 04:10

satojkovic