This article was published as a part of the Data Science Blogathon
In this article, I’ll go over Market Basket Analysis and how to use it in R.
1. Market Basket Analysis
2. How is it used?
3. Association Rules
4. Applications
5. The Dataset
6. Math’s in Market Basket Analysis
7. Apriori Recommendation with R
8. Sorting
9. Redundancies
10. Targeting Items
11. Visualization
Market Basket Analysis is one of the key techniques used to uncover links between items by large retailers. It works by searching for combinations of items that often happen in transactions together. In a different way, retailers can identify relations among the items they buy.
It is a method of identifying object associations that “go together” in a commercial context. Market basket analysis, in actuality, goes beyond the supermarket scenario from which it gets its name. The investigation of any collection of commodities to uncover affinities that may be exploited in some way is known as market basket analysis. In big transactional or relational data sets, frequent itemset mining leads to the finding of relationships and correlations between items. With vast volumes of data being collected and stored on a regular basis, many companies are interested in extracting patterns from their databases. Many commercial decision-making processes, such as catalogue design, cross-marketing, and customer buying behaviour research, might benefit from the finding of interesting correlation patterns across massive amounts of business transaction records.
Association rules are widely used to analyse retail basket or transaction data, with the objective of establishing strong rules based on a strong transaction information rules concept.
Market Basket Analysis uses the information to:
Learn about your goods (products):
Action to take:
We would like to respond ideally to questions such as
What products are purchased jointly?
Which products could be promoted?
What are the best opportunities for cross-selling?
Most purchases are purchased at retail. The analysis of the market basket gives insights into what a customer could have purchased had the idea happened to them. As a first step, market basket analysis can therefore be used to determine where goods are to be placed and promote within a shop. If buyers of barbie dolls buy more sweets, high margin candy can be placed near the doll display of Barbie as observed.
Image 1
A list like this can be useful for:
The similarities between items are many ways to see. These techniques fall within the framework of the general association. Simply said, the end result of this method is a set of rules that can be interpreted as “if that is so.”
Image 2
From the above image:-
Assume there are 5 customers: 3 of them bought milk, 2 bought potato chips and 2 bought both of them.
Transactions | Frozen Pizza | Cola | Milk | Potato Chips | Pretzels |
Transaction 1 | 1 | 1 | 1 | 0 | 0 |
Transaction 2 | 0 | 0 | 1 | 1 | 0 |
Transaction 3 | 1 | 1 | 0 | 0 | 0 |
Transaction 4 | 0 | 1 | 0 | 0 | 1 |
Any Rule with a lift < 1 does not indicate a cross-selling opportunity.
Support(Milk) = P(Milk)
= 3/5 = 0.6
Support(Potato Chips) = P(Potato Chips)
= 2/5 = 0.4
Support = P(Milk & Potato Chips)
= 2/5 = 0.4
2. Confidence
Confidence = Support(Milk & Potato Chips)/ Support(Milk)
= 0.4/0.6 = 0.67
3. Lift
Lift = Confidence/ Support(Potato Chips)
= 0.67/0.40 = 1.67
So, what are we talking about, what sort of items? Many association applications exist:
This article focuses on the retail application — the data set comes with R and is simple, intuitive, and repeatable.
Image 3
Put on your table, imagine 10,000 receipts. Each receipt is a transaction of purchased items. The receipt represents stuff which was inserted into the basket of a customer — and thus ‘Market Basket Analysis.’
The foodstuffs dataset contains exactly this: a collection of receipts with a receipt of 1 receipt per line and items purchased. Every column in a column represents an item, each line is called a transaction.
The food dataset can be downloaded to view it.
Image 4
The concept of items and item sets has already been discussed. This reads: “When a user purchases a product in the item on the left side, the user probably purchases the product on the right. An instance that can be read more humanly is:
Three important proportions can be understood: support, confidence, and lift. I shall describe in the following bullet points the importance of these
Note: if the lift is 1. the items on the right and left are separate.
Image 5
Here is a dataset with six transactions. Each transaction is made up of 0s and 1s, with 0 representing the absence of an item and 1 representing it’s presence.
Transaction ID | Grapes | Apple | Mango | Orange |
1 | 1 | 1 | 1 | 1 |
2 | 1 | 0 | 1 | 1 |
3 | 0 | 0 | 1 | 1 |
4 | 0 | 1 | 0 | 0 |
5 | 1 | 1 | 1 | 1 |
6 | 1 | 1 | 0 | 1 |
We will use the following matrices to find interesting rules among the many possible rules in this small business scenario:
1. Support: An item’s default popularity. In mathematical terms, item A’s support is simply the ratio of transactions involving A to the total number of transactions.
Support(Grapes) = (Grape-Related Transactions)/ (Total transaction)
Support(Grapes) = 0.666 Support
2. Confidence: The likelihood that a consumer who purchased both
A and B will return. It divides the number of transactions in which both A and
B are involved by the number of transactions in which B is involved.
Confidence(A => B) = (Transactions involving both A and B)/(Transactions involving only A)
Confidence({Grapes, Apple} => {Mango})
= Support(Grapes, Apple, Mango)/Support(Grapes, Apple)
= 2/6 / 3/6
= 0.667
3. Lift : When you sell B, you increase the sale of A.
As a result, the likelihood of a customer purchasing both A and B together is ‘lift-value’ times greater than the chances of purchasing either separately.
Lift(A => B) = Confidence(A, B) / Support(B)
Lift ({Grapes, Apple} => {Mango}) = 1
So, let’s get started by loading up our libraries and data set
# Load the libraries library(arules) library(arulesViz) library(datasets) # Load the data set data(Groceries)
Lets explore the data before we make any rules:
# Create an item frequency plot for the top 20 items itemFrequencyPlot(Groceries,topN=20,type="absolute")
Now some rules are ready for mine! The minimum required support and trust will always have to be given.
The minimum amount of support is 0.001
We have established 0.8 minimum trust
The top five rules are shown.
# Get the rules rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8))
# Show the top 5 rules, but only 2 digits
options(digits=2)
inspect(rules[1:5])
The output we see should look something like this:
For example, this reads easily: 81 per cent of the buyer is likely to buy whole milk if someone purchases yoghurt and cereals. We can get summary information regarding the rules that provide us with some interesting information, like:
· The number of rules produced: 410
· Length allocation of the rules: The most common rules are 4 long items
· Quality measures summary: The rates of support, lift and trust are interesting to see.
· Total data and minimum parameters are extracted from the data mines.
The first problem is that the rules are not sorted. We often want first the most important rules. We wanted the most likely rules, let’s say. By executing the following code, we can easily sort by trust.
rules<-sort(rules, by="confidence", decreasing=TRUE)
Our top five outputs are now sorted by trust and thus the most relevant rules appear.
Rule 4 might be too long. You want more precise rules, let’s say. This can also be done easily by adding a ‘maxlen’ parameter to your apriori function:
rules <- apriori(Groceries, parameter = list(supp = 0.001, conf = 0.8,maxlen=3))
Rules will be repeated sometimes. Redundancy means one item could be specified. You have the option of removing the item from the dataset. Otherwise, redundant rules generated can be removed. These repeated rules can be deleted using the following code snippet:
subset.matrix <- is.subset(rules, rules) subset.matrix[lower.tri(subset.matrix, diag=T)] <- NA redundant = 1 rules.pruned <- rules[!redundant] rules<-rules.pruned
Now that we know how to generate rules, limit the output, we want to go for rules. We could be interested in two types of targets, illustrated in an example of “all milk”:
What are customers who can buy whole milk before buying?
1) If customers buy whole milk, what are they likely to buy?
2) In essence, we want the left side and the right side of the hand. It’s not hard to do with R!
Response: We modify our apriori() function as follows on the first question:
rules<-apriori(data=Groceries, parameter=list(supp=0.001,conf = 0.08), appearance = list(default="lhs",rhs="whole milk"), control = list(verbose=F)) rules<-sort(rules, decreasing=TRUE,by="confidence") inspect(rules[1:5])
The output will look like this:
We can also make the left side “full milk” and find its background. Please note that:
· We have set the trust to 0.15 because we do not have 0.8 rules.
· To avoid leaky left-hand items, we set a minimum length of 2
rules<-apriori(data=Groceries, parameter=list(supp=0.001,conf = 0.15,minlen=2), appearance = list(default="rhs",lhs="whole milk"), control = list(verbose=F)) rules<-sort(rules, decreasing=TRUE,by="confidence") inspect(rules[1:5])
Now our output looks like this:
Visualization is the final step. Let’s say that you wanted a graph to map the rules. This can be done with another “arulesViz” library.
plot(rules, method="graph")
Here you can browse for
the code!
Thank you for following with me all the way to the end. By the end of this article, we should have a good understanding of Market Basket Analysis and its implementation in R.
I hope you found this article useful. Please feel free to distribute it to your peers.
Hello, I’m Gunjan Agarwal from Gurugram, and I earned a Master’s Degree in Data Science from Amity University in Gurgaon. I enthusiastically participate in Data Science hackathons, blogathons, and workshops.
I’d like to connect with you on Linkedin. Mail me here for any queries.
Image Source: