Loading…
DS · Recommenders & reduction · Program 26 · Bonus
Recommendation system using collaborative filtering.
1import pandas as pd2from sklearn.metrics.pairwise import cosine_similarity3 4ratings = pd.DataFrame({5 'Inception': [5, 4, 0, 1, 0, 5],6 'Interstellar': [5, 5, 4, 0, 1, 4],7 'The Matrix': [4, 0, 5, 1, 2, 5],8 'Titanic': [1, 1, 0, 5, 4, 0],9 'The Notebook': [0, 1, 1, 5, 5, 1],10 'La La Land': [1, 0, 2, 4, 5, 0],11 'Avengers': [4, 5, 4, 2, 0, 0]12}, index=['Asha', 'Rahul', 'Meera', 'Arjun', 'Priya', 'Karan'])13 14sim = pd.DataFrame(cosine_similarity(ratings), index=ratings.index, columns=ratings.index)15print("User Similarity Matrix:\n", sim.round(2))16 17user = 'Priya'18similar = sim[user].drop(user).idxmax()19recs = ratings.loc[similar, ratings.loc[user] == 0].sort_values(ascending=False)20print(f"\n{user} is most similar to {similar}")21print(f"Recommended for {user}:", list(recs.index))User Similarity Matrix:
Asha Rahul Meera Arjun Priya Karan
Asha 1.00 0.87 0.80 0.33 0.28 0.87
Rahul 0.87 1.00 0.63 0.34 0.20 0.61
Meera 0.80 0.63 1.00 0.39 0.44 0.65
Arjun 0.33 0.34 0.39 1.00 0.94 0.22
Priya 0.28 0.20 0.44 0.94 1.00 0.28
Karan 0.87 0.61 0.65 0.22 0.28 1.00
Priya is most similar to Arjun
Recommended for Priya: ['Avengers', 'Inception']Run program 26 in your browser
Python 3.14 · Pyodide. Change the code or the input, run it, break it, and see what happens.