O'Reilly logo

Mastering Python for Data Science by Samir Madhavan

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Item-based collaborative filtering

User-based collaborative filtering finds the similarities between users, and then using these similarities between users, a recommendation is made.

Item-based collaborative filtering finds the similarities between items. This is then used to find new recommendations for a user.

To begin with item-based collaborative filtering, we'll first have to invert our dataset by putting the movies in the first layer, followed by the users in the second layer:

>>> def transform_prefs(prefs):
       result={}
       for person in prefs:
           for item in prefs[person]:
               result.setdefault(item,{})
            
               # Flip item and person
               result[item][person]=prefs[person][item]
       return result

{'Avenger: Age of Ultron': {'Jill': 7.0,'Julia': 10.0,
 'Max': ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required