Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Update of collaborative filtering code at http://tungwaiyip.info/2012/Collaborative%20Filtering.html
# 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
You can’t perform that action at this time.