In the vibrant cityscape of London, a city renowned for its cultural diversity and culinary richness, Indian cuisine stands out as a favorite among locals and tourists alike. The intriguing blend of spices, the variety of flavors, and the traditional cooking techniques make Indian restaurants a staple in London’s gastronomic scene. To thrive in a competitive market, restaurant managers and chefs often seek to understand customer preferences and dining patterns. This is where data-driven decision-making, leveraging techniques such as the Apriori method, becomes invaluable.
The key aim of the project is the applying the Apriori method to transactional data from an Indian takeaway restaurant in London to reveal frequent itemsets and strong association rules among the dishes ordered. The method will help to get insights into customer behavior, enabling the restaurant to tailor its menu, manage inventory efficiently, and design targeted marketing campaigns.
This project aspires to demonstrate the utility of the Apriori method in the culinary business domain, providing a roadmap for similar establishments.
Dataset used in the project consists of 13 397 transactions from the Indian takeaway restaurant located in London1.
Analyzing the transactional data:
summary(data_trans)
## transactions as itemMatrix in sparse format with
## 13397 rows (elements/itemsets/transactions) and
## 248 columns (items) and a density of 0.02247554
##
## most frequent items:
## Pilau Rice Plain Naan Plain Papadum Garlic Naan Onion Bhajee
## 4721 3752 3596 2628 2401
## (Other)
## 57576
##
## element (itemset/transaction) length distribution:
## sizes
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
## 338 605 1611 2560 2421 1984 1377 935 617 331 227 128 101 63 39 19
## 17 18 19 20 21 22 24 25 26 27 35 45 46 58 60
## 10 8 4 3 5 2 1 1 1 1 1 1 1 1 1
##
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 1.000 4.000 5.000 5.574 7.000 60.000
##
## includes extended item information - examples:
## labels
## 1 Aloo Chaat
## 2 Aloo Gobi
## 3 Aloo Methi
##
## includes extended transaction information - examples:
## transactionID
## 1 630
## 2 647
## 3 648
The most frequently ordered dishes are Pilau Rice, Plain Naan, Plain Papadum, Garlic Naan, and Onion Bhajee. The first three dishes account for 90% of all transactions. If all the baskets contained all possible products, 3,322,456 orders would have been made (13,397 * 248). Since the density is 2.25%, 74,674 orders were placed (3,322,456 * 0.0225).
itemFrequencyPlot(data_trans, topN=15, type="relative", main="Item frequency", col="#56B4E9")
The top 5 dishes are the ones mentioned above. The most frequently ordered is Pilau Rice. Also, popular dishes include: Plain Rice, Chicken Tikka Masala, Mango Chutney and others.
The Apriori algorithm, a cornerstone in the field of data mining, provides a systematic approach to uncovering relationships between items in large transaction databases. Let’s apply the Apriori algorithm with default values (confidence = 0.8, support = 0.1).
apriori_rules <- apriori(data_trans)
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.8 0.1 1 none FALSE TRUE 5 0.1 1
## maxlen target ext
## 10 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 1339
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[248 item(s), 13397 transaction(s)] done [0.00s].
## sorting and recoding items ... [13 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 done [0.00s].
## writing ... [0 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
The algorithm has not found any rules with the appropriate support and confidence levels. To generate association rules, we reduce the support coefficient to 0.02. The algorithm has already found 11 rules.
apriori_rules <- apriori(data_trans, parameter = list(supp = 0.02, conf = 0.8))
## Apriori
##
## Parameter specification:
## confidence minval smax arem aval originalSupport maxtime support minlen
## 0.8 0.1 1 none FALSE TRUE 5 0.02 1
## maxlen target ext
## 10 rules TRUE
##
## Algorithmic control:
## filter tree heap memopt load sort verbose
## 0.1 TRUE TRUE FALSE TRUE 2 TRUE
##
## Absolute minimum support count: 267
##
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[248 item(s), 13397 transaction(s)] done [0.00s].
## sorting and recoding items ... [63 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3 4 done [0.00s].
## writing ... [11 rule(s)] done [0.00s].
## creating S4 object ... done [0.00s].
The graphic below shows 11 association rules:
plot(apriori_rules, method="graph")
We observe a strong link between such dishes as: Mint Sauce, Mango Chutney and Plain Papadum; Onion Chutney, Mango Chutney and Plain Papadum.
plot(apriori_rules, method = "scatterplot", measure = c("support", "confidence"), shading = "lift")
Confidence measures how often a rule is true when the conditions on the left-hand side (LHS) are satisfied.It indicates the probability that an item or item set on the right-hand side (RHS) of the rule will occur in a transaction when the items on the LHS are present. Formula: Confidence(A → B) = (Support(A ∪ B)) / (Support(A))
confidence <- sort(apriori_rules, by = "confidence", decreasing = TRUE)
inspect(head(confidence, n = 3))
## lhs rhs support confidence coverage lift count
## [1] {Mango Chutney,
## Mint Sauce,
## Onion Chutney} => {Plain Papadum} 0.02104949 0.9306931 0.02261700 3.467323 282
## [2] {Mango Chutney,
## Mint Sauce,
## Pilau Rice} => {Plain Papadum} 0.02321415 0.9014493 0.02575203 3.358375 311
## [3] {Mint Sauce,
## Onion Chutney} => {Plain Papadum} 0.03112637 0.8687500 0.03582892 3.236553 417
According to the highest confidence rates, we got the three important following rules:
Support measures the frequency of occurrence of an item set in a dataset. Formula: Support(A) = (Number of transactions containing item set A) / (Total number of transactions)
support <- sort(apriori_rules, by = "support", decreasing = TRUE)
inspect(head(support, n = 3))
## lhs rhs support confidence
## [1] {Mango Chutney, Mint Sauce} => {Plain Papadum} 0.04650295 0.8652778
## [2] {Mint Sauce, Pilau Rice} => {Plain Papadum} 0.03836680 0.8263666
## [3] {Mango Chutney, Onion Chutney} => {Plain Papadum} 0.03679928 0.8427350
## coverage lift count
## [1] 0.05374338 3.223617 623
## [2] 0.04642830 3.078652 514
## [3] 0.04366649 3.139633 493
The top three rules based on the support value are:
customers who buy Mango Chutney and Mint Sauce also order Plain Papadum in about 4.65% of the transactions;
customers who buy Mint Sauce and Pilau Rice also order Plain Papadum in about 3.84% of the transactions;
customers who buy Mango Chutney and Onion Chutney also buy Plain Papadum in about 3.68% of the transactions.
Lift quantifies how much more likely item B is purchased when item A is purchased, compared to when item B is purchased without item A. Formula: Lift(A → B) = (Confidence(A → B)) / (Support(B))
lift <- sort(apriori_rules, by = "lift", decreasing = TRUE)
inspect(head(lift, n = 3))
## lhs rhs support confidence coverage lift count
## [1] {Mango Chutney,
## Mint Sauce,
## Onion Chutney} => {Plain Papadum} 0.02104949 0.9306931 0.02261700 3.467323 282
## [2] {Mango Chutney,
## Mint Sauce,
## Pilau Rice} => {Plain Papadum} 0.02321415 0.9014493 0.02575203 3.358375 311
## [3] {Mint Sauce,
## Onion Chutney} => {Plain Papadum} 0.03112637 0.8687500 0.03582892 3.236553 417
The lift values are all above 1, which suggests that the presence of the items on the left-hand side increases the likelihood of Plain Papadum being purchased. This means that these combinations are stronger than random chance and can be used to create targeted promotions. Let’s explain the rules based on the lift coefficient:
The combination of Mango Chutney, Mint Sauce, and Onion Chutney has a lift of 3.467, which means that the likelihood of Plain Papadum being purchased when these three items are bought together is 3.467 times greater than the likelihood of Plain Papadum being bought at all. The confidence is very high at 93.06%, indicating that when customers buy these three items, they are very likely to also purchase Plain Papadum. However, this occurs in 2.1% of transactions, showing that while it’s a strong rule, it affects a smaller segment of transactions.
The second rule has a slightly lower lift at 3.358, suggesting that buying Mango Chutney, Mint Sauce, and Pilau Rice together increases the chances of Plain Papadum being purchased by 3.359 times compared to its overall likelihood of being bought. The confidence is also high at 90.14%, and the support is 2.32%, indicating that this rule is reliable but slightly less frequent than Rule 1.
With a lift of 3.237, the third rule indicates that the purchase of Mint Sauce and Onion Chutney together is 3.237 times more likely to result in the sale of Plain Papadum than chance. It has the highest support at 3.11%, meaning it occurs more frequently in the dataset than the other two rules, and it has a high confidence of 86.88%, making it a reliable rule.
plot(apriori_rules, method = "grouped", control = list(k = 11))
The top-15 frequent items also include such popular dishes as Plain Naan, Garlic Naan and Onion Bhajee. Let’s explore the association rules related to these dishes.
rules_naan <- apriori(data=data_trans, parameter=list(supp=0.01, conf = 0.5), appearance=list(default="lhs", rhs="Plain Naan"), control=list(verbose=F))
rules_naan_s <- sort(rules_naan, by="support", decreasing=TRUE)
inspect(head(rules_naan_s))
## lhs rhs support confidence
## [1] {Chicken Tikka Masala, Plain Rice} => {Plain Naan} 0.01978055 0.5567227
## [2] {Curry} => {Plain Naan} 0.01925804 0.5149701
## [3] {Pilau Rice, Plain Rice} => {Plain Naan} 0.01858625 0.5928571
## [4] {Bombay Aloo, Chicken Tikka Masala} => {Plain Naan} 0.01724267 0.5539568
## [5] {Mango Chutney, Plain Rice} => {Plain Naan} 0.01694409 0.5550122
## [6] {Bombay Aloo, Plain Rice} => {Plain Naan} 0.01388371 0.5068120
## coverage lift count
## [1] 0.03553034 1.987850 265
## [2] 0.03739643 1.838767 258
## [3] 0.03135030 2.116873 249
## [4] 0.03112637 1.977974 231
## [5] 0.03052922 1.981743 227
## [6] 0.02739419 1.809638 186
When ordering left-hand side set of products, customers will buy Plain Naan with a probability of 50-59% (as stated in the 6 rules above). In addition, the first three rules have a high level of support and lift coefficients. It means that these combinations of dishes are the common trends and can be used for promotions or cross-seling.
The graph below shows the rules for the Plain Naan dish.
plot(rules_naan, method="graph")
rules_gnaan <- apriori(data=data_trans, parameter=list(supp=0.01, conf = 0.3), appearance=list(default="lhs", rhs="Garlic Naan"), control=list(verbose=F))
rules_gnaan_s <- sort(rules_gnaan, by="support", decreasing=TRUE)
inspect(head(rules_gnaan_s))
## lhs rhs support confidence
## [1] {Butter Chicken} => {Garlic Naan} 0.02231843 0.3051020
## [2] {Butter Chicken, Pilau Rice} => {Garlic Naan} 0.01142047 0.3355263
## coverage lift count
## [1] 0.07315071 1.555347 299
## [2] 0.03403747 1.710444 153
Some of the popular combinations are Butter Chicken and Garlic Naan, Butter Chicken with Pilau Rice and Garlic Naan. When customers order Butter Chicken or Pilau Rice, the staff can offer Garlic Naan.
rules_bhajee <- apriori(data=data_trans, parameter=list(supp=0.01, conf = 0.4), appearance=list(default="rhs", lhs="Onion Bhajee"), control=list(verbose=F))
rules_bhajee_c <- sort(rules_bhajee, by="confidence", decreasing=TRUE)
inspect(head(rules_bhajee_c))
## lhs rhs support confidence coverage lift
## [1] {Onion Bhajee} => {Pilau Rice} 0.07942077 0.4431487 0.1792192 1.257544
## count
## [1] 1064
In almost 8% of all transactions, customers buy Onion Bhajee with Pilau Rice. In addition, the lift coefficient is higher than 1. These results indicate that it is a frequently ordered combination of two dishes and can also be used for promotions.
Let’s analyze the most popular dish Pilau Rice as both a left-hand side product and a right-hand side product.
rules_rice_lhs <- apriori(data=data_trans, parameter=list(supp=0.01, conf = 0.3), appearance=list(default="rhs", lhs="Pilau Rice"), control=list(verbose=F))
rules_rice_lhs_s <- sort(rules_rice_lhs, by="support", decreasing=TRUE)
inspect(head(rules_rice_lhs_s))
## lhs rhs support confidence coverage lift
## [1] {Pilau Rice} => {Plain Papadum} 0.1332388 0.3780979 0.3523923 1.408614
## [2] {Pilau Rice} => {Plain Naan} 0.1291334 0.3664478 0.3523923 1.308449
## count
## [1] 1785
## [2] 1730
In 13% of transactions, a customer buys Plain Papadum or Plain Naan after having Pilau Rice in their basket. But the likelihood of such situations is not very high (38% and 37%).
rules_rice_rhs <- apriori(data=data_trans, parameter=list(supp=0.01,conf = 0.6), appearance=list(default="lhs", rhs="Pilau Rice"), control=list(verbose=F))
rules_rice_rhs_s <- sort(rules_rice_rhs, by="support", decreasing=TRUE)
inspect(head(rules_rice_rhs_s))
## lhs rhs support confidence
## [1] {Chicken Tikka Masala, Plain Naan} => {Pilau Rice} 0.04030753 0.6257242
## [2] {Chicken Tikka Masala, Plain Papadum} => {Pilau Rice} 0.03687393 0.6159601
## [3] {Peshwari Naan, Plain Papadum} => {Pilau Rice} 0.02396059 0.6056604
## [4] {Bombay Aloo, Chicken Tikka Masala} => {Pilau Rice} 0.02015377 0.6474820
## [5] {Korma, Plain Papadum} => {Pilau Rice} 0.01970590 0.6226415
## [6] {Onion Bhajee, Peshwari Naan} => {Pilau Rice} 0.01910876 0.6139089
## coverage lift count
## [1] 0.06441741 1.775647 540
## [2] 0.05986415 1.747938 494
## [3] 0.03956110 1.718710 321
## [4] 0.03112637 1.837390 270
## [5] 0.03164888 1.766899 264
## [6] 0.03112637 1.742118 256
Analyzing Pilau Rice as a right-hand side product, we notice that the most frequently ordered combination is with Chichen Tikka Masala and Plain Naan (540 transactions).
The graph below shows the rules for the Pilau Rice dish.
plot(rules_rice_rhs, method="graph")
The Apriori method was used to study the behavior of customers of an Indian takeaway restaurant in London. Comparing the association rules by confidence, support, and lift coefficients, we came to the following conclusions:
The rules sorted by confidence suggest that when customers buy certain combinations of Mango or Onion Chutneys, Mint Sauce and Pilau Rice, they are very likely to also buy Plain Papadum. A restaurant could use this information to suggest Plain Papadum to customers buying these combinations, perhaps with a special offer to encourage the sale.
The second set of rules (by support) suggests that Plain Papadum is a common item that gets added to the basket with various combinations of other items. A restaurant should ensure they are well-stocked with Plain Papadum and consider bundling these items together.
The lift values of all association rules are all above 1, which suggests that the presence of the items on the left-hand side increases the likelihood of Plain Papadum being purchased. This means that these combinations are stronger than random chance and can be used to create targeted promotions.
In addition, there are many trendy combinations of Pilau Rice with Chicken Tikka Masala; Mango Chutney and Plain Papadum, etc. Curry with Plain Naan; Bombay Aloo, Chicken Tikka Masala with Plain Naan; Butter Chicken with Garlic Naan; or Onion Bhajee with Pilau Rice are interesting association rules with a high lift indicator. The restaurant may design the menu to highlight these combinations that lead to a higher lift coefficient, encouraging customers to try these combinations. By leveraging these rules, restaurants can potentially increase their average order value and improve customer satisfaction by offering popular item combinations.