Content-based filtering in recommender systems involves recommending items to users based on the characteristics of the items and the preferences expressed by the user. It assesses the content of items and user preferences to make personalized recommendations, offering suggestions similar to those the user has shown interest in.
Author
Vraj Shah
Published
September 28, 2023
Import Libraries
import numpy as npimport tensorflow as tffrom tensorflow import kerasfrom numpy import loadtxtimport pandas as pd
WARNING:tensorflow:From C:\Users\vrajs\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.
my_ratings = np.zeros(num_movies)my_ratings[2700] =5# Toy Story 3 (2010)my_ratings[2609] =2# Persuasion (2007)my_ratings[929] =5# Lord of the Rings: The Return of the King, Themy_ratings[246] =5# Shrek (2001)my_ratings[2716] =3# Inceptionmy_ratings[1150] =5# Incredibles, The (2004)my_ratings[382] =2# Amelie (Fabuleux destin d'Amélie Poulamy_ratings[366] =5# Harry Potter and the Sorcerer's Stone (a.k.a. Harry Potter and the Philosopher's Stone) (2001)my_ratings[622] =5# Harry Potter and the Chamber of Secrets (2002)my_ratings[988] =3# Eternal Sunshine of the Spotless Mind (2004)my_ratings[2925] =1# Louis Theroux: Law & Disorder (2008)my_ratings[2937] =1# Nothing to Declare (Rien à déclarer)my_ratings[793] =5# Pirates of the Caribbean: The Curse of the Black Pearl (2003)my_rated = [i for i inrange(len(my_ratings)) if my_ratings[i] >0]print('\nNew user ratings:\n')for i inrange(len(my_ratings)):if my_ratings[i] >0:print(f'Rated {my_ratings[i]} for {movieList_df.loc[i,"title"]}')Y = np.c_[my_ratings, Y]R = np.c_[(my_ratings !=0).astype(int), R]
New user ratings:
Rated 5.0 for Shrek (2001)
Rated 5.0 for Harry Potter and the Sorcerer's Stone (a.k.a. Harry Potter and the Philosopher's Stone) (2001)
Rated 2.0 for Amelie (Fabuleux destin d'Amélie Poulain, Le) (2001)
Rated 5.0 for Harry Potter and the Chamber of Secrets (2002)
Rated 5.0 for Pirates of the Caribbean: The Curse of the Black Pearl (2003)
Rated 5.0 for Lord of the Rings: The Return of the King, The (2003)
Rated 3.0 for Eternal Sunshine of the Spotless Mind (2004)
Rated 5.0 for Incredibles, The (2004)
Rated 2.0 for Persuasion (2007)
Rated 5.0 for Toy Story 3 (2010)
Rated 3.0 for Inception (2010)
Rated 1.0 for Louis Theroux: Law & Disorder (2008)
Rated 1.0 for Nothing to Declare (Rien à déclarer) (2010)
iterations =201lambda_ =1foriterinrange(iterations):with tf.GradientTape() as tape: cost_value = cost_fxn(X, W, b, Ynorm, R, lambda_) grads = tape.gradient(cost_value, [X, W, b]) optimizer.apply_gradients(zip(grads, [X, W, b]))ifiter%20==0:print(f"Training loss at iteration {iter}: {cost_value:0.1f}")
Training loss at iteration 0: 2321191.3
Training loss at iteration 20: 136169.3
Training loss at iteration 40: 51863.7
Training loss at iteration 60: 24599.0
Training loss at iteration 80: 13630.6
Training loss at iteration 100: 8487.7
Training loss at iteration 120: 5807.8
Training loss at iteration 140: 4311.6
Training loss at iteration 160: 3435.3
Training loss at iteration 180: 2902.1
Training loss at iteration 200: 2566.6
Predictions
p = np.matmul(X.numpy(), np.transpose(W.numpy())) + b.numpy()pm = p + Ymeanmy_predictions = pm[:, 0]ix = tf.argsort(my_predictions, direction='DESCENDING')for i inrange(17): j = ix[i]if j notin my_rated:print(f'Predicting rating {my_predictions[j]:0.2f} for movie {movieList[j]}')print('\n\nOriginal vs Predicted ratings:\n')for i inrange(len(my_ratings)):if my_ratings[i] >0:print(f'Original {my_ratings[i]}, Predicted {my_predictions[i]:0.2f} for {movieList[i]}')
Predicting rating 4.49 for movie My Sassy Girl (Yeopgijeogin geunyeo) (2001)
Predicting rating 4.48 for movie Martin Lawrence Live: Runteldat (2002)
Predicting rating 4.48 for movie Memento (2000)
Predicting rating 4.47 for movie Delirium (2014)
Predicting rating 4.47 for movie Laggies (2014)
Predicting rating 4.47 for movie One I Love, The (2014)
Predicting rating 4.47 for movie Particle Fever (2013)
Predicting rating 4.45 for movie Eichmann (2007)
Predicting rating 4.45 for movie Battle Royale 2: Requiem (Batoru rowaiaru II: Chinkonka) (2003)
Predicting rating 4.45 for movie Into the Abyss (2011)
Original vs Predicted ratings:
Original 5.0, Predicted 4.90 for Shrek (2001)
Original 5.0, Predicted 4.84 for Harry Potter and the Sorcerer's Stone (a.k.a. Harry Potter and the Philosopher's Stone) (2001)
Original 2.0, Predicted 2.13 for Amelie (Fabuleux destin d'Amélie Poulain, Le) (2001)
Original 5.0, Predicted 4.88 for Harry Potter and the Chamber of Secrets (2002)
Original 5.0, Predicted 4.87 for Pirates of the Caribbean: The Curse of the Black Pearl (2003)
Original 5.0, Predicted 4.89 for Lord of the Rings: The Return of the King, The (2003)
Original 3.0, Predicted 3.00 for Eternal Sunshine of the Spotless Mind (2004)
Original 5.0, Predicted 4.90 for Incredibles, The (2004)
Original 2.0, Predicted 2.11 for Persuasion (2007)
Original 5.0, Predicted 4.80 for Toy Story 3 (2010)
Original 3.0, Predicted 3.00 for Inception (2010)
Original 1.0, Predicted 1.41 for Louis Theroux: Law & Disorder (2008)
Original 1.0, Predicted 1.26 for Nothing to Declare (Rien à déclarer) (2010)