CommencerCommencer gratuitement

Bag-of-words for book titles

PyBooks now has a list of book titles that need to be encoded for further analysis. The data team believes the Bag of Words (BoW) model could be the best approach.

The following packages have been imported for you: torch, torchtext.

Cet exercice fait partie du cours

Deep Learning for Text with PyTorch

Afficher le cours

Instructions

  • Import the CountVectorizer class for implementing bag-of-words.
  • Initialize an object of the class you imported, then use this object to transform the titles into a matrix representation.
  • Extract and display the first five feature names and encoded titles with the get_feature_names_out() method.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Import from sklearn
from sklearn.feature_extraction.text import ____

titles = ['The Great Gatsby','To Kill a Mockingbird','1984','The Catcher in the Rye','The Hobbit', 'Great Expectations']

# Initialize Bag-of-words with the list of book titles
vectorizer = ____()
bow_encoded_titles = ____.fit_transform(____)

# Extract and print the first five features
print(vectorizer.____[:5])
print(bow_encoded_titles.toarray()[0, :5])
Modifier et exécuter le code