sophryu99 / TIL

TIL Repository
0 stars 1 forks source link

Recommendation System - Content-based filtering #3

Open sophryu99 opened 3 years ago

sophryu99 commented 3 years ago

Content-based Filtering

Content-based filtering uses item features to recommend other items similar to what the user likes, based on their previous actions or explicit feedback. Considers only one user.

Screen Shot 2021-07-23 at 4 49 48 PM
sophryu99 commented 3 years ago

Idea in code

Steps

  1. Pick a metric to calculate similarity (dot product etc.)
def recommend_songs(self, song_list, n_songs=10):
        song_center = self.get_mean_vector(song_list, self.df)
        scaler = self.cluster().steps[0][1]
        scaled_data = scaler.transform(self.df[self.number_cols])
        scaled_song_center = scaler.transform(song_center.reshape(1, -1))
        distances = cdist(scaled_song_center, scaled_data, 'cosine')
        index = list(np.argsort(distances)[:, :n_songs][0])

        rec_songs = self.df.iloc[index]
        # Exclude tracks data in input
        rec_songs = rec_songs[~rec_songs['track_id'].isin(song_list)]
        track_ids = [i for i in rec_songs['track_id']]
        rec_results = self.song_features(track_ids)
        return rec_results