Machine Learning Tutorial

Apriori Algorithm in Machine Learning

Apriori Algorithm in Machine Learning
Apriori Algorithm in Machine Learning

Apriori Algorithm in Machine Learning

When you walk into a supermarket and see an attractive combo offer like Buy Bread and Get Butter at 50% Off, there’s a good chance that such a strategy comes from analysing customer purchase patterns using the Apriori Algorithm. It is a fundamental algorithm used in association rule mining to discover relationships between items in transaction data.

The Apriori algorithm helps answer questions such as: Which items are frequently purchased together? and What purchasing patterns can be found across thousands of transactions?

Apriori Algorithm in Machine Learning

Complete Advance AI Topics: Click Here
SQL Tutorial:
Click Here

What is the Apriori Algorithm?

The Apriori algorithm was introduced by Rakesh Agrawal and Ramakrishnan Srikant in 1994. It is used to identify frequently occurring itemsets and generate association rules from transactional datasets.

The algorithm is particularly useful for working with large datasets where identifying relationships between items is important.

Some common applications include:

  • Finding products that are frequently purchased together, known as market basket analysis.
  • Healthcare: Finding relationships between medications and therapies.

What is a Frequent Itemset?

A frequent itemset is a group of items that appear together in a transactional database more often than a user-defined minimum support threshold.

Example:

Suppose we have:

  • Transaction A = {1,2,3,4,5}
  • Transaction B = {2,3,7}

The items {2,3} appear in both transactions. Therefore, they can be considered a frequent itemset if their support meets the required threshold.

Note: Understanding support, confidence, and lift is important for working with Apriori and association rules.

Steps of the Apriori Algorithm

  1. Calculate support: Determine the support of itemsets and remove those below the minimum support threshold.
  2. Generate candidate itemsets: Create larger candidate itemsets from frequently occurring itemsets.
  3. Calculate confidence: Evaluate the confidence of the generated association rules and remove weak rules.
  4. Sort by lift: Sort the rules according to their lift to identify the most useful associations.

How Apriori Works: A Step-by-Step Example

Let’s understand the Apriori algorithm with a simple hypothetical example.

Step 1: Generate C1 and L1

  • C1: Count the frequency or support of individual items.
  • L1: Keep only those items whose support meets the minimum support requirement.

Items such as E with low support are removed.

Step 2: Generate C2 and L2

  • Create pairs from the frequently occurring items in L1.
  • Count how often each pair occurs in the transactions.
  • Keep the pairs that satisfy the minimum support requirement in L2.

Step 3: Generate C3 and L3

  • Combine frequent items to create triplets.
  • Find triplets such as {A, B, C} that meet the support threshold.

Step 4: Generate Association Rules

From an itemset such as {A, B, C}, different association rules can be generated and their confidence can be calculated.

  • A, B → C
  • B, C → A
  • A, C → B

Example Output:

RuleConfidence
A, B → C50%
B, C → A50%
A, C → B50%

These rules can be considered strong association rules based on the selected criteria.

Advantages of the Apriori Algorithm

  • Simple to understand and implement.
  • Effective on large datasets because of its pruning strategies.
  • Can be integrated with business intelligence tools.

Disadvantages of the Apriori Algorithm

  • Computationally expensive: It requires multiple scans of the database.
  • The number of possible itemsets can grow exponentially, resulting in high time and space requirements.
  • Performance can decrease as the size of the dataset increases.

Python Implementation of Apriori

Let’s use the apyori library to implement the Apriori algorithm in Python.

Step 1: Install and Import Libraries

pip install apyori
import numpy as np
import pandas as pd
from apyori import apriori

Step 2: Load and Preprocess the Data

dataset = pd.read_csv('Market_Basket_data1.csv', header=None)
transactions = []

for i in range(0, 7501):
    transactions.append([str(dataset.values[i, j]) for j in range(0, 20)])

Here, each row represents a transaction. The data is converted into a list of lists so that it can be used with the Apriori algorithm.

Step 3: Apply the Apriori Algorithm

rules = apriori(
    transactions=transactions,
    min_support=0.003,
    min_confidence=0.2,
    min_lift=3,
    min_length=2,
    max_length=2
)

results = list(rules)
  • min_support: 0.003, which represents approximately 3 transactions out of 7501.
  • min_confidence: 20%.
  • min_lift: 3, which helps filter for more interesting associations.

Step 4: Display the Results

for item in results:
    print(f"Rule: {item.items}")
    for stat in item.ordered_statistics:
        print(
            f"   {set(stat.items_base)} -> {set(stat.items_add)} | "
            f"Confidence: {stat.confidence:.2f} | "
            f"Lift: {stat.lift:.2f}"
        )

Sample Output:

Rule: frozenset({'chicken', 'light cream'})
   {'light cream'} -> {'chicken'} | Confidence: 0.29 | Lift: 4.84

Rule: frozenset({'escalope', 'pasta'})
   {'pasta'} -> {'escalope'} | Confidence: 0.37 | Lift: 4.70

These insights can help retailers create smarter promotions, such as “Buy Pasta, Get Escalope Discount!”

Download New Real Time Projects :- Click here

Final Thoughts

The Apriori Algorithm is one of the most intuitive and practical tools used in data mining and association rule learning. From retail to healthcare, it can help organizations discover hidden patterns and use them to make better decisions.

Although Apriori may not be the fastest algorithm for every dataset, its simplicity and reliability make it a useful starting point for understanding association rule mining.

If you’re exploring data mining or e-commerce analytics, the Apriori algorithm is a useful tool for discovering hidden patterns in transactional data.


SEO Keywords: apriori algorithm in data mining, apriori algorithm example, apriori algorithm in python,

Source Code Available

Interested in This Project?

Get the complete source code for this project at a very affordable price — perfect for your portfolio, college submission, or learning. Message us on WhatsApp and we'll get back to you instantly!

Full source code included Step-by-step setup guide Instant delivery on WhatsApp Instant reply on WhatsApp
Chat on WhatsApp

We usually reply within a few minutes

Leave a Reply

Your email address will not be published. Required fields are marked *

Chat with us