Hello and welcome to this art tutorial.
So in the previous tutorial we did the data pre-processing step.
We first imported the data set the usual way with the recursive function,
but then we explained that we needed to create a sparse matrix containing
all the transactions that occurred in the store during the whole week,
and to build the sparse matrix we used to read that transaction function,
including the arm duplicates argument to remove all the duplicates.
And this sparse matrix
is exactly what we need to train our apriori model on the data set.
And that's what we are going to do in this tutorial.
We're going to make the rules if I can say that okay.
So now thanks to the rules package, the training is going to be very simple
because we're just going to use one function, which is by the way
called the apriori function with only two arguments.
So let's do it.
We're going to create a new variable and we will call it rules.
Because this variable will contain in some way the different rules
of our business problem.
So rules here and then equals.
And that's where we use the apriori function with the different arguments.
And so we're going to input two arguments.
The first argument is going to be our data set.
And the second argument is going to be the parameter argument that will contain
the choice of a minimum support and a choice of a confidence support.
So let's have a look at these arguments.
I'm going to press F1 here.
And here I have some info about the primary function.
So as you can see the first argument is data.
So let's input it.
Now that's the easiest argument to input.
So data equals data set.
And then comma.
And then the second argument.
So the second argument is parameter.
And as it's written here the parameter is an object of class a parameter.
And so this object will contain the minimum support
that we will set ourselves and a minimum confidence.
And we can also specify a maximum of items you want to have in the rules.
So that's given with Max then.
And actually it's also possible to include a minimum specifying
the minimum number of products you want to have in your rules,
but we won't actually need that.
And what we will need inevitably is the support and the confidence.
So let's input that so parameter here.
And we need to include the support and the confidence in this parameter
the following way we're going to take the list function.
And in this list we're going to input the support and the confidence.
So I'm going to add the two arguments here.
And then we'll see what value we will input for these two arguments.
All right.
So let's take the slide of the intuition tutorial about the apriori algorithm.
And let's see what the different steps of this algorithm are.
So the first step as you can see is to set a minimum support and confidence.
So that's exactly what we are about to do right now.
We are in the first step of the apriori algorithm.
And that consists of choosing a support and a confidence.
So the choice of a support and confidence is not a general rule.
We cannot express to support all the confidence with an explicit equation.
It actually depends on the business problem itself.
It actually depends on your goals.
The goal related to your business problem.
It also depends on your data
set, the number of observations you have, the number of items.
So that depends on different circumstances that don't allow us
to make some general rule about how to calculate the support and the confidence.
But don't worry,
it will make a lot of sense when we explain
how we calculate the support and the confidence here.
And you will be able to apply it to your business problem.
Okay, so let's start with the support.
The support of a set of items AI is equal
to the number of transactions containing this set of items.
I divided by the total number of transactions.
And the support argument that we're inputting here
is actually the minimum support you want to have in your rules.
That means that the items that are going to appear in your rules
will have a higher support than this minimum support here.
And same for the confidence.
So what we must ask ourselves is that what supports do we want to have
of our different items in the rules so that the rules are relevant?
Because, for example, if we go back to the plot here with
actually the 100 observations, which is this one, if we zoom on it, well,
we can see that we have a lot of products that are not purchased very frequently.
And these specific products are the products with small supports
because a few transactions contain these products here.
So when you divide the number of transactions containing these products
by the total number of transactions, then you will get a small support.
And you know, since these products are not purchased very often,
they're not very relevant
for our optimization problem because, you know, we want to optimize the sales,
but what we want to optimize overall is the revenue.
And since the revenue is a linear combination of the different numbers
of products where the coefficients are actually the prices of these products,
well, in order to optimize the revenue, we would need to optimize the sales
of these products here that are purchased very often,
rather than these products here that are less purchased.
And so what we need to choose here is a support
that will only include the products
on the left of this vertical bar here that will correspond
to the minimum support.
So for example, let's say the value here on the y axis is 0.05.
Then that means that all the products on the left of this vertical
bar will have a support higher than 0.05.
And so if we set a minimum support of 0.05, then the rules will only contain
the different products of this left side of the vertical bar here.
And so now how to choose to support.
Well we need to look at the products that are purchased rather frequently.
Like at least 3 or 4 times a day.
Again that depends on your business goal.
But what's for sure is that if we managed to find some strong rules about items
that are bought, at least 3 or 4 times a day,
then by associating them and placing them together, customers
will be more likely to put them in their basket
and therefore more of these products will be purchased
and therefore the sales will increase.
So that will be the starting point of how we are going to set the minimum support.
We are going to consider the product
that are purchased at least 3 or 4 times a day.
And then we will look at the rules.
And of course,
if we're not convinced by the rules, we will change this value of the support.
That's how we work with the apriori model.
You know,
we try different values of the support, different values of the confidence
until we are satisfied with the rules and until we think it makes sense.
And, you know, we can also try these rules within a certain period of time.
And then we look at the impact on the revenue.
And if we don't observe a meaningful increase in the sales revenue,
we can later change the support and the confidence to change the rules
and then experience again
until we find the strongest rules that optimize the sales.
So that's actually what happened in real life.
But of course, in these tutorials, we're going to try
with products purchased 3 or 4 times a day.
And so we'll see what happens okay.
So actually we didn't set the support yet.
We just decided that we will
look at the products that are purchased at least 3 or 4 times a day.
But that will quickly lead us to the support.
Because if a product is bought, let's say three times a day,
that means it's purchased three times seven equals 21 times a week.
And since the support is the number of transactions
contained in this product, over the total number of transactions,
and since there are 7500 transactions, then we get the minimum support
that is equal to seven times three over 7005 hundred.
So let me explain that by writing this here.
So okay, we said we considered the products
that are purchased three times a day.
So that's three here.
Then since the total number of transactions where register over a week,
that means that if we consider the products
that are purchased three times a day, that means that a purchase
on average three times seven times a week.
So this three times seven equals 21.
Here is the number of transactions containing
this product bought three times a day over the whole week.
And now we need to divide by the total number
of transactions to get this minimum support.
And the total number of transactions is actually 7005 hundred.
And this value here that we're about to compute is nothing else
than the support of a product that is purchased three times a day.
And, you know,
we want our rules to consider only the products that are at least purchased
three times a day.
So all the products of our rules will have a higher support
than this support here that we're about to compute.
So let's compute it.
Let's find out what it is.
And that's the value we will give to this support parameter here.
So right now I just need to press enter.
And that's the value 0.00 28.
We will round this to 0.03.
And so that is the minimum support
of the product that will be considered by our rules.
So let's input it 0.003 right okay.
So that's it for the support.
Now the second step of our step one is to set a minimum confidence.
So the choice of the confidence still depends
on the business problem but mostly on your business goals.
So what we'll do now is we're not going to compute a confidence
like we computed the support.
So we are going to start with the default value and then decrease
the confidence step by step until we get some relevant rules.
Because you know the confidence is kind of an arbitrary choice.
We don't want to have a too
high confidence because if we get it too high confidence,
we will get to obvious rules, you know, rules that we don't need
a machine learning algorithm to understand where we need to place the products
next to each other.
And we shouldn't have a too small confidence,
because if we get a too small confidence, we will get some nonsense rules.
Like, like, you know, if I'm buying chocolate, I want to buy shampoo.
That's a nonsense rule that doesn't make any sense.
And that's the kind of rule we will get if we set it to small confidence.
So we will start with the default value, which is actually open eight.
I think we will have a look at it.
We can go back to help here to look at the description.
And if we want to have the info about these two arguments here, support
and confidence.
What we need to do is click on this API parameter here, which is the class.
And here we go that gives you the information
of the parameter arguments of apriori and class.
The other model that will make after this section.
And as you can see we get informations
about the support and confidence and other arguments.
So these are actually the arguments that are both in apriori and eclat.
And below you have some additional arguments
that are only for apriori, because you'll see that the Eclair algorithm doesn't
have a confidence in its algorithm and only consider this support.
We will see that afterwards.
But right now what we're interested in is actually the confidence.
And you can see that the default value is 0.8.
So that's what we will start with.
I'm not saying we will get some interesting results.
You can already imagine what we're going to get
because 0.8 is a very high confidence.
Try to guess what we'll get with this high confidence 0.8.
And don't worry, we will divide it by two to try some smaller
confidences until we get some relevant rules.
Okay, so that's actually ready with the single line
of code containing only these two parameters, the data set
and this parameter here with the minimum support and the minimum confidence
we train our primary model on our data set.
So let's select this line and execute.
And here we go.
Our apriori model is created.
And by the way the rules are also created.
So let's have a look at the info here.
All right. So that's the apriori model.
And here we have the default parameters of this parameter argument here.
So we can see that we have the minimum confidence here 0.8.
And the minimum support 0.03.
And we also have the midlane of the basket.
That means that the basket that the rules will consider will contain
at least one product.
Well, we could have actually set two here to have at least two products in the row.
We'll see if we get a problem with that.
But so far the most important arguments we need to input are the support
and the confidence.
Okay.
So the algorithmic control that's not very important for us.
Now that's kind of a little more advanced.
And here we get some other interesting informations.
The most important information we need to look at here is the number of rules.
We can actually see zero rules here.
That means that when we trained our apriori model here,
this model actually found zero rule.
And can you guess why.
Well of course it's due to the choice of our minimum confidence.
Because by setting this minimum confidence 0.8, that means that all the rules
made by our apriori algorithm have a confidence higher than 0.8.
And what does that mean?
That means that each rule should be correct
at least on 80% of the transactions.
So 80% is a lot.
That means that the rule must be true at least four times out of five.
And that's why the apriori found zero rule with a minimum confidence of 0.8,
because no rule is true at least four transactions out of five.
So that's what I was telling you.
We can start with the default value, but since we have a lot of transactions
and a lot of products that the customers can purchase, well,
of course we need to set smaller confidence.
So we will divided by two.
So we will try now. 0.4.
And now let's see what we get.
So let's re-execute this line.
It will retrain the model on a data set
and recreate some new rules. Here we go.
Now we have 281 rules.
Much better. So that's a relief.
And now of course what we're going to do is look at the rules themselves.
We're going to visually see what the rules are.
So we're going to see exactly
which products should be placed next to each other.
And we will see the strongest association rules.
We will see what product customers purchase if they buy another product.
So we will see all this very explicitly.
And that's what we are going to do in the next tutorial.
So I look forward to discovering these rules with you.
And until then, enjoy machine learning.