Module 17 · Recommender systems · Lesson 58 of 66

Movies like this one

Recommend similar movies by correlating how users rated them.

In this lesson · about 25 minutes

  • Tell content-based and collaborative filtering apart
  • Build a user-by-movie ratings matrix with pivot_table
  • Find similar movies with corrwith, and filter out ones with too few ratings

Every streaming service and online store recommends things. The two main approaches:

  • Content-based: recommend items with similar attributes. You liked an action movie with a certain actor, so here's another.
  • Collaborative filtering: recommend what similar people liked, using only the pattern of ratings, with no knowledge of what the items are. This is what most large systems rely on.

We'll use MovieLens 100K: 100,000 ratings (1 to 5 stars) from 943 users on 1,682 movies, collected by the GroupLens research group in the 1990s.

Exercise

Average rating and count

Make a DataFrame ratings with each title's mean rating in a column called rating, then add a column 'num of ratings' with how many ratings each title got. Show the 5 most-rated titles.

Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
column_names = ['user_id', 'item_id', 'rating', 'timestamp']
df = pd.read_csv('u.data', sep='\t', names=column_names)
movie_titles = pd.read_csv('Movie_Id_Titles.csv')
df = pd.merge(df, movie_titles, on='item_id')

Look at the ratings

Most movies have only a few ratings. Movies with many ratings tend to be rated higher: popular movies are popular for a reason.

The ratings matrix

Pivot the data so each row is a user and each column is a movie, with the rating in the cell. Most cells are empty (NaN), because each person has seen only a few movies.

To find movies like Star Wars, take its column and correlate it with every other column using corrwith. A high correlation means people who rated Star Wars highly also rated that movie highly, and the reverse.

Exercise

Similar to Star Wars

From moviemat, take the 'Star Wars (1977)' column as starwars_user_ratings. Correlate every movie with it using moviemat.corrwith(...), put the result in a DataFrame corr_starwars with the column 'Correlation', and drop the NaN rows.

Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
column_names = ['user_id', 'item_id', 'rating', 'timestamp']
df = pd.read_csv('u.data', sep='\t', names=column_names)
movie_titles = pd.read_csv('Movie_Id_Titles.csv')
df = pd.merge(df, movie_titles, on='item_id')
ratings = pd.DataFrame(df.groupby('title')['rating'].mean())
ratings['num of ratings'] = df.groupby('title')['rating'].count()
moviemat = df.pivot_table(index='user_id', columns='title', values='rating')

Sorted by correlation, the top of that list is full of obscure movies with a perfect 1.0, because one or two people happened to rate both the same way. The fix: only trust movies with enough ratings. Join on the counts and keep movies with more than 100.

Exercise

Only trust popular movies

Join ratings['num of ratings'] onto corr_starwars, keep movies with more than 100 ratings, and show the top 5 by correlation.

Code that runs before yours
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
column_names = ['user_id', 'item_id', 'rating', 'timestamp']
df = pd.read_csv('u.data', sep='\t', names=column_names)
movie_titles = pd.read_csv('Movie_Id_Titles.csv')
df = pd.merge(df, movie_titles, on='item_id')
ratings = pd.DataFrame(df.groupby('title')['rating'].mean())
ratings['num of ratings'] = df.groupby('title')['rating'].count()
moviemat = df.pivot_table(index='user_id', columns='title', values='rating')
corr_starwars = pd.DataFrame(moviemat.corrwith(moviemat['Star Wars (1977)']), columns=['Correlation']).dropna()

Try another movie

Here's the comedy Liar Liar. Swap in any title from the ratings table, such as 'Toy Story (1995)' or 'Fargo (1996)'.

Exercises solved: 0 of 3

Tip: solve each exercise first. Press Check to test your code.

Noviq Academy's Data Science course is free and for learning. Python runs in your browser with Pyodide, which downloads about 20 MB the first time, plus a few more for scikit-learn. Your code and progress stay on this device. The salaries data is a sample of San Francisco's public pay records; the purchases, customers, housing, advertising and classified data are made up. The Titanic, tips, flights and iris data sets come from Seaborn's examples; the maps use public 2011 to 2014 US and world figures. The 911 data is a 20,000-call sample of Montgomery County, Pennsylvania's public 911 records; the movie ratings are the MovieLens 100K data from GroupLens Research; the reviews are a 3,000-review sample from the Yelp Dataset Challenge; the loans are public LendingClub data; the colleges are the College data from An Introduction to Statistical Learning; the bank notes, kyphosis and breast cancer data come from the UCI Machine Learning Repository. Spark and TensorFlow can't run in a browser, so those lessons use stand-ins that run here, and show the code for the real tools.