Applying TF-IDF to book descriptions
PyBooks has collected several book descriptions and wants to identify important words within them using the TF-IDF encoding technique. By doing this, they hope to gain more insights into the unique attributes of each book to help with their book recommendation system.
The following packages have been imported for you: torch, torchtext.
Diese Übung ist Teil des Kurses
Deep Learning for Text with PyTorch
Anleitung zur Übung
- Import the
TfidfVectorizerclass fromsklearn.feature_extraction.textthat converts a collection of raw documents to a matrix of TF-IDF features. - Instantiate an object of this class, then use this object to encode the
descriptionsinto a TF-IDF matrix of vectors. - Retrieve and display the first five feature names from the
vectorizerand encoded vectors fromtfidf_encoded_descriptions.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Importing TF-IDF from sklearn
from sklearn.feature_extraction.text import ____
# Initialize TF-IDF encoding vectorizer
vectorizer = ____()
tfidf_encoded_descriptions = vectorizer.____(descriptions)
# Extract and print the first five features
print(____.get_feature_names_out()[:5])
print(____.toarray()[0, :5])