Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

Successfully reported this slideshow.

Like this presentation? Why not share!

- PyParis 2017 / Un mooc python, by t... by Pôle Systematic P... 1186 views
- Rekomendujemy - Szybkie wprowadzeni... by Bartlomiej Twardo... 2512 views
- How To Implement a CMS by Jonathan Smith 7577 views
- Content based filtering by Bendito Freitas R... 4782 views
- How to Build Recommender System wit... by Võ Duy Tuấn 5868 views
- How to build a Recommender System by Võ Duy Tuấn 7583 views

5,555 views

Published on

PyParis 2017

http://pyparis.org

Published in:
Technology

No Downloads

Total views

5,555

On SlideShare

0

From Embeds

0

Number of Embeds

2

Shares

0

Downloads

154

Comments

0

Likes

9

No embeds

No notes for slide

- 1. An introduction to COLLABORATIVE FILTERING IN PYTHON and an overview of Surprise 1
- 2. WHAT SHOULD I READ? 2
- 3. WHAT SHOULD I SEE? 3
- 4. WHERE SHOULD I EAT? 4
- 5. TAXONOMY Content-based Leverage information about the items content Based on item profiles (metadata). Collaborative filtering Leverage social information (user-item interactions) E.g.: recommend items liked by my peers. Usually yields better results. Knowledge-based Leverage users requirements Used for cars, loans, real estate... Very task-specific. 5
- 6. Collaborative filtering Leverage social information (user-item interactions) E.g.: recommend items liked by my peers. Usually yields better results. 5
- 7. RATING PREDICTION Rows are users, columns are items ~ 99% of the entries are missing Your mission: predict the 6
- 8. ABOUT ME Nicolas Hug PhD Student (University of Toulouse III) Needed a Python library for RS research... Did it. 7
- 9. OUTLINE 1. THE NEIGHBORHOOD METHOD (K-NN) 8
- 10. OUTLINE 1. THE NEIGHBORHOOD METHOD (K-NN) 2. OVERVIEW OF ( ) SURPRISE http://surpriselib.com 8
- 11. OUTLINE 1. THE NEIGHBORHOOD METHOD (K-NN) 2. OVERVIEW OF ( ) SURPRISE http://surpriselib.com 3. MATRIX FACTORIZATION 8
- 12. 1. THE NEIGHBORHOOD METHOD (K-NN) 8
- 13. NEIGHBORHOOD METHODS 9
- 14. NEIGHBORHOOD METHOD We have a history of past ratings We need to predict Alice's rating for Titanic 1. Find the users that have the same tastes as Alice (using the rating history) 2. Average their ratings for Titanic That's it 10
- 15. 1. Find the users that have the same tastes as Alice (using the rating history) 10
- 16. WE NEED A SIMILARITY MEASURE 11
- 17. WE NEED A SIMILARITY MEASURE number of common rated items 11
- 18. WE NEED A SIMILARITY MEASURE number of common rated items average absolute difference between ratings (it's actually a distance) 11
- 19. WE NEED A SIMILARITY MEASURE number of common rated items average absolute difference between ratings (it's actually a distance) cosine angle between and 11
- 20. WE NEED A SIMILARITY MEASURE number of common rated items average absolute difference between ratings (it's actually a distance) cosine angle between and Pearson correlation coefficient between and 11
- 21. WE NEED A SIMILARITY MEASURE number of common rated items average absolute difference between ratings (it's actually a distance) cosine angle between and Pearson correlation coefficient between and About 3 millions other measures 11
- 22. NEIGHBORHOOD METHOD We have a history of past ratings We need to predict Alice's rating for Titanic 1. Find the users that have the same tastes as Alice (using the rating history) 2. Average their ratings for Titanic That's it 12
- 23. 2. Average their ratings for Titanic 12
- 24. AGGREGATING THE NEIGHBORS' RATINGS Alice is close to Bob. Her rating for Titanic is probably close to Bob's (1). So 13
- 25. AGGREGATING THE NEIGHBORS' RATINGS Alice is close to Bob. Her rating for Titanic is probably close to Bob's (1). So But we should of course look at more than one neighbor! 13
- 26. LET'S CODE! def predict_rating(u, i): '''Return estimated rating of user u for item i. rating_hist is a list of tuples (user, item, rating)''' # Retrieve users having rated i neighbors = [(sim[u, v], r_vj) for (v, j, r_vj) in rating_hist if (i == j)] # Sort them by similarity with u neighbors.sort(key=lambda tple: tple[0], reversed=True) # Compute weighted average of the k-NN's ratings num = sum(sim_uv * r_vi for (sim_uv, r_vi) in neighbors[:k]) denum = sum(sim_uv for (sim_uv, _) in neighbors[:k]) return num / denum 14
- 27. LET'S CODE! def predict_rating(u, i): '''Return estimated rating of user u for item i. rating_hist is a list of tuples (user, item, rating)''' # Retrieve users having rated i neighbors = [(sim[u, v], r_vj) for (v, j, r_vj) in rating_hist if (i == j)] # Sort them by similarity with u neighbors.sort(key=lambda tple: tple[0], reversed=True) # Compute weighted average of the k-NN's ratings num = sum(sim_uv * r_vi for (sim_uv, r_vi) in neighbors[:k]) denum = sum(sim_uv for (sim_uv, _) in neighbors[:k]) return num / denum 14
- 28. NEIGHBORHOOD METHOD We have a history of past ratings We need to predict Alice's rating for Titanic 1. Find the users that have the same tastes as Alice (using the rating history) 2. Average their ratings for Titanic That's it... 15
- 29. NEIGHBORHOOD METHOD We have a history of past ratings We need to predict Alice's rating for Titanic 1. Find the users that have the same tastes as Alice (using the rating history) 2. Average their ratings for Titanic That's it... ? 15
- 30. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings 16
- 31. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) 16
- 32. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) Use a fancier aggregation 16
- 33. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) Use a fancier aggregation Discount similarities (give them more or less confidence) 16
- 34. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) Use a fancier aggregation Discount similarities (give them more or less confidence) Use item-item similarity instead 16
- 35. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) Use a fancier aggregation Discount similarities (give them more or less confidence) Use item-item similarity instead Or use both kinds of similarities! 16
- 36. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) Use a fancier aggregation Discount similarities (give them more or less confidence) Use item-item similarity instead Or use both kinds of similarities! Cluster users and/or items 16
- 37. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) Use a fancier aggregation Discount similarities (give them more or less confidence) Use item-item similarity instead Or use both kinds of similarities! Cluster users and/or items Learn the similarities 16
- 38. THERE ARE (APPROXIMATELY) HALF A BILLION VARIANTS Normalize the ratings Remove bias (some users are mean) Use a fancier aggregation Discount similarities (give them more or less confidence) Use item-item similarity instead Or use both kinds of similarities! Cluster users and/or items Learn the similarities Blah blah blah... 16
- 39. OUTLINE 1. THE NEIGHBORHOOD METHOD (K-NN) 2. OVERVIEW OF ( ) 3. MATRIX FACTORIZATION SURPRISE http://surpriselib.com 17
- 40. 2. OVERVIEW OF ( ) SURPRISE http://surpriselib.com 17
- 41. SURPRISE A Python library for recommender systems (Or rather: a Python library for rating prediction algorithms) 18
- 42. WHY? Needed a Python lib for quick and easy prototyping Needed to control my experiments 19
- 43. SO WHY NOT SCIKIT-LEARN? 20
- 44. SO WHY NOT SCIKIT-LEARN? Rating prediction ≠ regression or classification 20
- 45. YES :) NO :( clf = MyClassifier() clf.fit(X_train, y_train) clf.predict(X_test) rec = MyRecommender() rec.fit(X_train, y_train) rec.predict(X_test) 21
- 46. BASIC USAGE from surprise import KNNBasic from surprise import Dataset data = Dataset.load_builtin('ml-100k') # download dataset trainset = data.build_full_trainset() # build trainset algo = KNNBasic() # use built-in prediction algorithm algo.train(trainset) # fit data algo.predict('Alice', 'Titanic') algo.predict('Bob', 'Toy Story') # ... 22
- 47. MAIN FEATURES Easy dataset handling 23
- 48. DATASET HANDLING # Built-in datasets (movielens, Jester) data = Dataset.load_builtin('ml-100k') # Custom datasets (from file) file_path = os.path.expanduser('./my_data') reader = Reader(line_format='user item rating timestamp', sep='t') data = Dataset.load_from_file(file_path, reader=reader) # Custom datasets (from pandas dataframe) reader = Reader(rating_scale=(1, 5)) data = Dataset.load_from_df(df[['uid', 'iid', 'r_ui']], reader) 24
- 49. MAIN FEATURES Built-in prediction algorithms (SVD, k-NN, many others) Built-in similarity measures (Cosine, Pearson...) 25
- 50. BUILT-IN ALGORITHMS AND SIMILARITIES SVD, SVD++, NMF, -NN (with variants), Slope One, some baselines... Cosine, Pearson, MSD (between users or items) sim_options = {'name': 'cosine', 'user_based': False, 'min_support': 10 } algo = KNNBasic(sim_options=sim_options) 26
- 51. MAIN FEATURES Custom algorithms are easy to implement 27
- 52. CUSTOM PREDICTION ALGORITHM class MyRatingPredictor(AlgoBase): def estimate(self, u, i): return 3 algo = MyRatingPredictor() algo.train(trainset) pred = algo.predict('Alice', 'Titanic') # will call estimate 28
- 53. CUSTOM PREDICTION ALGORITHM class MyRatingPredictor(AlgoBase): def train(self, trainset): '''Fit your data here.''' self.mean = np.mean([r_ui for (u, i, r_ui) in trainset.all_ratings()]) def estimate(self, u, i): return self.mean 29
- 54. CUSTOM PREDICTION ALGORITHM class MyRatingPredictor(AlgoBase): def train(self, trainset): '''Fit your data here.''' self.mean = np.mean([r_ui for (u, i, r_ui) in trainset.all_ratings()]) def estimate(self, u, i): return self.mean trainset object: Iterators over the rating history (user-wise, item-wise, or unordered) Iterators over users and items ids Other useful methods/attributes 29
- 55. class MyKNN(AlgoBase): def train(self, trainset): self.trainset = trainset self.sim = ... # Compute similarity matrix def estimate(self, u, i): neighbors = [(self.sim[u, v], r_vi) for (v, r_vj) in self.trainset.ur[u]] neighbors = sorted(neighbors, key=lambda tple: tple[0], reverse=True) sum_sim = sum_ratings = 0 for (sim_uv, r_vi) in neighbors[:self.k]: sum_sim += sim sum_ratings += sim * r return sum_ratings / sum_sim 30
- 56. MAIN FEATURES Cross-validation, grid-search 31
- 57. TYPICAL EVALUATION PROTOCOL Hide some of the (they become ) Use the remaining to predict the 32
- 58. TYPICAL EVALUATION PROTOCOL Hide some of the (they become ) Use the remaining to predict the RMSE = The average error, where big errors are heavily penalized (squared) 32
- 59. TYPICAL EVALUATION PROTOCOL Hide some of the (they become ) Use the remaining to predict the RMSE = The average error, where big errors are heavily penalized (squared) Do that many times with different subsets: this is cross-validation 32
- 60. CROSS VALIDATION data = Dataset.load_builtin('ml-100k') # download dataset data.split(n_folds=3) # 3-folds cross validation algo = SVD() for trainset, testset in data.folds(): algo.train(trainset) # fit data predictions = algo.test(testset) # predict ratings accuracy.rmse(predictions, verbose=True) # print RMSE 33
- 61. GRID-SEARCH param_grid = {'n_epochs': [5, 10], 'lr_all': [0.002, 0.005], 'reg_all': [0.4, 0.6]} grid_search = GridSearch(SVD, param_grid, measures=['RMSE', 'FCP']) data = Dataset.load_builtin('ml-100k') data.split(n_folds=3) grid_search.evaluate(data) # will evaluate each combination print(grid_search.best_score['RMSE']) print(grid_search.best_params['RMSE']) algo = grid_search.best_estimator['RMSE']) 34
- 62. MAIN FEATURES Built-in evaluation measures (RMSE, MAE...) 35
- 63. EVALUATION TOOLS Can also compute Recall, Precision and top-N related measures easily for trainset, testset in data.folds(): algo.train(trainset) # fit data predictions = algo.test(testset) # predict ratings accuracy.rmse(predictions) accuracy.mae(predictions) accuracy.fcp(predictions) 36
- 64. MAIN FEATURES Model persistence 37
- 65. MODEL PERSISTENCE Can also dump the predictions to analyze and carefully compare algorithms with pandas # ... grid search algo = grid_search.best_estimator['RMSE']) # dump algorithm file_name = os.path.expanduser('~/dump_file') dump.dump(file_name, algo=algo) # ... # Close Python, go to bed # ... # Wake up, reload algorithm, profit _, algo = dump.load(file_name) 38
- 66. OUTLINE 1. THE NEIGHBORHOOD METHOD (K-NN) 2. OVERVIEW OF ( ) 3. MATRIX FACTORIZATION SURPRISE http://surpriselib.com 39
- 67. 3. MATRIX FACTORIZATION 39
- 68. MATRIX FACTORIZATION 40
- 69. MATRIX FACTORIZATION A lot of hype during the Netflix Prize (2006-2009: improve our system, get rich) Model the ratings in an insightful way Takes its root in dimensional reduction and SVD 41
- 70. BEFORE SVD: PCA Here are 400 greyscale images (64 x 64): Put them in a 400 x 4096 matrix : 42
- 71. PCA also gives you the . In advance: you don't need all the 400 guys BEFORE SVD: PCA PCA will reveal 400 of those creepy typical guys: These guys can build back all of the original faces 43
- 72. PCA ON A RATING MATRIX? SURE! Assume all ratings are known Exact same thing! We just have ratings instead of pixels. PCA will reveal typical users. 44
- 73. PCA ON A RATING MATRIX? SURE! Assume all ratings are known Exact same thing! We just have ratings instead of pixels. PCA will reveal typical users. 44
- 74. PCA ON A RATING MATRIX? SURE! Assume all ratings are known. Transpose the matrix Exact same thing! PCA will reveal typical movies. 45
- 75. PCA ON A RATING MATRIX? SURE! Assume all ratings are known. Transpose the matrix Exact same thing! PCA will reveal typical movies. Note: in practice, the factors semantic is not clearly defined. 45
- 76. SVD IS PCA2 PCA on gives you the typical users PCA on gives you the typical movies SVD gives you both in one shot! 46
- 77. SVD IS PCA2 PCA on gives you the typical users PCA on gives you the typical movies SVD gives you both in one shot! is diagonal, it's just a scaler. This is our matrix factorization! 46
- 78. THE MODEL OF SVD 47
- 79. THE MODEL OF SVD 47
- 80. THE MODEL OF SVD 47
- 81. THE MODEL OF SVD Rating(Alice, Titanic) will be high Rating(Bob, Titanic) will be low 47
- 82. SO HOW TO COMPUTE AND ? Columns of are the eigenvectors of Columns of are the eigenvectors of Associated eigenvalues make up the diagonal of There are very efficient algorithms in the wild 48
- 83. SO HOW TO COMPUTE AND ? Columns of are the eigenvectors of Columns of are the eigenvectors of Associated eigenvalues make up the diagonal of There are very efficient algorithms in the wild Alternate option: find the s and the s that minimize the reconstruction error (With some orthogonality constraints). There are also very efficient algorithms in the wild 48
- 84. SO HOW TO COMPUTE AND ? Columns of are the eigenvectors of Columns of are the eigenvectors of Associated eigenvalues make up the diagonal of There are very efficient algorithms in the wild Alternate option: find the s and the s that minimize the reconstruction error (With some orthogonality constraints). There are also very efficient algorithms in the wild So, piece of cake right? 48
- 85. OOPS! 49
- 86. WE ASSUMED THAT WAS DENSE But it's not! 99% are missing and are not even defined So and are not defined either There is no SVD 50
- 87. WE ASSUMED THAT WAS DENSE But it's not! 99% are missing and are not even defined So and are not defined either There is no SVD Two options: Fill the missing entries with a simple heuristic " Let's just not give a damn " -- Simon Funk (ended- up top-3 of the Netflix Prize for some time) 50
- 88. Dense case: find the s and the s that minimize the total reconstruction error (With orthogonality constraints) Sparse case: find the s and the s that minimize the partial reconstruction error (Forget about orthogonality) APPROXIMATION 51
- 89. Dense case: find the s and the s that minimize the total reconstruction error (With orthogonality constraints) Sparse case: find the s and the s that minimize the partial reconstruction error (Forget about orthogonality) APPROXIMATION Basically the same, except we don't have all the ratings (and we don't care) 51
- 90. We want the value such that is minimal: Compute Randomly initialize (do this until you're fed up) MINIMIZATION BY GRADIENT DESCENT 52
- 91. MINIMIZATION BY (STOCHASTIC) GRADIENT DESCENT Our parameter is for all and . The function to be minimized is: def compute_SVD(): '''Fit pu and qi to known ratings by SGD''' p = np.random.normal(size=F) q = np.random.normal(size=F) for iter in range(n_max_iter): for u, i, r_ui in rating_hist: err = r_ui - np.dot(p[u], q[i]) p[u] = p[u] + learning_rate * err * q[i] q[i] = q[i] + learning_rate * err * p[u] def estimate_rating(u, i): return np.dot(p[u], q[i]) 53
- 92. SOME LAST DETAILS Unbias the ratings, add regularization: you get "SVD": 54
- 93. SOME LAST DETAILS Unbias the ratings, add regularization: you get "SVD": Good ol' fashioned ML paradigm: Assume a model for your data ( ) Fit your model parameters to the observed data Praise the Lord and hope for the best 54
- 94. A FEW REMARKS is mostly a tool for algorithms that predict ratings. RS do much more than that. Focus on explicit ratings (implicit ratings algorithms will come, hopefully) Not aimed to be a complete tool for building RS from A to Z (check out ) Not aimed to be the most efficient implementation (check out ) Surprise Mangaki LightFM 55
- 95. SOME REFERENCES Can't recommend enough (pun intended) Aggarwal's Recommender Systems - The Textbook Jeremy Kun's (great insights on and )blog PCA SVD 56
- 96. THANKS! 57

No public clipboards found for this slide

Be the first to comment