In this article, I will explain **What is the Apriori Algorithm With Example?**. I tried to explain the Apriori Algorithm in the **easiest way** so that you can understand the whole concepts of the apriori algorithm easily.

So, without further ado, let’s get started-

**What is Apriori Algorithm With Example?**

Apriori algorithm is the algorithm that is used to find out the **association rules** between objects. That means how two objects are associated and related to each other. In simple words, the **apriori algorithm is an association rule learning** that analyzes that** “People who bought item X also bought item Y.**

The objective of the apriori algorithm is to generate the association rule between objects. And the association rule tells us how two or three objects are correlated to each other. Apriori Algorithm is also known as** frequent pattern mining.**

**Where Apriori Algorithm** **is Used?**

The most common and popular example of the apriori algorithm is Recommendation System. Let’s understand with the help of the **Movie Recommendation** example.

Suppose this is the data of users who like some movies-

User Id | Movies Liked |

111 | Movie 1, Movie 2, Movie 3, Movie 4 |

112 | Movie 1, Movie 2 |

113 | Movie 1, Movie 2, Movie 4 |

114 | Movie 1, Movie 3 |

115 | Movie 2, Movie 4 |

*Table 1*

So from this data, we can generate some association rules that the person who likes Movie 1 also likes Movie 2, and people who like Movie 2 are quite likely to also like Movie 4, and so on.

We can generate many rules with the help of this data, some rules are weak and some rules are strong. So for business decisions, only strong rules are used.

And here the question comes in your mind- **How to filter strong rules from the weaker ones?**

Right?

So, let’s understand the whole working of the Apriori Algorithm in the next section with the help of an example-

**How Apriori Algorithm Works?**

Before I discuss the working of the apriori algorithm, you should remember two main concepts of the Apriori algorithm and that is-

**Support****Confidence**

The whole working of the apriori algorithm is based on these terms. So, let’s see What is Support, and confidence, in the Apriori Algorithm with respect to movie recommendation example-

**Support= People who watch Movie 1/ Total no. of users**

**Confidence (M1-> M2)**= **People who watch Movie 1 & Movie 2/ People who watch Movie 1**

Now, you knew about the terms that are used in the Apriori Algorithm. So let’s understand how the apriori algorithm works with the help of an example-

Suppose this is our dataset of any supermarket, where user id and items are listed-

User Id | Items |

001 | 1 3 4 |

002 | 2 3 5 |

003 | 1 2 3 5 |

004 | 2 5 |

*Table 2*

In this data, the **user 001** purchased items **1,3, and 4**. The **user 002 **purchased items** 2,3,** **and 5, **and so on. So here we have to find the **shopping pattern** between these items **1,2,3,4, and 5.**

**Step 1-**

So, the first step in the apriori algorithm is to **set minimum support and confidence**. This will act as a threshold value. By setting **minimum support and confidence**, you can avoid items that have less support than the threshold value. And here you got an answer to the question- **How to filter out strong rules from the weak rules?**– by setting minimum support and confidence, you can filter out strong rules from the weak rules.

Suppose I set minimum **support as 50%** and **confidence as 70%.**

**Step 2-**

Now the next step is to **calculate the support** of each item** 1,2,3,4, and 5**. So I calculated the support of each item in the following table-

Items | Support |

1 | 2/4= 50% |

2 | 3/4= 75% |

3 | 3/4=75% |

4 | 1/4=25% |

5 | 3/4=75% |

*Table 3*

Confused…? How I calculated the support?

Don’t Worry!…I’ll explain…So, let’s see how I calculated the support for Item 1-

According to the formula of **support**– **People who buy Item 1/ Total no. of users**.

So,** Item 1** is purchased by **2 people(001 & 003)**-> **refer Table 2**

That’s why it’s **2** and the **total no of users** is 4, so the support is 2/4=**50%**. I hope now you understood, similarly you can calculate the support of all other items.

So after calculating the support of all items, we need to check which item has less support than the minimum support threshold. In our example, Item 4 has 25% support that is less than our minimum support.

That’s why I remove item 4 for further steps. Now we have items **1,2,3 and 5.**

**Step 3-**

After calculating the support of each individual item, now we calculate the support of a pair of items. And for that, we need to form pairs.

Items | Support |

{1,2} | 1/4=25% |

{1,3} | 2/4=50% |

{1,5} | 1/4=25% |

{2,3} | 2/4=50% |

{2,5} | 3/4=75% |

{3,5} | 2/4=50% |

*Table 4*

I hope you understood how I formed the pairs. But still, I would like to explain- In step 2, we had 4 items left **[1,2,3,5]**. So I simply multiplied **1** with all items like** {1,2}, {1,3}, {1,5}**. Then I multiplied 2 with 3 and 5, so I got **{2,3}, and {2,5}**. And then 3 is multiplied by 5, so I got **{3,5}**.

And then for calculating the support of each pair, you need to refer again to** table 2**. For example, for **pair {1,2}**, you need to check** table 2**, how many people bought items 1 & 2 together.

So, according to **table 2**, only **one person** bought item** 1 & 2 together**, that’s why the **nominator is 1**. And the** total no of people is 4**, so the **denominator is 4**. So** 1/4=25%**. Similarly, I calculated the support of all pairs.

Now it’s time to filter out the pairs who have less support than the minimum support. So pair **{1,2} and {1,5}** have **25% support**. So I eliminate these two pairs for further steps.

**Step 4-**

Now we have following pairs-**{1,3},{2,3}, {2,5}, and {3,5}**. And in these pairs, we have item 1,2,3,5. Now it’s time to form** triplets** with these **four(1,2,3,5) items.**

Items | Support |

{1,3,5} | 1/4=25% |

{2,3,5} | 2/4=50% |

{1,2,3} | 1/4=25% |

{1,2,5} | 1/4=25% |

*Table 5*

In this table, I created all possible triplets in the same way as I formed pairs in the previous step. And similarly, I calculated support for all triplets in the same way as I did in the last step. So I think you understood how to form a triplet and calculate support.

Now let’s eliminate the triplets who have support less than the minimum support. And in this case, **{1,3,5},{1,2,5}, and {1,2,5}** are eliminated. We have only one triplet **{2,3,5}** who satisfies the minimum support.

**Step 5-**

Now we need to **form an association rule **with this** triplet-{2,3,5}**. As we have only three items, so we can generate rules something like that-

Rules | Support | Confidence |

(2^3)->5 | 2 | 2/2=100% |

(3^5)->2 | 2 | 2/2=100% |

(2^5)->3 | 2 | 2/3=66% |

2->(3^5) | 2 | 2/3=66% |

5->(2^3) | 2 | 2/3=66% |

3->(2^5) | 2 | 2/3=66% |

*Table 6*

In this table, I created rules with three items {2,3,5}. I hope you understood how I created the rules, simply by replacing 2, 3, and 5.

But you might be confused with **Support as 2**. Right…? So I put support as 2 in all the rules because these rules are generated by the **triplet {2,3,5}** and this triplet occurs **2** times in **Table 2**. So the** support count of {2,3,5} is 2.** That’s why I put support as 2. I hope now you understood.

Now, the next is-

**How I calculated the confidence of all rules?**

##### So, let’s understand-

The formula of** confidence** is= **S(AUB)/S(A)**

I will explain the calculation for the rule **(2^3)->5**. Similarly, you can calculate the confidence for all other rules.

So consider** (2^3)** **as** **A** and** 5 as B.**

According to the formula=**S(AUB)/S(A)**

=S((2^3)U5)/S(2^3)

= 2/2

=**100%**

Here the support of **S(2^3)U5) is 2** because all three items come from **triplet {2,3,5}** whose s**upport count is 2**. And the **support of S(2^3) is 2 **because the **pair {2,3} has support count 2**->refer **Table 4.**

I hope now you understood. Similarly, you can calculate the confidence of other rules. After calculating the confidence of all rules, compare with the threshold value of Confidence. In the beginning, I set the threshold value for **confidence as 70%.**

So the rules who have less than 70% confidence are eliminated. After eliminating the rules, we have only two rules left that satisfy the threshold value and these rules are-

**(2^3)->5****(3^5)->2**

So, these are the two final and **strong association rules** that are generated by using the **Apriori Algorithm.** I hope now you understood how the apriori algorithm works.

So, that’s all about Apriori Algorithm. Now it’s time to wrap up!

**Conclusion**

I tried to write this article in an easy way so that you understand the Apriori Algorithm easily. I hope you understood the whole concept of the **Apriori Algorithm**. But still, if you have some doubt, feel free to ask me in the comment section.

All the Best!

Happy Learning!

**Learn the Basics of Machine Learning Here**

**Related Search**

**9 Best Tensorflow Courses & Certifications Online- Discover the Best One!****Machine Learning Engineer Career Path: Step by Step Complete GuideBest Online Courses On Machine Learning You Must Know in 2024What is Machine Learning? Clear your all doubts easily.K Fold Cross-Validation in Machine Learning? How does K Fold Work?What is Principal Component Analysis in ML? Complete Guide!Linear Discriminant Analysis Python: Complete and Easy GuideTypes of Machine Learning, You Should Know Multi-Armed Bandit Problem- Quick and Super Easy Explanation!Upper Confidence Bound Reinforcement Learning- Super Easy GuideTop 5 Robust Machine Learning AlgorithmsSupport Vector Machine(SVM)Decision Tree ClassificationRandom Forest ClassificationK-Means ClusteringHierarchical ClusteringML vs AI vs Data Science vs Deep LearningIncrease Your Earnings by Top 4 ML JobsHow do I learn Machine Learning?Multiple Linear Regression: Everything You Need to Know About**

Thank YOU!

Though of the Day…

– Henry Ford

‘Anyone who stops learning is old, whether at twenty or eighty. Anyone who keeps learning stays young.

### Written By Aqsa Zafar

Founder of MLTUT, Machine Learning Ph.D. scholar at Dayananda Sagar University. Research on social media depression detection. Create tutorials on ML and data science for diverse applications. Passionate about sharing knowledge through website and social media.

Enrique Peña Nietothanks to whoever wrote this, not just because of the information, but for the nice way it was explained

aqsazafarGlad that you found this article helpful.