Apriori Algorithm Tutorial. Data mining and association rules over... | by François St-Amant | Towards Data Science

The main goal of association rules is to identify relations between products or variables in a dataset. The idea is to determine which products often come together. It is widely used in market basket analysis for instance, where the analyst, starting from a database containing all transactions, will try to determine which products come together, with what frequency, etc.
This information can be useful to optimize location of various products in a store or in planning for sales when a certain product goes on discount.
Apriori algorithm is the most popular algorithm for mining association rules. It finds the most frequent combinations in a database and identifies association rules between the items, based on 3 important factors:
Support: the probability that X and Y come togetherConfidence: the conditional probability of Y knowing x. In other words, how often does Y happen when X happened first.Lift: the ratio between support and confidence. A lift of 2 means that the likelihood of buying X and Y together is 2 times more than the likelihood of just buying Y.
Support: the probability that X and Y come together
Confidence: the conditional probability of Y knowing x. In other words, how often does Y happen when X happened first.
Lift: the ratio between support and confidence. A lift of 2 means that the likelihood of buying X and Y together is 2 times more than the likelihood of just buying Y.
In practice, a lift of at least 1 is necessary for a rule to be considered relevant.
Here is the implementation of the apriori algorithm using the mlxtend library. First, let’s import the library and look at the data, which comes from transactions from a restaurant.
from mlxtend.frequent_patterns import apriori, association_rulesdf.head(15)
This dataset contains more than 30,000 rows and about 9,000 transactions. For every transaction (which can span over multiple rows), what matters to us are the products that were included in the transaction.
The first thing we have to do is change the structure of the data, so that every transaction becomes a single row. We will use the unstack function on the product name, so that every possible product in the database becomes a column.
tl = df.unstack(level='productName').fillna(0)
Then, for every transaction, we want to know if a product is there (1) or not (0). We don’t care about the quantity of a product, only about it’s presence or not in the transaction.
#Function def presence(x):     if(x<= 0):         return 0    if(x>= 1):         return 1#Apply function tl_encoded = tl.applymap(presence)
We can then build the model with the apriori function and collect all the inferred rules that meet the minimum support of 0.01, using the association_rules function.
# Building the model frq_items = apriori(tl_encoded, min_support = 0.01, use_colnames = True)   # Collecting the inferred rules in a dataframe rules = association_rules(frq_items, metric ="lift", min_threshold = 1) 
This code will return frozensets like this:
[frozenset({'crpes sucre', 'Jus d'orange,'})]
I personally find it much more convenient to work with lists. Here is the code to make the conversion and create a dataframe containing all the association rules that the apriori algorithm created.
#Sort the rules rules = rules.sort_values(['lift'], ascending =[False])#From Frozenset to stringrules["antecedents"] = rules["antecedents"].apply(lambda x: list(x)[0]).astype("unicode")rules["consequents"] = rules["consequents"].apply(lambda x: list(x)[0]).astype("unicode")#Table with most relevant rulesprint(rules.sort_values('lift', ascending=False).nlargest(10, 'lift'))
Let’s interpret the first rule. It says that when crepes with sugar are in a transaction, orange juice often comes too. The lift of 3.22 means that the likelihood of buying crepes and orange juice together is 3.22 times more than the likelihood of just buying orange juice. The support of 0.01 means that they appear in transactions together in about 1% of all transactions.
This information could be useful in creating new offers for customer. For instance, we could have a buy a crepe, get an orange juice 50% off deal.
Thanks a lot for reading!