collaborative filtering for fun ...and profit!
DESCRIPTION
This is a fun presentation prepared for 30 high school-aged young ladies at the Girls Who Code summer camp. It's an introduction to concepts of collaborative filtering implemented in Python.TRANSCRIPT
![Page 1: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/1.jpg)
Collaborative Filtering for fun Elissa Brown Erin Shellman Stella Rowlett
…and profit!💖 😹💻
![Page 2: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/2.jpg)
![Page 3: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/3.jpg)
![Page 4: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/4.jpg)
![Page 5: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/5.jpg)
How’s it done?‣ Collaborative filtering!
‣ Look for people who like the stuff you like, and recommend the things they’ve rated positively that you haven’t seen yet.
![Page 6: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/6.jpg)
Leaf Shorts Floral ShortsElissa 5 5
Erin 2 5
Stella 4 1
Step 1: Collect ratings
![Page 7: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/7.jpg)
1
2
3
4
5
2 3 4 5Leaf Shorts
Flor
al S
horts Person
ElissaErinStella
![Page 8: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/8.jpg)
Step 2: Find someone similar for Mr. New Customer‣ A new customer wants to
buy a pair of shorts for his new “girlfriend.”
‣Which of us is most similar to Justin?
💋
💍
💘
💌
![Page 9: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/9.jpg)
Finding the nearest neighbor‣Remember the
Pythagorean Theorem?
a2 + b2 = c2
c =p
a2 + b2p(x1 � x2)2 + (y1 � y2)2
p(1� 5)2 + (4� 5)2 = 4.12
1
2
3
4
5
1 2 3 4 5Leaf Shorts
Flor
al S
horts
PersonElissaErinJustinStella
![Page 10: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/10.jpg)
What’s the distance between Justin and Stella?
1
2
3
4
5
1 2 3 4 5Leaf Shorts
Flor
al S
horts
PersonElissaErinJustinStella?
![Page 11: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/11.jpg)
What’s the distance between Justin and Stella?
1
2
3
4
5
1 2 3 4 5Leaf Shorts
Flor
al S
horts
PersonElissaErinJustinStella
p(1� 4)2 + (1� 4)2 = 4.24
![Page 12: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/12.jpg)
Meant to be!‣Justin and Erin are a
match made in heaven.
‣How can this help us shop for Justin’s girlfriend (not Erin)?p
(1� 2)2 + (4� 5)2 = 1.41
1
2
3
4
5
1 2 3 4 5Leaf Shorts
Flor
al S
horts
PersonElissaErinJustinStella
💘
![Page 13: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/13.jpg)
Leaf Shorts Floral Shorts Jungle ShortsElissa 5 5 1
Erin 2 5 5
Stella 4 1 2
Justin 1 4 ?
Step 3: Make a suggestion!
![Page 14: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/14.jpg)
Let’s go shopping!💃
![Page 15: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/15.jpg)
How should we store our ratings data?
![Page 16: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/16.jpg)
How should we store our ratings data?
A dictionary!
# Store user ratings user_ratings = {"Elissa": {"Leaf Shorts": 5, "Floral Shorts": 5}, "Erin": {"Leaf Shorts": 2, "Floral Shorts": 5}, "Stella": {"Leaf Shorts": 4, "Floral Shorts": 1} }
![Page 17: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/17.jpg)
Python Warm-up!# Store user ratings user_ratings = {"Elissa": {"Leaf Shorts": 5, "Floral Shorts": 5}, "Erin": {"Leaf Shorts": 2, "Floral Shorts": 5}, "Stella": {"Leaf Shorts": 4, "Floral Shorts": 1} } !# Print Stella's ratings. print user_ratings['Stella'] !# What did Elissa think of the floral ones? print user_ratings['Elissa']['Leaf Shorts'] !def what_did_they_think(rating): if rating > 3: opinion = "LOVED IT!" else: opinion = "HATED IT!" return opinion !my_humble_opinion = what_did_they_think(user_ratings['Erin']['Leaf Shorts']) !print "Erin's review of the Leaf Shorts: " + my_humble_opinion !!
![Page 18: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/18.jpg)
What functions do we need to build a recommender?
![Page 19: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/19.jpg)
What functions do we need to build a recommender?1. Function to compute distances
2. Function to find nearby people
3. Function to recommend items I haven’t rated yet
![Page 20: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/20.jpg)
Pseudocode: Function to compute distances
![Page 21: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/21.jpg)
compute_distancedef compute_distance(user1_ratings, user2_ratings): """ This function computes the distance between two user's ratings. Both arguments should be dictionaries keyed on users, and items. """ distances = [] for key in user1_ratings: if key in user2_ratings: distances.append((user1_ratings[key] - user2_ratings[key]) ** 2) total_distance = round(sum(distances) ** 0.5, 2) return total_distance
![Page 22: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/22.jpg)
Pseudocode: Function to find closest match
![Page 23: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/23.jpg)
find_nearest_neighborsdef find_nearest_neighbors(username, user_ratings): """ Returns the list of neighbors, ordered by distance. Call like this: find_nearest_neighbors('Erin', user_ratings) """ distances = [] for user in user_ratings: if user != username: distance = compute_distance(user_ratings[user], user_ratings[username]) distances.append((distance, user)) distances.sort() return distances
![Page 24: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/24.jpg)
Pseudocode: Function to make recommendations
![Page 25: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/25.jpg)
get_recommendationsdef get_recommendations(username, user_ratings): """ Return a list of recommendations. """ nearest_users = find_nearest_neighbors(username, user_ratings) recommendations = [] ! # Input user's ratings ratings = user_ratings[username] ! for neighbor in nearest_users: neighbor_name = neighbor[1] for item in user_ratings[neighbor_name]: if not item in ratings: recommendations.append((item, user_ratings[neighbor_name][item])) ! return sorted(recommendations, key = lambda personTuple: personTuple[1], reverse = True)
![Page 26: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/26.jpg)
Limitations‣ What happens if everyone rated the
same group of items?
‣ What if there’s no overlap?
‣ What about new users with no ratings?
![Page 27: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/27.jpg)
Now what?‣Ask your classmates to rate their electives,
and make a recommender to help students pick their classes.
‣Poll recent graduates from your school about their college, and make a recommender to help students pick colleges.
![Page 28: Collaborative Filtering for fun ...and profit!](https://reader033.vdocuments.us/reader033/viewer/2022051323/549f7446ac79591f768b4a23/html5/thumbnails/28.jpg)
https://github.com/erinshellman/girls-who-code-recommender
🐱