Recommendation Systems: ARL (Association Rule Learning)

zeynep beyza ayman
6 min readNov 30, 2022

--

Jerry-Lee Bosmans

Recommendation systems aim to recommend content, products and services to users using some methods and algorithms. Although it has been used since the 90s, it has been used extensively in data analytics since 2009.

While content is plentiful, users’ interests tend to be more customized to the entire content set and differ from person to person. In order not to get lost in this abundant cluster and to reach the desired personalized service according to the field of interest, various filters should be made. These filters and algorithms appear as “Recommendation Systems”. It is used in many areas such as dating applications, e-commerce sites, social media channels etc.

Recommendation systems can be examined under 4 main headings:

  • Simple Recommender Systems: Makes general recommendations with business knowledge or simple sequencing techniques.
  • Association Rule Learning: Makes suggestions according to the rules learned through association analysis.
  • Content-Based Filtering: Makes recommendations based on similarities of products.
  • Collaborative Filtering: Makes recommendations based on user or product according to common opinions. It is divided into 3 as User-Based, Product-Based, and Model-Based.

In this article, which will be the first part of Recommender Systems, the focus will be on Simple Recommender Systems and Association Rule Learning Stay tuned for Content-Based Filtering and Collaborative Filtering methods.

Simple Recommender Systems

Simple Recommender systems are not concerned with user behaviour or product features. These systems generally focus on the preferred, popular, legendary products, that have the highest score, etc. and recommend these products directly to the user.

Association Rule Learning

It is a rule-based machine learning technique used to find patterns in data. The Apriori Algorithm is used while the Association Rule Learning takes place. Apriori is a basket analysis method used to reveal product associations. There are 3 significant metrics in Apriori:

  • Support: Measures how often products X and Y are purchased together
Support(X, Y) = Freq(X, Y) / Total Transaction
  • Confidence: Probability of purchasing product Y when product X is purchased
Confidence(X, Y) = Freq(X, Y) / Freq(X)
  • Lift: The coefficient of increase in the probability of purchasing product Y when product X is purchased.
Lift = Support(X, Y) / (Support(X) * Support(Y))

How does the Apriori work?

The Apriori algorithm calculates possible product pairs according to the support threshold value determined at the beginning of the process and creates the final table by making eliminations according to the support value determined in each iteration.

  • Step 1: Calculate the support value of each product.
  • Step 2: Eliminate products with support values equal to or below the support threshold determined at the beginning of the process.
  • Step 3: Identify possible product pairs and calculate support values.
  • Step 4: Eliminate according to the determined support threshold.
  • Step 5: Identify new possible product pairs and calculate the Support value.
  • Step 6: Eliminate according to the determined support threshold.
  • Step 7: Final table

Eggs and beer are observed together in 40% of all purchases. 67% of customers who buy eggs also buy beer. Beer sales increase 1.11 times in egg purchases. According to the final table, these kinds of comments can be made.

Since we have examined the working logic of the Apriori Algorithm, it is time to make a Python project where we will implement Association Rule Learning with the Apriori Algorithm.

In this project, I will use the online_retail_II dataset that I used in Customer Segmentation with RFM and CLTV(Customer Lifetime Value) projects before. To remember the variables:

Varibales:

  • Invoice: Invoice number. Nominal. A 6-digit integral number uniquely assigned to each transaction. If this code starts with the letter ‘C’, it indicates a cancellation.
  • StockCode: Product (item) code. Nominal. A 5-digit integral number uniquely assigned to each distinct product.
  • Description: Product (item) name. Nominal.
  • Quantity: The quantities of each product (item) per transaction. Numeric.
  • InvoiceDate: Invoice date and time. Numeric. The day and time when a transaction was generated.
  • UnitPrice: Unit price. Numeric. Product price per unit in sterling (£).
  • CustomerID: Customer number. Nominal. A 5-digit integral number uniquely assigned to each customer.
  • Country: Country name. Nominal. The name of the country where a customer resides.

Operations and various manipulations related to the dataset have been done in the previous parts of the code, you can find the full code of the project here.

To apply ARL, first, you should prepare the ARL data structure. we need an Invoice-Item Matrix of 0s and 1s but before creating this matrix I am going to reduce the dataset to a single country. I chose France, you can choose another country and try it yourself.

Since we reduce the dataset to France, now we can create the Invoice-Item matrix. We can take both StockCode and Description as Item, but taking Description as Item causes it to take up a lot of memory and the code to run slowly, so it will be healthier to take StockCode as Item, not the product names.

A frequent itemset is a set of items that occur frequently together and reach a predefined level of support and confidence. Frequent itemsets can be found by using association rules.

antecedents : first product
consequents : second product
antecedent support : proportion of transactions that contains antecedent A
consequent support : proportion of transactions that contains consequent C
support : items’ frequency of occurrence
confidence : conditional probability of purchasing consequents Y
when antecedents X is purchased
lift : How many times the probability of purchasing consequents Y
increases when antecedents X is purchased
leverage : similar to lift but it gives priority to higher support.
conviction : expected frequency of antecedents X without consequent Y

After the rule table is created, sorting can be made according to the need (Support, Confidence, Lift, Leverage) and then the recommendation process can be performed.

As the last step of the project, we will make a product recommendation. For this, we will use a function that takes rules data frame, product ids and the number of recommendations to be made as parameters.

Suppose we recommend 2 products for the product with product id of 22492.

By using the following function we can check the product names from the id of the products we are going to recommend.

We have reached the end! Got questions, got stuck or just want to say hi? kindly use the comment box. 🦖

Stay tuned for Content-Based Filtering and Collaborative Filtering, which will be the next articles on recommendation systems. 🔔

--

--