Get startedGet started for free

Your first TfIdf

In this exercise, you will apply the TfIdf method to the small annak dataset, containing the first sentence of Anna Karenina by Leo Tolstoy.

Your task will be to work with this dataset and apply the TfidfVectorizer() function. Recall that performing a numeric transformation of text is your first step in being able to understand the sentiment of the text. The Tfidf vectorizer is another way to construct a vocabulary from our sentiment column.

This exercise is part of the course

Sentiment Analysis in Python

View Course

Exercise instructions

  • Import the function for building a TfIdf vectorizer from sklearn.feature_extraction.text.
  • Call the TfidfVectorizer() function and fit it on the annak dataset .
  • Transform the vectorizer.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import the required function
____

annak = ['Happy families are all alike;', 'every unhappy family is unhappy in its own way']

# Call the vectorizer and fit it
anna_vect = ____.___(annak)

# Create the tfidf representation
anna_tfidf = anna_vect.____(annak)

# Print the result 
print(anna_tfidf.toarray())
Edit and Run Code