Explore how the Apriori algorithm works, its key metrics, real-world applications, benefits, and limitations in market basket analysis.
An online stationery shop finds that 80% of those who buy notebooks also buy pens. The marketing team is considering putting together a notebook-and-pen package, which they believe will boost sales.
But, on closer inspection, 80% of all customers already buy pens. There is no difference in the probability that a notebook buyer will buy a pen, compared to any other buyer.
The following example will demonstrate the need to identify relationships between products. It’s crucial to know how strong those relationships are, too.
How Apriori Algorithm Works?
Apriori Algorithm: It is a technique in Data Mining used to find frequent sets of items in a given data set and make association rules from these.
The algorithm was introduced by Rakesh Agrawal and Ramakrishnan Srikant in 1994, and it is mainly used for Market Basket analysis.
An itemset is a set of one or more items. An association rule is a relation between various itemsets.
Notebooks → pens, for instance, implies that there is a certain frequency of pens involved in transactions that involve notebooks. Does not determine the order in which a product was bought.
What is the working of the Apriori Algorithm?
Suppose a stationery company is analysing 1000 customer transactions.
It has the following on its records:
| Item or combination | Transactions |
| Notebooks | 400 |
| Pens | 800 |
| Highlighters | 200 |
| Notebooks and pens | 320 |
| Notebooks and highlighters | 160 |
| Pens and highlighters | 180 |
| Notebooks, pens and highlighters | 150 |
The analyst decided the minimum support value as 0.2. Thus, we can say that an itemset is frequent if its occurrence is at least 200 transactions.
The algorithm first finds the individual items that meet this requirement. It then looks into pairs of common items.
Notebooks and pens are good, as they have 320 transactions together. Notebooks and highlighters do not, however, meet the threshold.
The algorithm is thus able to remove any larger combination of notebooks and highlighters without counting its occurrences.
This is called the Apriori Principle: If an itemset is infrequent, then any superset of it must be infrequent.
The algorithm eliminates unsuitable combinations early on, thus saving unnecessary calculations.
Knowing how to interpret support, confidence, and lift.
There are three key metrics that analysts use to analyse relationships found in transactional data.
1. Support
Support is defined as the number of times an item or set of items occurs in all the transactions.
Support: Number of transactions with the itemset / Total number of transactions.
Notebooks and pens:
320 / 1,000 = 32%
This indicates that 32% of all transactions include both items.
2. Confidence
Confidence is a measure that indicates the frequency of the consequent in the transactions that contain the antecedent.
Confidence = Transactions with both items / Transactions with antecedent item
For the rule notebooks → pens:
320 / 400 = 80%
80% of the customers who buy notebooks also buy pens.
While this may sound compelling, the only way to know if there is a meaningful relationship between notebook purchases and pen purchases is to look at the confidence.
3. Lift
Lift is an association rule’s confidence divided by the frequency of the consequent.
Confidence / Support of the consequent = Lift
The pens are involved in 80% of all transactions:
0.80 / 0.80 = 1
A lift of 1 means that the co-occurrence observed is as expected if the purchases are independent.
Items that have a lift greater than 1 tend to be positively associated with each other, while those that have a lift less than 1 tend to be negatively associated with each other, meaning that they co-occur less frequently than expected if they were independent.
What are the circumstances when no valuable patterns can be found in Apriori?
Let’s take a look at the notebooks and highlighters set from the same data.
It has support of 16%, which is less than the initial amount of 20%. This is because Apriori discards it.
But if the analyst sets the minimum support to 15%, he or she can view this combination.
The confidence of notebooks → highlighters is 40%, and the confidence of highlighters is 20% of all the transactions.
Hence, its lift is:
0.40 / 0.20 = 2
Although there is less confidence in this rule than in the notebook and pen rule, there is a greater positive association.
Thus it may be a mistake to select too great a value for support level, so that useful patterns are not revealed.
More than just retail.
Apriori can be useful in the education field for discovering which courses students tend to take together, in an online store to suggest products to customers and in software to show which features are commonly used together in software.
But it is important to clarify what is meant by the term “transaction. For an education site, for instance, it should be determined if courses taken in one session or in an entire semester are a transaction.
This is because altering this definition may affect the rules that are generated in an association.
The Apriori Algorithm has the following drawbacks:
This can involve multiple passes over a data set and produce many different candidate itemsets, especially if many different items appear in the transactions.
Overhead of candidate generation can be lowered using alternative techniques, like FP-Growth.
A second key caveat is that association does not imply causation. When two products are purchased in conjunction with each other, it does not necessarily mean that promoting one will drive sales of the other product.
Attractive partnerships should be tested in a testbed prior to significant business investments.
Finally, the significance of Apriori is not just in the fact that it can find the frequent combinations, but also in finding relationships that are worth investigating.
