Coders Packet

Grocery Items Recommendation using Eclat Algorithm in Python - Association Rule Learning

By Radhika Talwar

In this Python Packet, We are going to Train our machine using the Eclat algorithm in which we can find the best deals that can be provided by Grocery Vendors to maximize his revenue.

There is a Dataset that contains details of about 7000 customers recorded by a grocery vendor and with this, we are trying to train our machine using Association Rule learning.

After the machine is trained, It will tell the vendors to make a set of products that can be combined other sets of products and in this deal when a customer will buy a product he will be given some product free.

For example: In the given set of observations, Machine-made an observation that if any person will buy Burger is likely to buy Ketchup also so with the information the vendors generate a deal if any person will buy 3 burgers he will get 5 kinds of ketchup free. So this deal will attract people and will Tempt them to buy this deal rather than buying only one burger.  

So This Association Rule Learning is of two types:

1.Apriori          

2. Eclat

In Eclat, the machine is trained to check the combination of products which is used the maximum number of times in the database, and this calculated using the Support parameter. It is Set based Learning in which a set of products is created after training.

Support: It indicates how frequently the itemsets appear in the given database. It is the fraction of records that contains the union of some products to the total numbers of records in the database, like 0.2 means 0.2% of the complete database.

Support (XY) = Support count of (XY) / Total number of transaction in Dataset.

Our first task is to install apyori library. This will itself install the apyori Library and we will importing the libraries.

!pip install apyori
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

Now we will Import Dataset and convert it in a list so as to increase readability for the machine.

dataset = pd.read_csv('Market_Basket_Optimisation.csv', header = None)
transactions = []
for i in range(0, 7501):
  transactions.append([str(dataset.values[i,j]) for j in range(0, 20)])

Now We will Train the Eclat Model on the Dataset. These parameters are the list named Transactions then the minimum support we need and The number of products we need in deal is 2.

from apyori import apriori
rules = apriori(transactions = transactions, min_support = 0.003, min_length = 2, max_length = 2)

Visualizing the results. It will show us the top 10 sets of Product 1 and Product 2 and their corresponding Support.

results = list(rules)
def inspect(results):
    lhs         = [tuple(result[2][0][0])[0] for result in results]
    rhs         = [tuple(result[2][0][1])[0] for result in results]
    supports    = [result[1] for result in results]
    return list(zip(lhs, rhs, supports))
resultsinDataFrame = pd.DataFrame(inspect(results), columns = ['Product 1', 'Product 2', 'Support']
resultsinDataFrame.nlargest(n = 10, columns = 'Support')

Dataset is attached. You can try it yourself.

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Radhika Talwar (radhikatalwar62)

Download packets of source code on Coders Packet