Complete the code to calculate user similarity using cosine similarity.
from sklearn.metrics.pairwise import [1] user_similarity = [1](user_item_matrix)
The cosine similarity function measures similarity between users based on their item ratings.
Complete the code to find the top 3 similar items for a given item index.
import numpy as np item_similarities = item_similarity_matrix[item_index] top_items = np.argsort(item_similarities)[[1]][:3]
Using [::-1] reverses the sorted indices to get the most similar items first.
Fix the error in the code to compute predicted rating using user-based collaborative filtering.
predicted_rating = sum(user_similarity[user_id, [1]] * ratings[[1], item_id]) / sum(abs(user_similarity[user_id, [1]]))
The neighbor_id represents other users whose similarity and ratings are used to predict the rating.
Fill both blanks to create an item-based collaborative filtering prediction formula.
predicted_rating = sum(item_similarity[item_id, [1]] * ratings[user_id, [1]]) / sum(abs(item_similarity[item_id, [2]]))
Both blanks refer to neighbor items to weight the user's ratings for prediction.
Fill all three blanks to complete the code that builds a user-item rating matrix from raw data.
import pandas as pd ratings_df = pd.DataFrame(data, columns=[[1], [2], [3]]) rating_matrix = ratings_df.pivot(index=[1], columns=[2], values='rating').fillna(0)
The DataFrame columns must be user_id, item_id, and rating to build the matrix correctly.