Project - How to Build a Sentiment Classifier using Python and IMDB Reviews

6 / 11

Constructing the Vocabulary

Next, we will construct the vocabulary. This requires going through the whole training set once, applying our preprocess() function, and using a Counter() to count the number of occurrences of each word.


  • Counter().update() : We can add values to the Counter by using update() method.

  • map(myfunc) of the tensorflow datasets maps the function(or applies the function) myfunc across all the samples of the given dataset. More here.


Make sure to write each block of code below in different code-cells.

  • Import Counter from collections.

    from << your code comes here >> import << your code comes here >>
  • Get the Counter() object vocabulary.

    << your code comes here >> = Counter()
  • For each review in every batch of the train data, let us make a vocabulary dictionary containing the words and their counts correspondingly:

    for X_batch, y_batch in datasets["train"].batch(2).map(preprocess):
        for review in X_batch:
  • Let’s look at the 5 most common words:

  • Let us find the length of the vocabulary using len function.

    << your code comes here >>(vocabulary)
Get Hint See Answer

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...