Affinity Analysis (Market Basket Analysis) | by Eser Saygın | Towards Data Science

Have you ever wondered how often do you buy certain items together? Why do you buy some items together? How likely do you purchase an item along with another one? Why sugar placed near tea bags? Is there any relationship between these items? What is the main idea behind it?
The answer to all those questions is Affinity Analysis (can also be referred to as Market Basket Analysis). The main idea behind this analysis is to achieve valuable insights by identifying which items are frequently purchased together.
Affinity Analysis (or Market Basket Analysis) finds patterns to determine connections between purchases in order that stores can increase their cross-selling potential.
Before we get into the detail, we’ll have to explain some terms.
A market basket refers to a set of items that are commonly purchased and sold in an economic system. (Check out the image above to see the basic example of the market basket.)
As everyone knows, an itemset is a set of items that consist of two or more items. (What a piece of exciting information, right? :P)
Transactions are the main data source of Affinity Analysis. Stores or retailers gather tremendous transaction data by recording activities over time. Each transaction is associated with a unique transaction ID (TID), and it contains subsets of itemsets.
Support simply emphasizes how popular an itemset is. Support, despite being simple, is an important metric in the Affinity Analysis that used to determine the strength of association between items. Take 5 transactions, for instance. If you purchase bread in 3 transactions, you can tell the support of bread is equal to 3/5.
While the support emphasizes how popular an itemset is, confidence denotes the likelihood of certain items are purchased together. For instance, how likely butter is purchased when item bread is purchased. Confidence is typically notated as Bread ⇒ Butter (Proportion of transactions containing Bread that also contain Butter.)
Confidence, as you can see above, is a probability and so its range is [0,1]. If the confidence of Bread ⇒ Butter is equal to 1, we can say every time a customer purchases bread, also purchases butter.
Like confidence, the lift is notated as Bread ⇒ Butter. It says how likely Butter is purchased when Bread is purchased while controlling for how popular Butter is.
Lift’s range is [0, +∞]. When lift equal to one, bread and butter are independent and, thus, no inferences can be made about butter when the bread is purchased. However, when the lift is greater than 1, it means that the butter is likely to be purchased together with bread.
Wikipedia says “ conviction can be interpreted as the ratio of the expected frequency that X occurs without Y (that is to say, the frequency that the rule makes an incorrect prediction) if X and Y were independent divided by the observed frequency of incorrect predictions.” Conviction has the range [0,+∞] and the following form:
So, let’s go over it to make sure you clearly understand what the terms mean.
Step 1:
Let’s create transaction data.
Step 2:
Let’s define a function.
This function gives the values of support, confidence, lift and conviction. (By the way, you can determine more than two parameters.)
frequency_items('bread','butter')
36% of transactions contain both bread and butter. Butter appears every time in transactions that contain bread only. Confidence = 1 indicates that butter is always purchased whenever bread is purchased. Lastly, the value of lift is greater than 1 and it means it is more likely bread and butter will be bought together than each individually.
(Remember our dataset is pretty small. In practice, a rule needs the support of several hundred transactions.)
The Apriori algorithm is a data mining technique for identifying the frequent itemsets and relevant association rules in the database. Support, confidence and lift are the three main components of the Apriori Algorithm.
Let’s illustrate the apriori algorithm using an example:
Step 1:
In the first step, we will create a frequency table of items in the transactions. Let us set the minimum support to 2.
As you notice, all the items have greater support than the minimum support (2).
Step 2:
In this step, we will make all the possible pairs of items. (Order doesn’t matter, i.e {Bread,Butter} = {Butter, Bread})
Again we need to compare these pairs with the minimum support(2).
Repeat previous steps:
Now analyze 3 item sets and calculate the frequency, and apply the threshold again. (Bread, Butter, Beer), (Bread, Butter, Toilet Paper)...
As you notice the algorithm scans the database over and over again. Hence, overall performance reduces.
I’m going to use Groceries Market Basket Dataset. (Kaggle Link)
Loading data.
Loading data.
2. Importing packages and data preprocessing (about mlxtend)
3. Converting to dataframe.
4. Calculating support.
5. Creating rules (Metric: Confidence) Antecedents ⇒ Consequents
6. Creating rules (Metric: Lift) Antecedents ⇒ Consequents
7. Scatter plot
Scatter plots help us to evaluate general tendencies of rules between antecedents and consequents.
In this post, we have had a glimpse into what Affinity Analysis is and how to implement it in python.
Affinity Analysis or Market Basket Analysis is used to extract valuable insights from transaction data. It can be used to determine what products to discount. Also, it can increase sales and customer satisfaction. It is important to realize that there are many other areas in which it can be applied.
You can find the mlxtend documentation here.