Last active
August 29, 2015 13:57
-
-
Save soxofaan/9761470 to your computer and use it in GitHub Desktop.
Update of collaborative filtering code at http://tungwaiyip.info/2012/Collaborative%20Filtering.html
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
# The original code at http://tungwaiyip.info/2012/Collaborative%20Filtering.html | |
# does not seem to work on my setup (python 2.7.2, numpy 1.6.1, pandas 0.13.1) | |
# The version below works for me. | |
import numpy as np; | |
import pandas as pd; | |
rating = pd.read_csv('movie_rating.csv') | |
rp = rating.pivot_table(cols=['critic'], rows=['title'], values='rating') | |
rating_toby = rp['Toby'] | |
sim_toby = rp.corrwith(rating_toby) | |
# Original line (which does not work for me): | |
# rating_c = rating[(rating_toby[rating.title].isnull()) & (rating.critic != 'Toby')] | |
rating_c = rating[(rating.title.map(rating_toby.get).isnull()) & (rating.critic != 'Toby')] | |
rating_c['similarity'] = rating_c['critic'].map(sim_toby.get) | |
rating_c['sim_rating'] = rating_c.similarity * rating_c.rating | |
recommendation = rating_c.groupby('title').apply(lambda s: s.sim_rating.sum() / s.similarity.sum()) | |
recommendation.order(ascending=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment