Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a TF-IDF matrix from the item descriptions.
ML Python
from sklearn.feature_extraction.text import TfidfVectorizer items = ['Action movie with explosions', 'Romantic comedy with love', 'Sci-fi thriller with aliens'] tfidf = TfidfVectorizer() item_matrix = tfidf.[1](items)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform before fitting the vectorizer.
Using fit without transforming the data.
✗ Incorrect
We use fit_transform to learn the vocabulary and transform the items into TF-IDF vectors in one step.
2fill in blank
mediumComplete the code to compute cosine similarity between item vectors.
ML Python
from sklearn.metrics.pairwise import [1] similarity_matrix = cosine_similarity(item_matrix)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using distance functions instead of similarity.
Confusing cosine_similarity with pairwise_distances.
✗ Incorrect
cosine_similarity computes similarity scores between vectors, which is used in content-based filtering.
3fill in blank
hardFix the error in the code to get the top 2 similar items for the first item.
ML Python
import numpy as np similarities = similarity_matrix[0] top_indices = np.argsort(similarities)[[1]][:2]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not reversing the sorted indices, resulting in lowest scores first.
Using incorrect slice syntax causing errors.
✗ Incorrect
We reverse the sorted indices with [::-1] to get the highest similarity scores first.
4fill in blank
hardFill both blanks to create a dictionary of item indices and their similarity scores above 0.5.
ML Python
similar_items = {i: similarities[i] for i in range(len(similarities)) if similarities[i][1] 0.5 and i != [2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causing wrong filtering.
Not excluding the item itself leading to self-similarity.
✗ Incorrect
We want similarities greater than 0.5 and exclude the item itself at index 0.
5fill in blank
hardFill all three blanks to sort the similar items by score descending and get their indices.
ML Python
sorted_items = sorted(similar_items.items(), key=lambda x: x[1], reverse=[2]) top_similar_indices = [item[[3]] for item in sorted_items]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting ascending by mistake with reverse=False.
Extracting wrong tuple element for indices.
✗ Incorrect
We sort by the similarity score at index 1 descending (reverse=True) and extract the item indices at position 0.