Recommendation System
Lecture 02
CMSC 320 - Introduction to Data Science
2026
COLLABORATIVE FILTERING
TYPE 2: Item-based collaborative filtering
COLLABORATIVE FILTERING: ANOTHER VERSION
Alternative view that often works better: Item-Item (NEXT)
COLLABORATIVE FILTERING: TYPES
Item-based collaborative filtering:
Item-Based CF recommends items based on similarity between items
CORE IDEA:
How?
similarity is based solely on user-item interactions and does not consider the content features of items. |
Item-based collaborative filtering
But how do we know about this ‘similarity’?
we compute similarity using user ratings.
Item-based collaborative filtering
If same users give similar ratings to two items, then those items are considered similar
Item-based CF
Two completely unrelated movies could be "similar" just because the same audience watches and rates them the same way.
STEPS OF Item-based collaborative filtering
Courtesy: Jure Leskovec & Mina Ghashami, Stanford
Algorithm: “Rate an unseen item i as the mean of my ratings for other items, weighted by their similarity to i.”
Note: The matrix is sparse, but the empty cells are not (necessarily) zero!
EXAMPLE (ITEM-ITEM CF)
Item-based collaborative filtering: Let’s say, |N| =2 (the size of the neighborhood, or the number of similar items)
Figure: User - Item Interaction Matrix where Movie Ratings is between [1-5].
EXAMPLE (ITEM-ITEM CF; |N|=2)
Let’s say, we want to know what USER 5 think about Movie 1?
You want to predict whether User 5 will like Movie 1, given that they already liked Movies 3, 4, 5, and 6.
Notes: Here,
EXAMPLE (ITEM-ITEM CF; |N|=2): NEIGHBOUR SELECTION
Let’s say, we want to know what USER 5 think about Movie 1?
Let’s say, we already know that, Movie 3 and Movie 6 are most similar to Movie 1.
We now need to calculate similarity between these rows!
Step 2: Similarity measure between Items (using cosine similarity)
When computing similarity between two movies, we must use only users who have rated both movies (overlapping co-rated users).
s(1,M)
1.0
0.75
0.97
0.89
0.86
1.0
** Common users = intersection of users who rated both items.
N=2. so movie 3 and 6 are most 2 similar movies to movie 5
Step 2: Similarity measure between Items (using cosine similarity)
When computing similarity between two movies, we must use only users who have rated both movies (overlapping co-rated users).
s(1,M)
1.0
0.75
0.97
0.89
0.86
1.0
** Common users = intersection of users who rated both items.
N=2. so movie 3 and 6 are most 2 similar movies to movie 5
Step 2: Similarity measure between Items (using cosine similarity)
** Common users = intersection of users who rated both items.
Item- Item Similarity Calculation Summary
Step 3: PREDICT AND RECOMMEND using Cosine Similarity
what USER 5 think about Movie 1?
User 5 rated:
So both top-2 movies are usable.
APPROXIMATE RATING WITH WEIGHTED MEAN
Predicted rating for User 5 on Movie 1 = 2.51.
2.51
OPTION 2: Pearson Correlation
We will use same example but this time will use Pearson Correlation (Centered Cosine) instead of cosine similarity to calculate similarity between items.
What USER 5 think about Movie 1?
Here we use “mean centered item-overlap cosine as similarity:
Let’s say, we already know that, Movie 3 and Movie 6 are most similar to Movie 1.
EXAMPLE (ITEM-ITEM CF; |N|=2): SIMILARITY CALCULATION
Let’s say, we want to know what USER 5 think about Movie 1?
Compute Cosine Similarity S(1,m):
2. Compute (item-overlapping) cosine similarities between rows
EXAMPLE (ITEM-ITEM CF; |N|=2): SIMILARITY CALCULATION
Let’s say, we want to know what USER 5 think about Movie 1?
Compute Similarity Weight:
S1,3 = 0.658
S1,6= 0.768
We computed S1,2, S1,4,S1,6 too! Let’s assume those are smaller!
Sim(1,m)
1.000
…
0.658
…
…
0.768
EXAMPLE (ITEM-ITEM CF; |N|=2): APPROXIMATE RATING WITH WEIGHTED MEAN
Let’s say, we want to know what USER 5 think about Movie 1?
Predict By Taking Weighted Average:
Ruser1,movies5 = R1,5 =
(0.658*2 +0.768*3) / (0.658+0.768)
=2.54
~ 2.6
Sim(1,m)
1.000
…
0.658
…
…
0.768
EXAMPLE (ITEM-ITEM CF; |N|=2): APPROXIMATE RATING WITH WEIGHTED MEAN
Let’s say, we want to know what USER 5 think about Movie 1?
Predict By Taking Weighted Average:
Ruser1,movies5 = R1,5 =
(0.658*2 +0.768*3) / (0.658+0.768)
=2.54
~ 2.6
Pros and Cons of Item based CF
Advantages | Limitations |
“You liked HP1, so we recommend HP3” |
New users → no history, New items → no ratings
Common Solution: Use hybrid systems; combine collaborative + content-based filtering |
Next: EVALUATION OF RECOMMENDATION SYSTEM
EVALUATION OF RECOMMENDATION SYSTEM
As always, before we use this algorithm, we'd like to know how well it performs!
What unique challenges are present here?
A sample user:
Ant Man Endgame Thor 1 Return of Thor Iron Dude
Justin 1 0 1 0 1
The challenge: We don't know if no rating means the person doesn't know about the movie, or if they do know about it and knew not to watch it because they wouldn't like it.
EVALUATION OF RECOMMENDATION SYSTEM
EVALUATION OF RECOMMENDATION SYSTEM cont.
RMSE (Root Mean Square Error):
RMSE = 0.8 → Predicts ratings within ±0.8 on average. |
Precision at Top 10: (how many of those top ten things the user actually liked or bought)
Precision@10 = 70% → 7/10 recommendations are relevant. |
Rank Correlation (Pearson)
|
PER USER LEAVE-ONE-OUT-CROSS-VALIDATION
For each user, leave out one thing they've rated, and then predict the rating.
This tells us about how well we do at the things the user has seen (already rated), but we have no idea how they're doing with the things they haven't (new, not rated yet).
Collaborative vs Content Based Differences
(SOME MORE)�RECOMMENDER SYSTEMS (ISH)
26
ASSOCIATION RULES
In today's data-driven world, businesses strive to understand the relationships between different products or items purchased together by customers.
This understanding allows them to optimize marketing strategies, enhance product recommendations, and improve overall customer experience
ASSOCIATION RULES
Collaborative Filtering predicts preferences based on similar users' ratings
Complementary idea: Find rules that associate/connect the presence of one set of items with that of another set of items
28
Association rules are about finding connections between different sets of items. For example, if people often buy chips when they buy salsa, that's an association rule. It helps us understand how items are related to each other in a shopping basket.
ASSOCIATION RULES and RECOMMENDATION SYSTEM
We will now learn
Association rules
Idea: Association rules discover relationships between items that frequently occur together in transactions.
(when customers buy diapers, they are likely to also buy beer.)
Buying one set of items doesn't cause the purchase of another set. Instead, they tend to occur together frequently in transactions.
DEFINITION: FREQUENT ITEMSET
= 0/4 0r 40%
(or Frequency)
Association Rule Evaluations Matrics
S: measure the frequency of items appearing together.
C: How much more likely two items are bought together than by chance.
How often B is bought after A.
measures how likely it is for an item (consequent) to be bought when another set of items (antecedents) is already in the cart.
ASSOCIATION RULE MINING TASK
From a set of transactions T, find all rules where:
(Thresholds are set using business needs, domain knowledge, or trial and error.)
Brute-force approach:-
Computationally prohibitive!
Example: MINING ASSOCIATION RULES
Observations:
MINING ASSOCIATION RULES: TWO STEPS
Two-step approach:
1. Frequent Itemset Generation
2. Rule Generation
FREQUENT ITEMSET GENERATION IS STILL COMPUTATIONALLY EXPENSIVE
ONE SOLUTION: REDUCING NUMBER OF CANDIDATES
Apriori principle:- If an itemset is frequent, then all of its subsets must also be frequent
Illustrating Apriori Principle
Next:
Illustrating Apriori Principle for “Frequent Itemset Generation”
Set minimum support threshold.�
Find all frequent 1-itemsets.�
Repeat (generate 3-itemsets, then 4, etc.) until no more frequent itemsets.
ILLUSTRATING APRIORI PRINCIPLE
Given:
Calculate Support Count or Frequency
STEP 01: “1-ITEMSET GENERATION
Minimum Support Count (min_sup) = 3
ILLUSTRATING APRIORI PRINCIPLE
Frequent Itemset Generation
Given:
STEP 02: “2-ITEMSET GENERATION” FROM “1-ITEMSETS
Calculate Support Count or Frequency
Minimum Support Count (min_sup) = 3
ILLUSTRATING APRIORI PRINCIPLE
Frequent Itemset Generation
Given:
STEP 03: “3-ITEMSET GENERATION” FROM “2-ITEMSETS
2
Minimum Support Count (min_sup)= 3
ILLUSTRATING APRIORI PRINCIPLE
Frequent Itemset Generation
Given:
2
Minimum Support Count (min_sup) = 3
ILLUSTRATING APRIORI PRINCIPLE
Frequent Itemset Generation
Given:
Support
(No need to
generate candidates involving {Bread, Bear} or {Milk Bear}
2
Minimum Support
Count (min_sup) = 3
APRIORI ALGORITHM/ PRINCIPLE
Rule Generation: How to efficiently generate rules from frequent itemsets?
1 - FREQUENT ITEM | SUPPORT COUNT |
BREAD | 4 |
MILK | 4 |
BEER | 3 |
DIAPER | 4 |
2 - FREQUENT ITEM | SUPPORT COUNT |
{BREAD,MILK} | 3 |
{BREAD,DIAPER} | 3 |
{MILK,DIAPER} | 3 |
{BEER, ,DIAPER} | 3 |
Given, min confidence = 70%
Confidence (X →Y) :
Our frequent itemsets: {BREAD}, {MILK}, {BEER}, {DIAPER},{BREAD,MILK}, {BREAD,DIAPER}, {MILK,DIAPER}, {BEER, ,DIAPER}
Therefore, candidate rules are:
For {BREAD,MILK}, there are two possibilities:
For {BEER, ,DIAPER}, there are two possibilities:
Continue the proceed for other all frequent datasets.
ANOTHER EXAMPLE: Rule Generation: LETS SAY {BREAD,MILK, DIAPER} is FREQUENT
1 - FREQUENT ITEM | SUPPORT COUNT |
BREAD | 4 |
MILK | 4 |
BEER | 3 |
DIAPER | 4 |
3 - FREQUENT ITEM | SUPPORT COUNT |
{BREAD,MILK, DIAPER} | 3 ( WE ARE CONSIDERING) |
2 - FREQUENT ITEM | SUPPORT COUNT |
{BREAD,MILK} | 3 |
{BREAD,DIAPER} | 3 |
{MILK,DIAPER} | 3 |
{BEER, ,DIAPER} | 3 |
Given, min confidence = 70%
Confidence (X →Y) :
We have 5 frequent itemsets: {BREAD,MILK},
{BREAD,DIAPER}, {MILK,DIAPER}, {BEER, ,DIAPER}, {BREAD,MILK, DIAPER}
For {BREAD,MILK ,DIAPER}, some possibilities:
Another Example: Apriori Principle
There is only one itemset with minimum support 2. So
Try �[Coke^Chips]=>[Hot Dogs] ,
[Hot Dogs]=>[Coke^Chips] Assoc Rules
(min_sup)
The End
Additional Reading
Try if you are interested in collaborative filtering:
ITEM-ITEM vs USER-USER
In practice, it has been observed that often works better than user-user item-item!
Example:
Alice likes action but not romance.
Bob likes comedy and romance but not action.
Item-based filtering suggests movies based on what Alice or Bob liked, fitting their unique tastes.
Example: User Based Neighboorhood Collaborative Filtering
Scenario: Suppose User A and User B have similar movie preferences. User A has watched and liked movies X, Y, and Z.
Process: The system identifies User B as a similar user based on their past movie preferences. If User B has also liked movies X, Y, and Z, the system recommends other movies that User B has liked but User A hasn't seen yet.
Example: User A likes action movies, and the system identifies User B as having similar tastes. User B has watched and enjoyed movies W, X, and Y. The system recommends movie W to User A because it's likely to align with their preferences.
Example: Item Based Filtering
Scenario: Consider a scenario where a user has watched and liked movie X.
Process: The system identifies other movies that are similar to movie X based on certain criteria such as genre, actors, or director. It then recommends these similar movies to the user.
Example: User A likes the movie X, which is a science fiction film featuring actor A and directed by Director B. The system identifies other science fiction movies with actor A or directed by Director B, such as movies Y and Z, and recommends them to User A because of their similarity to movie X.