Slide 1

Slide 1 text

Building a recommender system from scratch Jill Cates PyDataDC Tutorial November 16, 2018 Washington D.C.

Slide 2

Slide 2 text

1. Build a item-item recommender • “Because you watched Movie X…” 2. Build a top-N recommender (time permitting) • “Your Top Recommendations” Objective

Slide 3

Slide 3 text

• An intro to recommenders - What is a recommender? Why are they important? • Structure of a recommender - Item-item recommendations - Top N recommendations • Types of recommenders - Collaborative filtering vs. Content-based filtering • Tutorial using the MovieLens dataset - Build an item-item recommender - Build a top N recommender (time permitting) Agenda

Slide 4

Slide 4 text

Recommender Systems in the Wild Spotify Discover Weekly Amazon Customers who bought this item also bought Netflix Because you watched this show… OkCupid Finding your best match LinkedIn Jobs recommended for you New York Times Recommended Articles for You Medicine Facilitating clinical decision making GitHub Repos “based on your interest”

Slide 5

Slide 5 text

Things were sold exclusively in brick-and-mortar stores… Before e-commerce limited inventory mainstream products

Slide 6

Slide 6 text

Things were sold exclusively in brick-and-mortar stores… Before e-commerce limited inventory mainstream products unlimited inventory niche products unlimited inventory niche products E-commerce

Slide 7

Slide 7 text

Things were sold exclusively in brick-and-mortar stores… Before e-commerce limited inventory mainstream products unlimited inventory niche products unlimited inventory niche products E-commerce

Slide 8

Slide 8 text

Recommender Systems in the Wild The Tasting Booth Experiment 6 jam samples 24 jam samples vs.

Slide 9

Slide 9 text

Recommender Systems in the Wild The Tasting Booth Experiment 6 jam samples 24 jam samples vs. Initial Interest 40% of customers stopped at the limited-choice booth 60% of customers stopped at the extensive-choice booth

Slide 10

Slide 10 text

Recommender Systems in the Wild The Tasting Booth Experiment 6 jam samples 24 jam samples vs. Subsequent Purchase 30% conversion rate 3% conversion rate

Slide 11

Slide 11 text

What is a recommender system? An application of machine learning Machine Learning Model Data Predictions

Slide 12

Slide 12 text

What is a recommender system? An application of machine learning Recommender System User preferences Recommendations

Slide 13

Slide 13 text

predicting future behaviour explicit feedback implicit feedback What is a recommender system? An application of machine learning Recommender System User preferences Recommendations

Slide 14

Slide 14 text

predicting future behaviour explicit feedback implicit feedback What is a recommender system? An application of machine learning Recommender System User preferences Recommendations Collaborative filtering Content-based filtering item user John Jim Anne Liz Erica

Slide 15

Slide 15 text

Collaborative Filtering Similar people like similar things items users John Jim Anne Liz Erica 3 User-item (“utility”) matrix

Slide 16

Slide 16 text

User Feedback item user John Jim Anne Liz Erica What are we populating these cells with? Explicit feedback Implicit feedback Likert-scale rating (1-5) Liked or not (boolean) Browsing behaviour Purchased? Read? Watched? Developing a user feedback score • Dwell time • Recent vs. old interactions • Negative implicit feedback • What behaviour are you trying to drive?

Slide 17

Slide 17 text

Content-based Filtering Looks at user and item features users John Jim Anne Liz Erica items scary funny family anime drama romance age gender country lang family? horror? 24 63 10 38 45 M F F F M CA US CA IT UK EN EN FR IT EN N N Y Y Y Y Y N N Y N N N Y N N Y N N Y Y N Y Y Y N N N N Y Y Y N N N Y N Y N N • User features: age, gender, spoken language • Item features: movie genre, year of release, cast

Slide 18

Slide 18 text

Tutorial

Slide 19

Slide 19 text

• Option 1: Run notebook locally • Option 2: Run notebook with Google Colab - Jupyter notebook environment that runs in the cloud - Minimal set-up required - Supports free GPU Environment set-up

Slide 20

Slide 20 text

• Created by GroupLens research group at the University of Minnesota • Titanic dataset of recommenders MovieLens

Slide 21

Slide 21 text

MovieLens

Slide 22

Slide 22 text

Follow along here: https://github.com/topspinj/pydata-workshop/

Slide 23

Slide 23 text

Examples

Slide 24

Slide 24 text

Pre-processing Hyperparameter Tuning Model Training Post-processing Evaluation user_id movie_id rating 2 439 4.0 10 368 4.5 14 114 5.0 19 371 1.0 2 371 3.0 19 114 4.5 3 439 3.5 54 421 2.0 32 114 3.0 10 369 1.0 Pre-processing 1.5 2.0 3.5 4.5 2.0 3.0 5.0 4.5 2.0 1.0 3.0 2.5 4.0 3.0 3.0 4.5 5.0 items users Transform original data to user-item (utility) matrix

Slide 25

Slide 25 text

Pre-processing Hyperparameter Tuning Model Training Post-processing Evaluation Mean Normalization • Optimists → rate everything 4 or 5 • Pessimists → rate everything 1 or 2 • Need to normalize ratings by accounting for user and item bias • Mean normalization - subtract from each rating for given item - subtract from each rating for given user bui = μ + bi + bu global avg user-item rating bias item’s avg rating user’s avg rating bi i u bu

Slide 26

Slide 26 text

Top N Recommender

Slide 27

Slide 27 text

Matrix Factorization • Dimensionality reduction • Factorize the user-item matrix to get 2 latent factor matrices: - User-factor matrix - Item-factor matrix • Missing ratings are predicted from the inner product of these two factor matrices Xmn ≈ Pmk × QT nk = ̂ X user item user K K item X ≈

Slide 28

Slide 28 text

Matrix Factorization • Algorithms that perform matrix factorization: - Alternating Least Squares (ALS) - Stochastic Gradient Descent (SGD) - Singular Value Decomposition (SVD) Xmn ≈ Pmk × QT nk = ̂ X user item user K K item X ≈

Slide 29

Slide 29 text

Pre-processing Hyperparameter Tuning Model Training Post-processing Evaluation Evaluation How do we evaluate recommendations? Traditional ML Recommendation Systems

Slide 30

Slide 30 text

Evaluation Metrics RMSE = ΣN i=1 (y − ̂ y)2 N precision = TP TP + FP recall = TP TP + FN F1 = 2 ⋅ precision ⋅ recall precision + recall Pre-processing Hyperparameter Tuning Model Training Post-processing Evaluation

Slide 31

Slide 31 text

Precision@K Of the top k recommendations, what proportion are actually “relevant”? Recall@K Proportion of items that were found in the top k recommendations. True negative False negative Reality Predicted liked did not like liked did not like precision = TP TP + FP recall = TP TP + FN True positive False positive Evaluation

Slide 32

Slide 32 text

Precision@K Of the top k recommendations, what proportion are actually “relevant”? Recall@K Proportion of items that were found in the top k recommendations. True negative False negative Reality Predicted liked did not like liked did not like precision = TP TP + FP recall = TP TP + FN True positive False positive Evaluation

Slide 33

Slide 33 text

Important Considerations •Interpretability •Efficiency and scalability •Diversity •Serendipity

Slide 34

Slide 34 text

• import surprise (@NicolasHug) • import implicit (@benfred) • import LightFM (@lyst) • import pyspark.mlib.recommendation Python Tools

Slide 35

Slide 35 text

Thank you! Jill Cates twitter: @jillacates github: @topspinj [email protected]