Market Basket Analysis using Association Rule Mining.
By: Kirthana R.D
Tags: Market Basket Analysis, Data Mining, Python, Apriori Algorithm, Jupyter Notebook

Association rule mining:

Association Rules are widely used to analyze retail basket or transaction data, and are intended to identify strong rules discovered in transaction data using measures of interestingness, based on the concept of strong rules. The outcome of this type of technique is, in simple terms, a set of rules that can be understood as “if this, then that”.

Apriori algorithm is a classical algorithm in data mining. It is used for mining frequent itemsets and relevant association rules. It is devised to operate on a database containing a lot of transactions.

Support is an indication of how frequently the itemset appears in the dataset.The support of X with respect to T is defined as the proportion of transactions t in the dataset which contains the itemset X.

Confidence is an indication of how often the rule has been found to be true. The confidence value of a rule, X=>Y , with respect to a set of transactions T,, is the proportion of the transactions that contains X which also contains Y.

The lift of a rule is defined as:

or the ratio of the observed support to that expected if X and Y were independent.

Formulas:

Input Dataset :http://archive.ics.uci.edu/ml/machine-learning-databases/00352/

Let’s look at the code of market basket analysis using Python:

code:

pip install mlxtend

import numpy as np

import pandas as pd

from mlxtend.frequent_patterns import apriori, association_rules

#loading data

data = pd.read_excel(‘Online Retail.xlsx’)

data.head()

Output

#exploring columns

data.columns

data.Country.unique()

Cleaning The Data

# Stripping extra spaces in the description

data[‘Description’] = data[‘Description’].str.strip()

# Dropping the rows without any invoice number

data.dropna(axis = 0, subset =[‘InvoiceNo’], inplace = True)

data[‘InvoiceNo’] = data[‘InvoiceNo’].astype(‘str’)

# Dropping all transactions which were done on credit

data = data[~data[‘InvoiceNo’].str.contains(‘C’)]

Splitting data

# Transactions done in France

basket_France = (data[data[‘Country’] ==”France”]

.groupby([‘InvoiceNo’, ‘Description’])[‘Quantity’]

.sum().unstack().reset_index().fillna(0)

.set_index(‘InvoiceNo’))

# Defining the hot encoding function to make the data suitable for the #concerned libraries

def hot_encode(x):

if(x<= 0):

return 0

if(x>= 1):

return 1

basket_encoded = basket_France.applymap(hot_encode)

basket_France = basket_encoded

Building model

frq_items = apriori(basket_France, min_support = 0.05, use_colnames = True)

rules = association_rules(frq_items, metric =”lift”, min_threshold = 1)

rules = rules.sort_values([‘confidence’, ‘lift’], ascending =[False, False])

print(rules.head())

CONCLUSION:

From the above output, it can be seen that paper cups and paper and plates are bought together in France. This is because the French have a culture of having a get-together with their friends and family atleast once a week. Also, since the French government has banned the use of plastic in the country, the people have to purchase the paper -based alternatives.

For germany:

basket2 = (data[data[‘Country’] ==”Germany”]

.groupby([‘InvoiceNo’, ‘Description’])[‘Quantity’]

.sum().unstack().reset_index().fillna(0)

.set_index(‘InvoiceNo’))

basket_sets2 = basket2.applymap(hot_encode)

basket_sets2.drop(‘POSTAGE’, inplace=True, axis=1)

frequent_itemsets2 = apriori(basket_sets2, min_support=0.09, use_colnames=True)

rules2 = association_rules(frequent_itemsets2, metric=”lift”, min_threshold=1)

print(rules2.head())

rules2[ (rules2[‘lift’] >= 4) &

(rules2[‘confidence’] >= 0.5)]

CONCLUSION:

It seems that in addition to round snack box set of 4 fruits and 4 woodland, Germans love Plasters in Tin Spaceboy and Woodland Animals.

For Entire code visit my Github: https://github.com/kirthanard/market-basket-analysis.git