In today’s world of technology and eCommerce, we are used to having products recommended to us.
But how are these recommendations made?
How do these companies know what we might want to buy?
Don’t worry they haven’t been following you around listening in on your every word.
They are using recommendation algorithms!
One recommendation algorithm you can implement using python is the apriori algorithm.
The apriori algorithm is an association rule learning algorithm.
Recommendation algorithms are having a profound impact on the world of marketing, you can read about it here.
What is an association rule learning algorithm?
Before we dive on into the apriori algorithm, let’s first explain a little about what an association rule learning algorithm is.
A recommendation algorithm looks at the data from previous times where the scenario you are replicating has happened, for example when you have purchased a product.
For this example, we will use a new TV.
The algorithm will see everyone who has previously bought a TV and check what they also purchased. It will then look at the most common items, and make those as recommendations to you.
For example, people who purchased a new TV often also buy a TV wall mount. Based on this understanding, you might like to also?
This gives you your association rule and recommendation.
What is the apriori recommendation algorithm?
The apriori rule association algorithm works by looking at the percentage of the times where the same combinations of items occur.
It’s as simple as that.
It uses the data on the combinations to determine a list of rules for when items are likely to go together.
The way that it works is by calculating four parameters, the minimum support, the confidence, minimum lift and the number of items in the rule.
The support is calculated based on the number of times an item occurs.
The confidence is how confident you are that two things are associated.
The lift value of a rule is determined using the following calculation.
Lift = confidence / expected confidence
Good rules will have a lift >1.
You can learn the mathematics behind the apriori algorithm here and how to calculate these values.
The number of items is just the number of things you want in your rule.
So that you don’t end up with too many association rules from the apriori algorithm, you can add some parameters.
How do you determine how many association rules to create?
The algorithm will attempt to minimize the number of rules created by disregarding items that are deemed as low importance, or too common to be considered associated.
As an example, purchasing toilet rolls with an item. Toilet roll is very commonly purchased but may not be associated with the other items in the basket.
You can determine the number of association rules by altering the parameters of your algorithm.
These parameters are very easily updated when implementing the apriori algorithm in python.
To tailor the number of association rules, you create you can edit the support, confidence, minimum lift and the number of items.
Tuning these parameters makes it more efficient to compute your apriori algorithm using python.
However, the apriori algorithm cannot be used in all scenarios as it requires a lot of computational power.
As a result, the algorithm falls down on large datasets.
How do you Implement the Apriori Recommendation Algorithm using python?
To implement the apriori algorithm in python, you need to import the apyori module and apriori class.
Then to get the list of rules you merely call the apriori algorithm with the four parameters.
You also include a transactions argument at the start of the algorithm.
So now you are ready to start using python to implement association rule learning with the apriori algorithm!
I hope you have enjoyed this tutorial.
Before I go, I will leave you with some examples of how to use it!
- Supermarket design – where to place things to maximize sales
- Recommendation email marketing
- Recommendations on your website
- Creating bundles of products
That’s all for now!
Interested in other machine learning algorithms? Try this tutorial on classification algorithms.
Advertising Disclosure: I an affiliate of Udemy and may be compensated in exchange for clicking on the links posted on this website. I only advertise for course I have found valuable and think will help you too.
If you have found this content helpful, I recommend the course linked below which gave me a baseline understanding of the materials and python code shared here.