Printing all TFIDF words and their vector values in the most efficient way
from gensim import corpora, models
# Example TF-IDF vector
tfidf_vector = [(0, 0.25), (1, 0.5), (2, 0.75)]
# Create a dictionary to map word indices to words
dictionary = corpora.Dictionary()
dictionary.id2token = {id_: token for token, id_ in dictionary.token2id.items()}
# Print all words and their TF-IDF values
for word_index, tfidf_value in tfidf_vector:
word = dictionary[word_index]
print(f"Word: {word}, TF-IDF value: {tfidf_value}")
output:
Alternatively, using List Comprehension technique:
from gensim import corpora, models
# Example TF-IDF vector
tfidf_vector = [(0, 0.25), (1, 0.5), (2, 0.75)]
# Create a dictionary to map word indices to words
dictionary = corpora.Dictionary()
dictionary.id2token = {id_: token for token, id_ in dictionary.token2id.items()}
# Print all words and their TF-IDF values using list comprehension
print([(dictionary[word_index], tfidf_value) for word_index, tfidf_value in tfidf_vector])
output: