TGViewer
Data Science & Machine Learning Data Science & Machine Learning @datasciencefun · 77.8K subscribers
Post #1734 8.69K
Let's start with Day 12 today

30 Days of Data Science Series: https://t.me/datasciencefun/1708

Let's learn about Association Rule Learning

Concept: Association rule learning is a rule-based machine learning method used to discover interesting relations between variables in large databases. It is widely used in market basket analysis to identify sets of products that frequently co-occur in transactions. The main goal is to find strong rules discovered in databases using some measures of interestingness.

#### Key Terms
- Support: The proportion of transactions in the dataset that contain a particular itemset.
- Confidence: The likelihood that a transaction containing an itemset A also contains an itemset B .
- Lift: The ratio of the observed support to that expected if A and B were independent.

#### Algorithm
The most common algorithm for association rule learning is the Apriori algorithm. It operates in two steps:
1. Frequent Itemset Generation: Identify all itemsets whose support is greater than or equal to a specified minimum support threshold.
2. Rule Generation: From the frequent itemsets, generate high-confidence rules where confidence is greater than or equal to a specified minimum confidence threshold.

#### Implementation

Let's consider an example using Python and its libraries.

##### Example
Suppose we have a dataset of transactions, and we want to identify frequent itemsets and generate association rules.

# Import necessary libraries
import pandas as pd
from mlxtend.frequent_patterns import apriori, association_rules

# Example data: list of transactions
data = {'TransactionID': [1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4],
'Item': ['Milk', 'Bread', 'Butter', 'Bread', 'Butter', 'Milk', 'Bread', 'Eggs', 'Milk', 'Bread', 'Butter', 'Eggs']}

df = pd.DataFrame(data)
df = df.groupby(['TransactionID', 'Item'])['Item'].count().unstack().reset_index().fillna(0).set_index('TransactionID')
df = df.applymap(lambda x: 1 if x > 0 else 0)

# Applying the Apriori algorithm
frequent_itemsets = apriori(df, min_support=0.5, use_colnames=True)

# Generating association rules
rules = association_rules(frequent_itemsets, metric='confidence', min_threshold=0.7)

print("Frequent Itemsets:")
print(frequent_itemsets)
print("\nAssociation Rules:")
print(rules)

#### Explanation of the Code

1. Libraries: We import necessary libraries like pandas and mlxtend.
2. Data Preparation: We create a transaction dataset and transform it into a format suitable for the Apriori algorithm, where each row represents a transaction and each column represents an item.
3. Apriori Algorithm: We apply the Apriori algorithm to find frequent itemsets with a minimum support of 0.5.
4. Association Rules: We generate association rules from the frequent itemsets with a minimum confidence of 0.7.

#### Evaluation Metrics

- Support: Measures the frequency of an itemset in the dataset.
- Confidence: Measures the reliability of the inference made by the rule.
- Lift: Measures the strength of the rule over random co-occurrence. Lift values greater than 1 indicate a strong association.

#### Applications

Association rule learning is widely used in:
- Market Basket Analysis: Identifying products frequently bought together to optimize store layouts and cross-selling strategies.
- Recommendation Systems: Recommending products or services based on customer purchase history.
- Healthcare: Discovering associations between medical conditions and treatments.

Best Data Science & Machine Learning Resources: https://topmate.io/coding/914624

Credits: t.me/datasciencefun

ENJOY LEARNING 👍👍
  • 👍 12
  • ❤ 7
  • 🔥 1
  • 👏 1
More from @datasciencefun
  1. Oct 9, 2026Post #4657
  2. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  3. Oct 8, 2026🔬 Kandinsky 6.0 Video: Training Details and Architecture The new Kandinsky 6.0 Video line…
  4. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  5. Oct 7, 2026ORDER BY salary DESC LIMIT 5; Mistake 3 — Forgetting that LIMIT applies after sorting For…
  6. Oct 7, 2026🚀 Data Science Roadmap 2026 📍 Phase 3: SQL for Data Science 📖 Topic 4 — LIMIT LIMIT is…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →