Association Rules

Apriori Algorithm

Market Basket Dataset

library(arules)
## Loading required package: Matrix
## 
## Attaching package: 'arules'
## The following objects are masked from 'package:base':
## 
##     abbreviate, write
library(arulesViz)
## Warning: package 'arulesViz' was built under R version 3.5.1
## Loading required package: grid
mydata <- read.csv("C:\\Users\\RISHI RAHUL\\Desktop\\DS\\8 Association Rules\\Assignment\\Market_Basket.csv", header = FALSE)

mydata <-  read.transactions('C:\\Users\\RISHI RAHUL\\Desktop\\DS\\8 Association Rules\\Assignment\\Market_Basket.csv', sep = ',', rm.duplicates = TRUE)
## distribution of transactions with duplicates:
## 1 
## 5
itemFrequencyPlot(mydata,topN=20)

rules <- apriori(mydata, parameter = list(supp = 0.005, confidence = 0.50, minlen = 2, maxlen = 3))
## Apriori
## 
## Parameter specification:
##  confidence minval smax arem  aval originalSupport maxtime support minlen
##         0.5    0.1    1 none FALSE            TRUE       5   0.005      2
##  maxlen target   ext
##       3  rules FALSE
## 
## Algorithmic control:
##  filter tree heap memopt load sort verbose
##     0.1 TRUE TRUE  FALSE TRUE    2    TRUE
## 
## Absolute minimum support count: 37 
## 
## set item appearances ...[0 item(s)] done [0.00s].
## set transactions ...[119 item(s), 7501 transaction(s)] done [0.00s].
## sorting and recoding items ... [101 item(s)] done [0.00s].
## creating transaction tree ... done [0.00s].
## checking subsets of size 1 2 3
## Warning in apriori(mydata, parameter = list(supp = 0.005, confidence =
## 0.5, : Mining stopped (maxlen reached). Only patterns up to a length of 3
## returned!
##  done [0.00s].
## writing ... [20 rule(s)] done [0.00s].
## creating S4 object  ... done [0.00s].
inspect(head(sort(rules), n = 10))
##      lhs                                rhs             support    
## [1]  {ground beef,milk}              => {mineral water} 0.011065191
## [2]  {eggs,ground beef}              => {mineral water} 0.010131982
## [3]  {frozen vegetables,ground beef} => {mineral water} 0.009198773
## [4]  {frozen vegetables,ground beef} => {spaghetti}     0.008665511
## [5]  {milk,soup}                     => {mineral water} 0.008532196
## [6]  {milk,olive oil}                => {mineral water} 0.008532196
## [7]  {chocolate,olive oil}           => {mineral water} 0.008265565
## [8]  {chicken,chocolate}             => {mineral water} 0.007598987
## [9]  {soup,spaghetti}                => {mineral water} 0.007465671
## [10] {ground beef,pancakes}          => {mineral water} 0.007465671
##      confidence lift     count
## [1]  0.5030303  2.110308 83   
## [2]  0.5066667  2.125563 76   
## [3]  0.5433071  2.279277 69   
## [4]  0.5118110  2.939582 65   
## [5]  0.5614035  2.355194 64   
## [6]  0.5000000  2.097595 64   
## [7]  0.5040650  2.114649 62   
## [8]  0.5181818  2.173871 57   
## [9]  0.5233645  2.195614 56   
## [10] 0.5137615  2.155327 56
plot(head(sort(rules, by = "lift"), n = 5), method = "graph", control = list(cex = 1.3)) #cex = font size

plot(rules)

plot(head(sort(rules, by = "lift"), n = 10), method = "grouped")