From fd503541647736f581a89a5f639732005108d347 Mon Sep 17 00:00:00 2001 From: "M.A. Miqdad Ali Riza" <it20276614@my.sliit.lk> Date: Wed, 6 Sep 2023 22:30:30 +0530 Subject: [PATCH] Replace Wishlist_create.py --- Wishlist_create.py | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/Wishlist_create.py b/Wishlist_create.py index e12c8a8..c069684 100644 --- a/Wishlist_create.py +++ b/Wishlist_create.py @@ -9,27 +9,28 @@ song_ratings = pd.read_csv("song_ratings.csv") user_song_ratings = song_ratings.pivot_table(index='user_id', columns='song_id', values='rating') user_song_ratings.fillna(0, inplace=True) -# Calculate the cosine similarity +# Calculate the cosine similarity between songs song_similarity = cosine_similarity(user_song_ratings.T) -# Define a function to generate a playlist based on a user's wish list -def generate_playlist(wish_list, num_songs=10): - invalid_users = set(wish_list) - set(user_song_ratings.index) - if invalid_users: - print(f"Song IDs: {invalid_users}") - return None - - user_ratings = user_song_ratings.loc[wish_list].values - song_ratings_mean = user_song_ratings.mean(axis=0).values.reshape(1, -1) - user_ratings_centered = user_ratings - song_ratings_mean - song_scores = np.dot(user_ratings_centered, song_similarity) / np.sum(np.abs(song_similarity), axis=0) - top_song_indices = np.argsort(-song_scores)[:num_songs] - top_song_ids = user_song_ratings.columns[top_song_indices].tolist() - return top_song_ids - - -wish_list = [3, 5] -playlist = generate_playlist(wish_list, num_songs=5) -if playlist is not None: - playlist_songs = song_ratings[song_ratings['song_id'].isin(playlist)]['song_id'] - print(playlist_songs) +# Define a function to generate song recommendations for a user +def generate_song_recommendations(user_id, num_recommendations=10): + if user_id not in user_song_ratings.index: + print(f"User ID {user_id} not found.") + return [] + + user_ratings = user_song_ratings.loc[user_id].values + song_scores = np.dot(user_ratings, song_similarity) / np.sum(np.abs(song_similarity), axis=1) + recommended_song_indices = np.argsort(-song_scores)[:num_recommendations] + recommended_song_ids = user_song_ratings.columns[recommended_song_indices].tolist() + return recommended_song_ids + +# Example: Generate song recommendations for user 98549 +user_id = 98549 +recommended_songs = generate_song_recommendations(user_id, num_recommendations=5) + +if recommended_songs: + print(f"Recommended songs for user {user_id}:") + print(recommended_songs) +else: + print("No recommendations available for this user.") + -- 2.24.1