Absolutely! Association rules, frequent itemsets, and the Apriori algorithm are core concepts inmarket basket analysisandunsupervised learning. Let’s break them down step by step, and you can let me know where you'd like to dive deeper.
🔑 1.Frequent Itemsets
These aregroups of items(e.g., {milk, bread}) that appear together frequently in a dataset.
Support: The proportion of transactions that contain the itemset.Support(A,B)=Number of transactions containing both A and BTotal number of transactions\text{Support}(A,B) = \frac{\text{Number of transactions containing both A and B}}{\text{Total number of transactions}}Support(A,B)=Total number of transactionsNumber of transactions containing both A and B
Example:If 100 out of 1000 customers bought both milk and bread, the support is 0.10.
If 100 out of 1000 customers bought both milk and bread, the support is 0.10.
Support: The proportion of transactions that contain the itemset.
Example:
If 100 out of 1000 customers bought both milk and bread, the support is 0.10.
If 100 out of 1000 customers bought both milk and bread, the support is 0.10.
🔁 2.Apriori Algorithm
TheApriori algorithmis used to findfrequent itemsets. It's based on theApriori principle:
If an itemset isfrequent, then all of itssubsets must also be frequent.
Steps:
Find frequent itemsets of size 1 (e.g., {milk}).
Use those to generate candidates of size 2 (e.g., {milk, bread}).
Prune candidates that have infrequent subsets.
Continue increasing the size until no more frequent itemsets are found.
Find frequent itemsets of size 1 (e.g., {milk}).
Use those to generate candidates of size 2 (e.g., {milk, bread}).
Prune candidates that have infrequent subsets.
Continue increasing the size until no more frequent itemsets are found.
You set aminimum support threshold(e.g., 0.05) to keep only itemsets that appear often enough.
🔁 3.Association Rules
From frequent itemsets, we deriveruleslike:
This means:If a person buys milk and bread, they are likely to buy butter.
Each rule has:
Support: As above.
Confidence: How often the rule is true.Confidence(A→B)=Support(A∪B)Support(A)\text{Confidence}(A \rightarrow B) = \frac{\text{Support}(A \cup B)}{\text{Support}(A)}Confidence(A→B)=Support(A)Support(A∪B)
Lift: How much more likely B is given A, compared to random chance.Lift(A→B)=Confidence(A→B)Support(B)\text{Lift}(A \rightarrow B) = \frac{\text{Confidence}(A \rightarrow B)}{\text{Support}(B)}Lift(A→B)=Support(B)Confidence(A→B)Lift > 1 suggests apositive association
Lift > 1 suggests apositive association
Support: As above.
Confidence: How often the rule is true.
Lift: How much more likely B is given A, compared to random chance.
Lift > 1 suggests apositive association
Lift > 1 suggests apositive association
👩🍳 Example (Tiny Market Basket)
['Transaction ID', 'Items']
['1', 'Milk, Bread, Butter']
['2', 'Milk, Bread']
['3', 'Milk, Butter']
['4', 'Bread, Butter']
['5', 'Milk, Bread, Butter']
{Milk, Bread} → {Butter}Support: 3/5 = 0.6Confidence: 3/4 = 0.75 (since Milk & Bread appeared in 4 transactions)Lift: 0.75 / 0.8 = 0.9375
Support: 3/5 = 0.6
Confidence: 3/4 = 0.75 (since Milk & Bread appeared in 4 transactions)
Lift: 0.75 / 0.8 = 0.9375
{Milk, Bread} → {Butter}
Support: 3/5 = 0.6
Confidence: 3/4 = 0.75 (since Milk & Bread appeared in 4 transactions)
Lift: 0.75 / 0.8 = 0.9375
Support: 3/5 = 0.6
Confidence: 3/4 = 0.75 (since Milk & Bread appeared in 4 transactions)
Lift: 0.75 / 0.8 = 0.9375
Would you like to:
See R or Python code for Apriori?
Visualize the rules?
Learn how to set thresholds (support, confidence)?
Apply to a real dataset?
See R or Python code for Apriori?
Visualize the rules?
Learn how to set thresholds (support, confidence)?
Apply to a real dataset?
Let’s go where your curiosity leads!