Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Update of collaborative filtering code at
# The original code at
# 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.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())
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.