TF-IDF of movie plots
Let us use the plots of randomly selected movies to perform document clustering on. Before performing clustering on documents, they need to be cleaned of any unwanted noise (such as special characters and stop words) and converted into a sparse matrix through TF-IDF of the documents.
Use the TfidfVectorizer class to perform the TF-IDF of movie plots stored in the list plots. The remove_noise() function is available to use as a tokenizer in the TfidfVectorizer class. The .fit_transform() method fits the data into the TfidfVectorizer objects and then generates the TF-IDF sparse matrix.
Note: It takes a few seconds to run the .fit_transform() method.
Cet exercice fait partie du cours
Cluster Analysis in Python
Instructions
- Import
TfidfVectorizerclass fromsklearn. - Initialize the
TfidfVectorizerclass with minimum and maximum frequencies of 0.1 and 0.75, and 50 maximum features. - Use the
fit_transform()method on the initializedTfidfVectorizerclass with the list plots.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Import TfidfVectorizer class from sklearn
from sklearn.feature_extraction.text import ____
# Initialize TfidfVectorizer
tfidf_vectorizer = TfidfVectorizer(____)
# Use the .fit_transform() method on the list plots
tfidf_matrix = ____