Apriori Algorithm in ML

Learn via video courses
Topics Covered

The Apriori algorithm, introduced by R. Agrawal and R. Srikant in 1994, targets frequent itemset identification in datasets for association rule mining. Named 'Apriori' due to its foundational knowledge of itemset properties, it employs an iterative, level-wise strategy. This approach leverages k-frequent itemsets to deduce k+1 itemsets, optimizing efficiency via the Apriori property, which curtails the search space.

Operating on transactional databases, the algorithm extracts association rules, quantifying relationships between items. Using breadth-first search and Hash Tree techniques, it efficiently computes itemset associations.

Predominantly utilized for market basket insights, Apriori identifies product combinations, but its applicability extends to sectors like healthcare, pinpointing potential drug interactions for patients.

Important Points Needed for Implementing Apriori Algorithm

  • Confidence
    • Measures how often items in Y appear in transactions that contain X.
    • Confidence(b | a) = (number of transactions containing a and b)/(number of transactions containing a)
  • Support(a)
    • (Number of transactions in which a appears)/(total number of transactions)
  • Frequent item set
    • Itemset having items whose support is greater than the threshold values or user-specified minimum support.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

Steps for Apriori Algorithm

1. Select minimum support and confidence from our transactional database.

2. Select all transaction supports that have a greater confidence value than the threshold or minimum confidence value.

3. Identify any rules in these subsets that have a greater confidence value than the threshold or minimal confidence value.

4. Arrange the rules in order of increasing lift.

Working on Apriori Algorithm

Let's understand this with the help of an example:

We are given the following data set, and Using the Apriori method, we must locate the frequently occurring itemsets and construct association rules:

Transaction IDItemSet
T1a, b
T2a, b, c
T3a, b, c, e
T4b, c, d
T5a, d

Minimum Support = 2 and minimum confidence = 60%

Sharpen Your Fundamentals with Free Learning

Solution

1. Create a table that contains the support count(frequency of each item set) of itemsets individually.

Item SetSupport Count
a4
b4
c3
d2
e1

After removing an item set with a support count less than minimum support, we get

Item SetSupport Count
a4
b4
c3
d2

2. Create a table that contains the support count of itemsets present in the final table of step 1 in pairs

Item SetSupport Count
a, b3
a, c2
a, d1
b, c3
b, d1
c, d1

After removing an item set with a support count less than minimum support, we get

Item SetSupport Count
a, b3
a, c2
b, c3

3. Create a table that contains the support count of itemsets present in the final table of step 1 in triplets.

Item SetSupport Count
a, b, c2
b, c, d1

After removing an item set with a support count less than minimum support, we get

Item SetSupport Count
a, b, c2

4. Find the association rules for the subsets Create a new table with all possible rules from the occurred combination {a, b, c}.

RulesSupportConfidence
{a, b} -> c22/4 = 50%
{b, c} -> a22/3 =66.67%
{a, c} -> b22/2 =100%
a -> {b, c}22/4 =50%
b -> {a, c}22/4 =50%
c -> {a, b}22/3=66.67%

After removing rules with confidence less than minimum confidence, we get

RulesSupportConfidence
{b, c} -> a22/3 =66.67%
{a, c} -> b22/2 =100%
c -> {a, b}22/3=66.67%

Now we can consider {b, c} -> a, {a, c} -> b, c -> {a, b} as strong association rules for the given problem.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Advantages of the Apriori Algorithm

1. This algorithm is simple to comprehend.

2. On big datasets, the algorithm's join and prune steps are simple to implement.

Disadvantages of Apriori Algorithm

1. In comparison to other algorithms, the apriori algorithm is slow.

2. Because it checks the database many times, overall performance may suffer.

3. The apriori algorithm has a time and space complexity of O(2D), which is extremely high. The horizontal width of the database is represented by D.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Python Implementation of Apriori Algorithm

1. transactions is a list of transactions.

2. min_support= To specify the minimum float value for support. We've used 0.005 in this case.

3. min_confidence= Set the minimum confidence value using min confidence. We've taken 0.2 in this case. It can be altered to suit the needs of the company.

4. min_lift= To determine the lift's minimal value.

5. min_length= For the alliance, the bare minimum of products is required.

6. max_length= It takes the maximum number of products for the association.

Code:

Application of Apriori Algorithm

1. Extracting association rules in data mining of admitted students based on features and specialties in the field of education.

2. In the Medical field: For example, Analysis of the patient’s database.

3. In Forestry: Analysis of probability and intensity of forest fire with the forest fire data.

4. Many firms employ Apriori, including Amazon's Recommender System and Google's auto-complete feature.

Conclusion

  • The apriori algorithm is a fast database scanning algorithm that only scans the database once.
  • It significantly reduces the size of the itemsets in the database while maintaining decent performance. As a result, data mining aids consumers and businesses in making better decisions.