From Transactions to Trends: The Apriori Algorithm
The Apriori algorithm is very popular and well-known mainly because of its simplicity and straight-forwardness. Apriori reduces the search space significantly by pruning infrequent itemsets early, making it efficient for small to medium-sized datasets.
Steps that are followed in the Apriori Algorithm:
i. Counting the frequency of each item in the dataset and applying the Minimum Support rule:
The Apriori algorithm starts by calculating the frequency of each item in the dataset. These are called 1-item sets. It now uses a rule called minimum support. In simple words, it is a number that tells us how often the items should appear in the dataset. Further, it discards all the items from the dataset that do not meet the minimum support.
ii. Creating Item groups:
After completing Step 1, with the residue 1-item sets, the algorithm combines them into groups of 2 called the 2-item sets. Then it again uses the minimum support rule and discards the sets that do not meet the threshold value of the support. This process is repeated iteratively by forming groups of size 3, 4, and so on, until no further frequent itemsets can be generated.
iii. Removing the infrequent groups:
Now, the algorithm removes the infrequent groups simultaneously using the Apriori Property. According to this property, if a particular group does not appear frequent then its larger group is also infrequent and thus it discards that particular group. If an itemset is infrequent, all its supersets are also infrequent.
Let’s take an example to understand this algorithm.
Step 1:
- T-shirt → appears in transactions 1, 2, 3 → frequency = 3
- Footwear → appears in transactions 1, 2 → frequency = 2
- Food item → appears in transactions 1, 3 → frequency = 2
Step 2:
Combine them into sets of 2 like {T-shirt, Footwear}, {Food item, Footwear}, and {T-shirt, Food item}. Filter out the ones that don’t meet the minimum support required. Continue the same with sets of 3.
Step 3:
Finally, we get association rule like: {T-shirt, Footwear} → {Food item}
This rule is generated after filtering frequent itemsets using minimum support and confidence thresholds.
Implementation of Apriori Algorithm in Python
Step 1: Downloading the required libraries
pip install mlxtenStep 2: Importing the libraries
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori, association_rulesStep 3: Loading the dataset
from google.colab import drive
drive.mount('/content/drive')
file_path = '/content/drive/MyDrive/market_basket.csv.xlsx'
df = pd.read_excel(file_path)
df.head()(Here’s the link for the dataset used — Dataset )
Step 4: Data Preprocessing
transactions = []
for i in range(len(df)):
transactions.append([str(item) for item in df.iloc[i].dropna().tolist()])
te = TransactionEncoder()
te_array = te.fit(transactions).transform(transactions)
df_encoded = pd.DataFrame(te_array, columns=te.columns_)So, for the Apriori Algorithm we require the data to be in the form:
Get Krupaashah’s stories in your inbox
Join Medium for free to get updates from this writer.
Transaction 1 → [Milk, beer, diaper]
Transaction 2 → [Beer, diaper]
So, we convert each row into a list of items and perform one-hot encoding.
Thus, we get a table like:
This is also called transaction encoding.
Step 5: Apply the apriori algorithm
frequent_itemsets = apriori(df_encoded, min_support=0.03, use_colnames=True)
frequent_itemsetsWe see the output as follows:
Here, the actual algorithm starts. This step finds frequent item transactions. It keeps only those transactions that appear in atleast 3% of all transactions. It calculates support, that is, how often they appear.
Thus, from the output we can see
Beer → Support = 0.36
Milk, Coke, Diapers → Support = 0.06
Step 6: Generate Association Rules
rules = association_rules(frequent_itemsets, metric="confidence", min_threshold=0.6)
rules[['antecedents', 'consequents', 'support', 'confidence', 'lift']]On executing we get,
This step creates: If a customer buys beer, then the customer buys bread.
It measures the antecedents and consequents by:
· Support: How often both items appear
· Confidence: Probability of buying bread if the customer buys beer
· Lift: Strength of the relationship.
So, let’s take the relationship chips → beer
Lift = 1.85. Thus, customers who buy chips are 1.85 times more likely to buy beer than a random chance.
Step 7: Filter the stronger association rules
rules.sort_values(by="lift", ascending=False).head(10)This step helps us to generate the rules that are most powerful and meaningful. We sort all the values in descending order of the value of lift. This gives us the top 10 most likely if-then relations.
Conclusion
The Apriori algorithm is one of the most widely used for association rule mining. It works by generating itemsets that appear frequently in the dataset and removing the ones that are insignificant. The datasets are removed if its support is less than the minimum support used. It is best used when the dataset is small. It is commonly applied in market basket analysis, educational demonstrations, and situations where computational resources are limited.








