Association Rule Learning
By: Bob Rupak Roy
Tags: Association Rule, Machine Learning, R Programming, Marketing Strategies, Data Science

Apriori

Association Rule Learning: Apriori is one of the powerful algorithm to understand association among the products. Take an example of a super market where most of the person buys egg also buys milk and also baking soda. Probably the reason is they want to bake cake for the new year eve.

So we can see there is an association among eggs, milk as well as baking soda. Now after knowing such association we simply put all the 3 things together in the shelf and that definitely will increase our sales.

Let’s perform Apriori with the help of an example. First we will try with R then we will move on with Python.

Install and load the library ‘arules’ that contains ‘apriori’ function.

install.packages('arules'

library(arules) dataset = read.csv(file.choose(), header = FALSE)

Shopping list Data set

if we import our data set with our regular read.csv function we will not able to view ‘itemFrequencyPlot’ So we will use ‘read.transactions’

dataset = read.transactions(file.choose(), sep = ',', rm.duplicates = TRUE)

Filter Option

If we filter our dataset to ‘milk’ we can see items sold with ‘milk’

This dosnt mean we can simply do the COUNT and figure it out the association of milk.

Its simply not appropriate when it comes to multivariate analysis.

Okay assume u have figure it out the association of milk. People who buy milk also buy item 1,item 3, item 5 by just filtering and having done with time consuming count calculation.

What about Beer? People who buy Beer also buy milk and other products. So will you be able to find association between both? Nope

So these is where Association Rule Learning : Apriori comes into the picture. Apriori will help us to find hidden patterns like for this example. People who buy Milk, egg, baking soda to bake cake and will also buy Beer and snacks becoz there is a festival nearby.

Association Rule Learning have lots of applications to understand and predict the trend. One of the best commonly used example is Movie Recommendation.

itemFrequencyPlot(dataset, topN = 10)

summary(dataset)

TopN =10 refers to the top 10 items.

Summary gives brief details of our data set attributes.

Item Frequency Plot

Now we need to understand 3 concepts support, confidence and lift to apply Apriori

Support: refers to transactions containing the item / total transactions

Confidence: Transactions containing the item / total items

Another example in Movie Recommendation:

Number of movies the users seen / total user watch list

Lift: is confidence / support, refers what is the probability of likelihood the consumers will buy milk will also buy egg, baking soda and beer from a random new sample of population.

So the lift is the improvement over the original prediction.

#Training Apriori on the data set

rules = apriori(data = dataset, parameter = list(support = 0.004, confidence = 0.3))

Support: 5*7/7500 =0.0046 i.e. a product that is purchased 5 times a day X 7 days in a week / total number of transaction

Confidence is totally based on business problem. However if we give low confidence then it will give inappropriate Rules like people who buy milk also buy washing power. If we are not able to get any rules then try to reduce the confidence level as there might be not be enough data for Apriori to generate rules.

#The results

inspect(sort(rules, by = 'lift')[1:10])

Here with inspection we are looking into first 10 rules but we need to sort by ‘lift” will give us 10 highest/strongest rules.

Apriori Rules

If people buy Pasta they will also buy escalope in 37% of the cases and again if people buy olive oil, tomatoes they will also buy spaghetti in 61% of the cases.

Amazing insights isn’t it?

There might have some rules that has no correlation is becoz of the ‘Support’ value. That is the times the item sold each day is higher so it will be reflecting it the output.

Offcourse we need all of our products to be in the rule. What we can do to fix this is by increasing the confidence value that’s it!

Alright let’s put all of the pieces together

#Apriori

#install.packages('arules')

library(arules)

dataset = read.csv(file.choose(), header = FALSE) dataset = read.transactions(file.choose(), sep = ',', rm.duplicates = TRUE) summary(dataset)

itemFrequencyPlot(dataset, topN = 10,col='blue') #Training Apriori on the dataset

rules = apriori(data = dataset, parameter = list(support = 0.004, confidence = 0.3)) #The results

inspect(sort(rules, by = 'lift')[1:10])

Another quicker and simplified version of Apriori is the Eclat

In Eclat we only need the Support and the minimum set of items (minlen =2) that is minimum of at least 2 items that has been purchased together

# Training Eclat on the dataset

rules = eclat(data = dataset, parameter = list(support = 0.004, minlen = 3))

Eclat output

One interesting fact if u have noticed, here we have 267 sets and before in Apriori we had Rules. Even though Eclat is considered to be Association Rule Learning, actually returns sets of items.

In our case atleast/minimum 3 set of items purchased together.

inspect(sort(rules, by = ‘support’)[1:10])

Finally in our inspect this time we don’t have ‘lift’ we will sort our result by ‘Support’ in descending/high to low order.

Eclat

So in Eclat we get different sets of items that are been purchased frequently. together.

let’s put all the codes together

# Eclat

# install.packages('arules')

library(arules)

dataset = read.csv(file.choose())

dataset = read.transactions(file.choose(), sep = ',', rm.duplicates = TRUE)

summary(dataset)

itemFrequencyPlot(dataset, topN = 10) #Training Eclat on the dataset

rules = eclat(data = dataset, parameter = list(support = 0.004, minlen = 3)) inspect(sort(rules, by = 'support')[1:10])

Done that’s it….!

Thanks your time to read to the end. I tried my best to keep it short and simple keeping in mind to use this code in our daily life.

I hope you enjoyed.

Feel Free to ask because “Curiosity Leads To Perfection”

My alternative internet presences, Facebook, Blogger, Linkedin, Medium, Instagram, ISSUU and my very own D2Academics, Data2Dimensions

Also available on Quora @ https://www.quora.com/profile/Bob-Rupak-Roy

Stay tuned for more updates.! have a good day….

~ Be Happy and Enjoy !