How to conduct market basket analysis | by Sambit Das | Towards Data Science

Market basket analysis is one of the key techniques that is used by large retailers to uncover hidden associations between items. Market basket analysis uses transaction data i.e. the list of all items bought by a customer in a single purchase to determine what products are ordered or purchased together and identify patterns of co-occurrence. This powerful analysis helps to reveal consumer preferences which will be very challenging to capture through an online survey. Retailers use the results of the market basket analysis to guide product placement in stores, cross-category and co-marketing promotions among others.
My last shopping experience was a fairly unpredictable one. I ended up in an Asian store to pick up a few items. Here is a look at my market basket.
This list will go into one row of the store’s database along with thousands of shopping trips or market baskets from other customers. Association rule mining is one of the most popular methods of extracting useful insights from the transaction data stored in the database. An item set is a collection of items selected from all items for sale in a grocery store. An item set can consist of 2 , 3 items and so on. The association rule determines the relationships between the items in the item set. These relationships are then used to build profiles containing IF-Then rules of the items purchased.
The rules could be written as :
If {A} Then {B}
The IF part of the rule is known as antecedent and the THEN part of the rule is known as consequent. Hence the antecedent is the condition and the consequent is the result. The association rule has three key measures that together determine the strength of the rule : Support , Confidence and Lift.
Lets assume that there are 100 customers. 10 of them brought milk , 8 of them bought butter and 6 bought both of them. If we were to evaluate the association rule “If someone buys milk , they will also buy butter “, then the Support of the association rule can be calculated as :
(Probability of Milk and Butter) or in other words (No of baskets containing both milk and butter) / (No of total baskets)
Support = (Probability of Milk and Butter) = 6/100 = 0.06
A support criterion of 0.06 means six in every hundred market baskets have both milk and butter items.
Confidence = Support of the item set divided by the support of sebset of items in the antecedent. In other words it is the Conditional probability P(Butter | Milk) calculated as :
Confidence of P(B|A) = P(AB) / P(A) => (Probability of Milk and Butter) / (Probability of Milk)ORConfidence = Support / P (Milk)ORConfidence = 0.06 / (10/100) = 0.06/0.1 = 0.6
Finally lift is a measure of relative predictive confidence. It is the confidence with which we can predict that a customer will buy Butter given he has bought milk in the association rule “IF Milk THEN Butter”.
Lift = P(B|A) / P(B)orLift = Confidence / P(Butter) = 0.6 / (8/100) = 0.6/0.08 = 7.5
Lift values greater than 1 reflect stronger associations.
For this analysis I draw upon a grocery dataset analysed by Hahsler , Hornik and Reutterer (2006). There are a total of 9835 transactions containing 169 unique grocery items.
grocery_data = data("Groceries")#Show dimensions of the datasetprint(dim(Groceries))print(dim(Groceries)[1]) # 9835 market baskets for shopping tripsprint(dim(Groceries)[2]) # 169 initial store itemssummary(Groceries)
A closer look at the dataset reveals each row having a subset of items.
# Let's take a look at the first 5 transactionsinspect(Groceries[1:5])    items                                                                [1] {citrus fruit,semi-finished bread,margarine,ready soups}             [2] {tropical fruit,yogurt,coffee}                                       [3] {whole milk}                                                         [4] {pip fruit,yogurt,cream cheese ,meat spreads}                        [5] {other vegetables,whole milk,condensed milk,long life bakery product}
Let’s take a look at a frequency plot to determine the frequency of items appearing in the market baskets. We set the the support to 0. to ensure each item in the plot appears in every 10 market baskets at a minimum
itemFrequencyPlot(Groceries, support = 0.025, cex.names=0.8, xlim = c(0,0.3),type = "relative", horiz = TRUE, col = "dark red", las = 1,xlab = paste("Proportion of Market Baskets Containing Item","\n(Item Relative Frequency or Support)"))
Let’s take a look at the top 20 frequently appearing items in the shopping baskets.
# Plot the frequency of top 20 itemsitemFrequencyPlot(Groceries,topN = 20)
So whole milk , vegetables , rolls , soda and yogurt are the top 5 most purchased items in the store.
The Apriori algorithm implemented in the arules package in R helps to mine the association rules. A set of 344 rules are obtained by setting the thresholds for support and confidence of 0.025 and 0.05.
rules <- apriori(groceries, parameter = list(support = 0.025 , confidence = 0.05))AprioriParameter specification: confidence minval smax arem  aval originalSupport maxtime support minlen maxlen target  ext       0.05    0.1    1 none FALSE            TRUE       5   0.025      1     10  rules TRUEAlgorithmic control: filter tree heap memopt load sort verbose    0.1 TRUE TRUE  FALSE TRUE    2    TRUEAbsolute minimum support count: 245set item appearances ...[0 item(s)] done [0.00s].set transactions ...[55 item(s), 9835 transaction(s)] done [0.03s].sorting and recoding items ... [32 item(s)] done [0.00s].creating transaction tree ... done [0.01s].checking subsets of size 1 2 3 4 done [0.00s].writing ... [344 rule(s)] done [0.00s].creating S4 object  ... done [0.00s].> rulesset of 344 rules
Let’s investigate the top 10 association rules sorted by the lift value.
inspect(sort(rules,by="lift")[1:10])
A review of all the 344 association rules manually will be extremely tiresome and is not a viable option. Using the R package arulesViz , we will implement visualization techniques to explore the relationships.
It is possible to view a scattered plot with support in the horizontal axis and lift in the vertical axis. Colour coding of the points relates to confidence.
plot(rules,measure = c("support","lift"),shading="confidence")
Unwin , Hofmann and Bernt (2001) introduced a special version of a scatter plot called Two-key plot. Here support and confidence represent the X and Y axis respectively and the colour points are used to indicate the “order” i.e the number of items contained in each rule.
plot(rules,shading="order",control=list(main="Two-Key plot"))
From the above plot , it is clear that the order and the support are inversely proportional to each other.
The below plot provides a clearer view of the identified association rules in the form of a matrix bubbled chart. An item in the antecedent (left hand side) of the association rule is represented in the labels at the top of the matrix while the item in the consequent (right hand side) of an association rule is represented at the right of the matrix. Support is represented by the size of each bubble and lift is reflected in the colour intensity.
plot(rules,method="grouped",control=list(col=rev(brewer.pal(9,"Greens")[4:9])))
Now if i were to identify what are the products that are commonly bought with vegetables , I can filter and report the data from the overall association rules. Below I rank these rules by lift and also identify the top 10 association rules.
vegie.rules <- subset(rules,subset=rhs %pin% "vegetables")inspect(sort(vegie.rules,by="lift")[1:10])
Next I represent the association rules for commonly bought products with vegetables below in a network diagram.
plot(top.vegie.rules,method = "graph",control = list(type="items"),shading = "lift" )
From the network diagram, it is evident that a customer is likely to buy beef, dairy, produce, bread, sausage when buying vegetables.
As a relative frequency or probability estimate, the support value lies between 0 to 1. Lower values of support are ok as long as it is not very low. Confidence also takes the value between 0 to 1 as it is a conditional probability measure. Higher values of confidence is generally preferred. Finally, the lift value needs to be greater than 1.0 to be taken seriously by the management.
The analysis we have done above is descriptive in nature as the shopping data was analysed to study the shopping behaviour. Taking this analysis to the next step is to implement the insights from this study at the ground and on the stores.
Retailers frequently use the findings of association rules to make decisions about store layout , product bundling or cross-selling. Store managers are also running ground experiments such as A/B tests to study how shoppers respond to new store layouts. This is an ongoing and fascinating research to truly test the performance of market basket predictive modelling.
So the next time you walk into your local supermarket store and find the arrangements have been changed, you now know why.
I would like to especially thank Professor Miller for his guidance and inspiration. Having studied in his classes during my Masters in Data science course with Northwestern, his books and his classes have had a profound influence on my data science career. My work draws upon the following literature :
Thomas W. Miller (2014) Modeling Techniques in Predictive Analytics
M. Hahsler, S. Chelluboina (2015) Visualizing Association rules : Introduction to the R-extension arulesViz
Rstat Building a Market Basket Model