Market Basket Analysis



                   MARKET BASKET ANALYSIS                                                      

Introduction:

Market Basket Analysis explains the combinations of products that frequently co-occur in transactions. For example, people who buy bread and eggs, also tend to buy butter as many of them are planning to make an omelette. Marketing team should target customers who buy bread and eggs with offers on butter, to encourage them to spend more on their shopping basket.

Questions:

1)  Read the csv file into a dataframe. Inspect the file.
Ans:  Set the working directory, install the package arules then inspect the file as follows:
   setwd("C:/Users/hp/Documents/DataScience")                                                                
   library(arules)                                                                                                                   
  input<-read.transactions("groceries.csv",sep = ",")                                                         
  #first five transactions                                                                                                        
  inspect(input[1:5])                                                                                                             

  View(input)

Ouput:


2) Clean the dataset & create a transaction list to be given as input to apriori algorithm.
Ans: View the input for a better understanding of the data and then pass the data to apriori algorithm.
    input<-read.csv("groceries.csv",stringsAsFactors = F,header = F)
    View(input)
    summary(input)
    apriori(input)

    groceriesrules<-apriori(input, parameter = list(supp = 0.001, conf = 0.8))

Ouput:



3) Find the top 20 "sold items" that occur in the dataset.
Ans: After reading the file, use group_by clause, arrange function and summarise to get the required output.
input<-read.csv("groceries.csv",stringsAsFactors = F,header = F)
groceries<-as.factor(c(input$V1,input$V2,input$V3,input$V4))
class(groceries)
View(groceries)
library(dplyr)

df<-as.data.frame(groceries)
View(df)
colSums(is.na(df))
(group_by(df,groceries)%>%summarise(Count=n()))

#top 20 sold items:
  dff<-group_by(df,groceries)%>%summarise(Count=n())%>%arrange(desc(Count))%>%head(21)
  dff<-dff[-1,]

  dff

Output:



4)  Find how much of the total sales they account for.
Ans:We can find the total sales the above 20 products account for, by converting the data in a dataframe and then using group_by clause, arrange function and summarise to get the required output.
  df1<-group_by(df,groceries)%>%summarise(Count=n())%>%arrange(desc(Count))%>%head(21)
  df1<-df1[-1,]
  df1
  (sum(df1$Count))
  df2<-group_by(df,groceries)%>%summarise(Count=n())%>%arrange(desc(Count))
  df2<-df2[-1,]
  df2
  (sum(df2$Count))
  result<-((sum(df1$Count))/(sum(df2$Count)))*100
  result
Output:



  We can also create a pie chart as follows:

  pie(c(result,100-result),labels = c("Top 20","Rest Items"),col=c("red","yellow"))

Output:




5) Inspect the rules & suggest conclusion for storing Products on Rack to get Increase in sales.
Ans: By using apriori algorithm we can then inspect the rules as follows:
  groceriesrules<-apriori(input, parameter = list(supp = 0.001, conf = 0.8))

  inspect(groceriesrules)

Output:




CONCLUSION:
Thus market basket analysis was studied and executed properly. All the outputs were obtained by using various functions like summarise, group_by clause, arrange, etc. So we come to know what what a person may buy on the basis of what he/she buys now. That way, we can increase the sale by appropriate placing of the products.

Comments