Market Basket Analysis Using Association Rule-Mining

What is Market Basket Analysis?

Market basket analysis is a technique used by retailers to find patterns in customer behavior based on their history of transactions .It is used to determine what items are frequently bought together or placed in the same basket by customers. It uses this purchase information to leverage effectiveness of sales and marketing. Market basket Analysis(MBA) looks for combinations of products that frequently occur in purchases and has been prolifically used since the introduction of electronic point of sale systems that have allowed the collection of immense amounts of data.

Types Of Market Basket Analysis:

  • Predictive MBA is used to classify cliques of item purchases, events and services that largely occur in sequence.
  • Differential MBA removes a high volume of insignificant results and can lead to very in-depth results. It compares information between different stores, demographics, seasons of the year, days of the week and other factors.

What is Association Rule-Mining?

Association rule mining is a technique to identify frequent patterns and associations among a set of items.

What Is an Apriori Algorithm?

Apriori algorithm assumes that any subset of a frequent itemset must be frequent.

How Does the Apriori Algorithm Work?

The key concept in the Apriori algorithm is that it assumes all subsets of a frequent itemset to be frequent. Similarly, for any infrequent itemset, all its supersets must also be infrequent.

  • Confidence
  • List
  • Conviction

The libraries used here are:-


Importing Libraries:

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import pandas as pd
from mlxtend.preprocessing import TransactionEncoder
from mlxtend.frequent_patterns import apriori
from mlxtend.frequent_patterns import association_rules

Loading CSV file


Data Preparation

records = []
for i in range (0, 9835):
records.append([str(df.values[i,j]) for j in range(0, 20)])

For implementing Apriori Algorithm we will use Mlxtend.

First we will find the frequent itemset with support.


  • The most popular item in this data set is whole milk followed by vegetables and rolls/buns.
  • By applying the Apriori algorithm and association rules we can have a better insight on what items are more likely to be bought together.

For Code and Dataset:

A Information Technology Student with a focus in Data Science, Machine Learning, Android Development