Association Rule Mining using Market Basket Analysis | by Sarit Maitra | Towards Data Science

https://sarit-maitra.medium.com/membership
MARKET Basket Analysis(MB) is an association analysis and is a popular data mining technique. It’s a kind of knowledge discovery in data (KDD) and this technique can be applied in various fields of work. Here, I will use a retail transaction data and show how to provide the information to business capturing the purchase behavior of the buyer. This can also be a part of decision support system.
Various forms of data mining can be applied on this kind of data set e.g. classification, association, prediction, clustering, outlier analysis etc. Here, I will focus on association rule mining technique which is discovering interesting relationships hidden in data sets. If you are interested, may visit my earlier articles (1); (2); (3).
Many of us are familiar with the below data set published at UCI machine learning data base. Data used in MB analysis is transactional data. Here, the data has all the basic ingredients of MB analysis. However, from my experience, transaction data rarely or we can rather say, never comes in this kind of format. Therefore, due to the complexity of transaction data, data cleaning and data reading is a major activity for MB analysis.
Let’s see the dimension of the data frame as well as unique invoiceno and customerid
If we compare “invoiceno” vis-à-vis “description”, we can see that some of the rows do not have invoice number.
data[‘invoiceno’].value_counts()
Let’s drop the rows that don’t have invoice numbers and remove the credit transactions (those with invoice numbers containing C).
Now, we need to consolidate the items into 1 transaction per row with each product 1 hot encoded. Let’s check the name of the countries in the data frame.
Let’s look at sales for Australia and compare the results to sales from France. Before we dive into analysis, let’s understand the association rule.
Rule generation is the first and foremost task in the mining of frequent patterns in such cases. An association rule is an implication expression of the form x → y, where x and y are disjoint itemsets. To evaluate the “interest” of such an association rule, different metrics have been developed. I will use the support, confidence and lift metrics.
Suppose item x is being bought by the customer, then the chances of item y being picked by the customer too under the same Transaction ID is found out. There are 3 ways to measure association: Support, Confidence and Lift.
Support {freq (x,y) / n, range: [0, 1]} gives the fraction of transactions which contains item x and y. It tells us about the frequently bought items or the combination of items bought frequently and we can filter out the items that have a low frequency.
Confidence {freq(x,y) / freq (x), range: [0, 1]} tells us how often the items x and y occur together, given the number times x occurs.
Lift {support/support(x) * support(y), range: [0, inf]} indicates the strength of a rule over the random occurrence of x and y. It explains the strength of a rule and more the Lift more is the strength.
This is a data mining methodology and the foundation of association rule. Apriori algorithm comes into act using “frequent itemsets” to generate association rules. It takes into consideration that, that a subset of a “frequent itemset” must also be a “frequent itemset”. The value of “frequent itemset” > than a threshold value(i.e. support).
Quite a lot of zeros in the data but we also need to make sure any positive values are converted to a 1 and anything less the 0 is set to 0. so, let’s apply one hot encoding of the data and remove the postage column; we are not going to explore postage.
Now that the data is structured properly, we can generate frequent item sets that have a support of at least 7% (this number was chosen so that I could get enough useful examples).
To select interesting rules we can use best-known constraints which are a minimum thresholds on confidence and support.
“Support is an indication of how frequently the itemset appears in the dataset. Confidence is an indication of how often the rule has been found to be true”
# generate the rules with their corresponding support, confidence # and liftfrequent_itemsets = apriori(basket_sets, min_support=0.07, use_colnames=True)print (frequent_itemsets)rules = association_rules(frequent_itemsets, metric=”lift”, min_threshold=1)rules.head()
If we print the number of associations, we see that 800 association rules were found.
support=rules.as_matrix(columns=[‘support’])confidence=rules.as_matrix(columns=[‘confidence’])
Below scatter plot for support and confidence showing the association rules (first 10 rules) for the data set.
Let’s find out what this tells us. For instance, we can see that there are quite a few rules with a high lift value which means that it occurs more frequently than would be expected given the number of transaction and product combinations. We can also see several where the confidence is high as well. Here, domain expertise has major advantage. I will look for a large lift (6) and high confidence (0.8) here.
Looking at the rules, we can figure out that, “RED RETROSPOT CAKE STAND” and “36 PENCILS TUBE RED RETROSPOT” are purchased together and “4 TRADITIONAL SPINNING TOPS”, “ALARM CLOCK BAKELIKE GREEN” and “RED DINER WALL CLOCK” are purchased together in a manner that is higher than the overall probability would suggest. At this point, we may want to look at how much opportunity there is to use the popularity of one product to drive sales of another.
We can see that, though there is an association rule exists, but compared to 385 numbers of “36 PENCILS TUBE RED RETROSPOT” but only 73 numbers of “RED RETROSPOT CAKE STAND” so maybe business has to take some strategy here to bring both at par.
What is also interesting is to see how the combinations vary by country of purchase. Let’s check out what some popular combinations might be in France.
So, this way we can compare and prepare an analysis report. Based on the association rule that we have defined, we found some significant correlation among some of the products. The priori algorithm applied here with the certain threshold. We can experiment with different threshold value as well. Larger the lift means more interesting association. Association rules with high support are potentially interesting rules. Similarly, rules with high confidence would be interesting rules as well.
I can be contacted here.