Using Apriori for Basket Analysis

Michael Le
3 min readDec 12, 2020

--

In this article, I will be walking you through the Apriori algorithm in order to do a Market Basket Analysis. This technique is commonly used by retailers and digital sellers to best recommend and display items that are most likely to be included in a basket if not already. The logic can be simplified as an if-else-then logic statement typically, but there are some other calculations made by the Apriori algorithm that we will cover in this article to better understand the recommendations.

Apriori — Understanding Association Rules

Let’s first understand what association rules are. Association rules are formed from a vector of transactions or otherwise known as an item set. If we define an item set as IS, then an IS is equivalent to:

IS = {Item 1, Item 2, Item 3, Item 4}

A transactions help make up item sets

The association rule is then defined as

X -> Y, where X is an component of IS, and Y is a component of IS

Metrics for Understanding

Support:

This is equivalent to seeing how popular an item set is based on the proportion of which an itemset appears. For example, let’s examine this transaction set:

Support formula is COUNT(X)/COUNT(TRANSACTIONS) where X is the itemset being evaluated, whether it’s one item or multiple.

Transaction 1 Beer, Chips, Pretzels
Transaction 2 Beer, Milk, Diapers
Transaction 3 Beer, Liquor, Diapers
Transaction 4 Diapers, Milk, Cereal
Transaction 5 Milk, Beer, Wine

The Support for beer, is 4/5 since or 80% since 80% of transactions have beer.

Can you calculate what the confidence of Milk is?

If you guessed 2/5 or 40% then you are correct!

Confidence

Confidence is understanding how often an itemset combination shows up based on a determinant itemset or item. This may sound very confusing so let me explain with an example:

Let’s use the above transaction set once more. If we were to say, what is the confidence level of someone buying beer means they also buy diapers (sad parents out there?) then we would take the support of beer and diapers, divided by the support level for beer.

Using the above example we have the support level of beer and diapers as 2/5 or 40%, and the support level of beer as 80%, so therefore our confidence level in beer buyers buying diapers is 50% (40%/80%).

Lift

Lift is the equivalent to saying how likely item Y is purchased when item X is purchased or a metric to help determine power of association. If we look at the lift for beer with diapers as in how likely would someone buy diapers given that they’ve bought beer, then the calculation would fit into this formula:

Lift (X -> Y) = Support(X,Y)/Support(X)*Support(Y)

Lift (Beer -> Diapers) = Support(Beer, Diapers) / Support(Beer) * Support(Diapers)

Support(Beer, Diapers) = 2/5

Support(Beer) = 4/5

Support(Diapers) = 2/5

We then have 2/5 / (4/5 * 3/5) = 2/5 / (12/5) = 10/60 ~ 16.7%

How do we translate this? A lift value greater than 1 translates to the item very likely being purchased from left hand side (X). A value less than 1 means Y is more unlikely to bought if X is bought.

Example Support Discovery with Grocery Dataset in Python

Libraries used:

Importing and viewing dataset:

Checking most popular items (exploratory analysis):

Determining items with minimum support of 1%:

Increasing support to 5%:

--

--

Michael Le

Innovation focused product leader within the AI/ML and Data problem space.