
As part of our unsupervised learning series today, we will talk about a famous algorithm of an association type. While exploring unsupervised learning, I came across the Apriori Algorithm. At first, it sounded complex, but once I broke it down with real-life examples, it started to make sense.

The Apriori Algorithm is one of the most fundamental techniques in data mining and unsupervised machine learning. It is widely used to discover patterns, relationships, and associations between items in large datasets.In simple terms, Apriori helps answer questions like:
“Which items are frequently bought together?”
This makes it extremely valuable in areas such as market basket analysis, recommendation systems, and customer behavior analysis.
Think of it as answering questions like:
What items do people usually buy together?
What actions often happen together?
What combinations appear frequently in data?
This idea is known as association rule mining.
Example: Imagine a grocery store analyzing customer purchases.They notice:
This information can help the store t0 create combo offers, improve recommendations, and arrange shelves smartly This is exactly what Apriori helps us do, but at scale.
A single purchase or event containing multiple items.
Example:
{Milk, Bread, Butter}
A group of one or more items.
Examples:
1-itemset: {Milk}
2-itemset: {Milk, Bread}
3-itemset: {Milk, Bread, Butter}
Measures how frequently an itemset appears in the dataset.

Measures how likely item Y is purchased when item X is purchased.

Measures how much more likely two items are purchased together compared to random chance.
Lift > 1 → strong positive association
Lift = 1 → independent
Lift < 1 → negative association

Before starting, define: Minimum Support and Minimum Confidence. These values control how strict the algorithm is.
Scan the dataset and count how often each item appears.
Example Dataset
| Transaction | Items |
| T1 | {Laptop, Mouse, Keyboard} |
| T2 | {Laptop, Mouse} |
| T3 | {Keyboard, Mouse} |
If minimum support = 50%:
{Laptop} → 66.7%
{Mouse} → 100%
{Keyboard} → 66.7%
All are frequent.
Combine frequent itemsets to form:
2-itemsets
3-itemsets
and so on
Example 2-itemsets:
{Laptop, Mouse}
{Laptop, Keyboard}
{Mouse, Keyboard}
If any subset of an itemset is not frequent, discard the entire itemset.
Example:
{Laptop, Keyboard} has low support → removed
Saves computation time
The process continues until no more frequent itemsets can be generated.
From frequent itemsets, generate rules of the form:
Example:
{Laptop} ⇒ {Mouse}Only keep rules that meet the minimum confidence threshold.
Example
{Laptop} ⇒ {Mouse}We’ll simulate a small grocery store dataset and discover frequent itemsets and association rules.
# installing libraires
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rules
# creating simple dataset
transactions = [
['Milk', 'Bread', 'Butter'],
['Milk', 'Bread'],
['Milk', 'Butter'],
['Bread', 'Butter'],
['Milk', 'Bread', 'Butter'],
['Bread'],
]
# encoding
te = TransactionEncoder()
te_array = te.fit(transactions).transform(transactions)
df = pd.DataFrame(te_array, columns=te.columns_)
df
# finding frequest itemset
frequent_itemsets = apriori(
df,
min_support=0.4,
use_colnames=True
)
frequent_itemsets
# generate rules
rules = association_rules(
frequent_itemsets,
metric="confidence",
min_threshold=0.6
)
rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']]

Support tells “how common” . Confidence tells “how reliable”. Lift tells “how meaningful.”
Each row represents an association rule written in the form: IF (antecedent) → THEN (consequent).
For example
(Milk) → (Butter) (row 5)
Support = 0.5 → Bought together in half of all transactions
Confidence = 0.75 → 75% of Milk buyers also buy Butter
Lift = 1.125 → Customers are 12.5% more likely to buy Butter when they buy Milk
This is a strong and useful association.
E-commerce: Recommends products that are frequently bought together to increase sales.
Food Delivery Apps: Identifies popular food combinations to create attractive combo deals.
Streaming Platforms: Suggests movies or shows based on content users often watch together.
Finance: Analyzes spending patterns to offer personalized credit cards or discounts.
Travel & Hospitality: Builds travel bundles like flight + hotel based on common bookings.
Apriori doesn’t predict numbers; it reveals hidden buying behavior. That’s why it’s widely used in recommendation systems, combo offers, and business strategy. This algorithm is a cornerstone of association rule mining. While it may not be the fastest algorithm today, it provides a clear conceptual foundation for understanding how machines discover hidden patterns in data.