Created
May 21, 2018 14:53
-
-
Save abevieiramota/54d69f5d0fd3aa14e9f1b26b088cf971 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
s = """ | |
users artist gender plays age | |
0 a aesop rock m 72 28.0 | |
1 b air m 178 28.0 | |
2 c amon tobin m 106 28.0 | |
3 d animal collective m 203 28.0 | |
4 e annie m 75 28.0 | |
""" | |
import io | |
import pandas as pd | |
import numpy as np | |
userArtist = pd.read_fwf(io.StringIO(s), header=1) | |
userArtist.set_index("Unnamed: 0", inplace=True) | |
music_mat = userArtist.pivot_table(index='artist', columns='users', values='plays').fillna(0).apply(np.sign) | |
# user-item collaberative filtering of random user | |
random_user = np.random.choice(userArtist.users) | |
# Check df for values of random user | |
random_user_rating = music_mat[random_user] | |
# Pairwise correlation of top five similar users | |
user_corr = music_mat.corrwith(random_user_rating)[:5] | |
rating_c = userArtist[(userArtist.artist.isnull().values) & (userArtist.users != random_user)] | |
rating_c['similarity'] = rating_c['users'].map(user_corr.get) | |
rating_c['sim_rating'] = rating_c.similarity * rating_c.plays | |
recommendation = rating_c.groupby('artist').apply(lambda s: s.sim_rating.sum() / s.similarity.sum()) | |
recommendation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment