Cosine similarity matrix of a corpus
In this exercise, you have been given a corpus
, which is a list containing five sentences. The corpus
is printed in the console. You have to compute the cosine similarity matrix which contains the pairwise cosine similarity score for every pair of sentences (vectorized using tf-idf).
Remember, the value corresponding to the ith row and jth column of a similarity matrix denotes the similarity score for the ith and jth vector.
This exercise is part of the course
Feature Engineering for NLP in Python
Exercise instructions
- Initialize an instance of
TfidfVectorizer
. Name ittfidf_vectorizer
. - Using
fit_transform()
, generate the tf-idf vectors forcorpus
. Name ittfidf_matrix
. - Use
cosine_similarity()
and passtfidf_matrix
to compute the cosine similarity matrixcosine_sim
.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Initialize an instance of tf-idf Vectorizer
tfidf_vectorizer = ____
# Generate the tf-idf vectors for the corpus
tfidf_matrix = tfidf_vectorizer.fit_transform(____)
# Compute and print the cosine similarity matrix
cosine_sim = ____(____, tfidf_matrix)
print(cosine_sim)