[Python] How to get bag of words and term frequency in text format using Sklearn?

Discussão em 'Python' iniciado por Stack, Abril 20, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to print out the list of words (i.e., bag of words) for each document in a coprus and their respective term frequency (in text format), using Sklearn's CountVectorizer. How could I achieve that?

    Here is my code:

    from sklearn.feature_extraction.text import CountVectorizer

    #instantiate vectorizer
    vectorizer=CountVectorizer()

    #Document creation
    document1='this is a sunny day';document2= 'today is a very very very pleasant day and we have fun fun fun';document3= 'this is an amazin experience'

    #list
    list_of_words= [document1,document2,document3]

    #bag of words
    bag_of_words = vectorizer.fit(list_of_words)

    #verify vocabulary of repeated word
    print (vectorizer.vocabulary_.get('very'))

    print (vectorizer.vocabulary_.get('fun'))

    #transform
    bag_of_words=vectorizer.transform(list_of_words)

    print(bag_of_words)>>>>
    (0, 3) 1 (0, 7) 1 (0, 9) 1 (0, 10) 1 (1, 2) 1 (1, 3) 1 (1, 5) 3 (1, 6) 1 (1, 7) 1 (1, 8) 1 (1, 11) 1 (1, 12) 3 (1, 13) 1 (2, 0) 1 (2, 1) 1 (2, 4) 1 (2, 7) 1 (2, 10) 1

    Continue reading...

Compartilhe esta Página