Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a user-item rating matrix using pandas.
ML Python
import pandas as pd ratings = pd.DataFrame({ 'user_id': [1, 2, 1, 3], 'item_id': [101, 101, 102, 103], 'rating': [5, 4, 3, 2] }) rating_matrix = ratings.pivot(index='user_id', columns=[1], values='rating')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'rating' instead of 'item_id' for columns in pivot.
Using 'user_id' for columns which is already the index.
✗ Incorrect
The pivot function needs the column name that represents items to create the user-item matrix. Here, 'item_id' is the correct column.
2fill in blank
mediumComplete the code to calculate cosine similarity between users using sklearn.
ML Python
from sklearn.metrics.pairwise import [1] import numpy as np user_ratings = np.array([[5, 3, 0], [4, 0, 0], [1, 1, 0]]) similarity = [1](user_ratings)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using distance functions instead of similarity.
Not importing the correct function from sklearn.
✗ Incorrect
Cosine similarity measures how similar two users are based on their rating vectors. sklearn's cosine_similarity function computes this.
3fill in blank
hardFix the error in the code to predict a user's rating for an item using weighted average of neighbors' ratings.
ML Python
import numpy as np user_similarities = np.array([0.9, 0.8, 0.4]) neighbor_ratings = np.array([5, 3, 4]) predicted_rating = np.sum(user_similarities * [1]) / np.sum(user_similarities)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Multiplying similarities by themselves instead of ratings.
Using mean instead of weighted average.
✗ Incorrect
To predict the rating, multiply each neighbor's rating by their similarity and sum these, then divide by sum of similarities.
4fill in blank
hardFill both blanks to create a dictionary of item recommendations for a user based on predicted ratings above 3.
ML Python
predicted_ratings = {'item1': 4.5, 'item2': 2.0, 'item3': 3.5, 'item4': 1.0}
recommendations = {item: rating for item, rating in predicted_ratings.items() if rating [1] [2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' causing wrong filtering.
Using wrong threshold number.
✗ Incorrect
We want to keep items with ratings greater than 3, so use '>' and 3 in the condition.
5fill in blank
hardFill all three blanks to compute user similarity matrix and get top 2 similar users for user 0.
ML Python
from sklearn.metrics.pairwise import [1] import numpy as np ratings = np.array([[5, 3, 0], [4, 0, 0], [1, 1, 0], [5, 0, 4]]) sim_matrix = [1](ratings) top_users = sim_matrix[0].argsort()[::-1][[3]:[2]]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong similarity function.
Incorrect slicing indices for top users.
✗ Incorrect
Use cosine_similarity to compute similarity matrix. argsort()[::-1] sorts descending. To get top 2 excluding self (index 0), slice from 1 to 3.