0
0
ML Pythonml~10 mins

Content-based filtering in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Afit
Btransform
Cfit_transform
Dfit_predict
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform before fitting the vectorizer.
Using fit without transforming the data.
2fill in blank
medium

Complete 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'
Acosine_similarity
Beuclidean_distances
Cmanhattan_distances
Dpairwise_distances
Attempts:
3 left
💡 Hint
Common Mistakes
Using distance functions instead of similarity.
Confusing cosine_similarity with pairwise_distances.
3fill in blank
hard

Fix 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'
A:
B-1:
C1:
D::-1
Attempts:
3 left
💡 Hint
Common Mistakes
Not reversing the sorted indices, resulting in lowest scores first.
Using incorrect slice syntax causing errors.
4fill in blank
hard

Fill 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'
A>
B0
C<
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causing wrong filtering.
Not excluding the item itself leading to self-similarity.
5fill in blank
hard

Fill 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'
A[1]
BTrue
C0
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Sorting ascending by mistake with reverse=False.
Extracting wrong tuple element for indices.