Complete the code to import the library used for building recommendation models.
import [1]
TensorFlow is commonly used to build recommendation models because it supports deep learning architectures.
Complete the code to create a simple user-item interaction matrix using pandas.
import pandas as pd interactions = pd.DataFrame({'user_id': [1, 2, 1], 'item_id': [101, 102, 103], 'rating': [5, 3, 4]}) matrix = interactions.pivot(index='user_id', columns=[1], values='rating')
The pivot uses 'item_id' as columns to create the user-item matrix.
Fix the error in the code that trains a simple recommendation model using TensorFlow.
import tensorflow as tf model = tf.keras.Sequential([ tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(1) ]) model.compile(optimizer='adam', loss=[1]) model.fit(x_train, y_train, epochs=5)
Mean squared error ('mse') is appropriate for regression tasks like rating prediction.
Fill both blanks to create a dictionary comprehension that maps items to their average ratings above 3.
average_ratings = {item: [1] for item, ratings in ratings_dict.items() if [2] > 3}We calculate the average rating with sum divided by length and filter items with average rating above 3.
Fill all three blanks to create a dictionary of recommended items with predicted scores above 4.
recommendations = [1] for item, score in predicted_scores.items() if score [2] 4} sorted_recs = sorted(recommendations.items(), key=[3], reverse=True)
The dictionary comprehension collects items with scores greater than 4, then sorts by score descending.