Hello and welcome to this art tutorial.
So in the previous tutorials we did the data pre-processing step.
And then we train our apriori model on the data set
with a minimum support of 0.030.
3% and a minimum confidence of 40%.
And now we did the job, and we are finally getting to the exciting step,
which is to visualize the results, that is, to look at the rules explicitly.
We're going to have the list of the strongest rules,
and we will eventually know how to place the products to optimize the sales.
Okay, so let's do it without waiting.
This will actually be very easy as well.
We will only need one line.
But before we write that line, let's jump back to the apriori algorithm.
Intuition.
Slide two. See the steps
we already did in the algorithm and what steps we need to do left?
Okay, so step one was to set the minimum support in confidence.
That was done here.
We set the minimum support here of 0.3 percent and the minimum confidence of 40%.
That's not our final word.
We might need to change the confidence again.
But we did the first step anyway.
And then the second step is to take all the subsets in transaction
having higher support than the minimum 0.3 percent support.
Okay.
So this step two was actually completed when we train the apriori on our data set.
So the function did it itself.
And that's the same for step three.
The step three is to take all the rules of these subsets
that have a higher support than 0.3 percent.
And so we need to take all the rules of these subsets
that have higher confidence than this minimum confidence of 40%.
And so again this was completed when we trained
the apriori model on a data set thanks to this apriori function.
Okay. So step three done.
And now we're finally getting to step four.
And that's the step that will lead us to the visualization of the results.
So this step four is to sort the rules by their decreasing lift.
As a rule explaining the intuition tutorial
to lift is the best metric to measure the relevance of a rule.
So that's why we're sorting the rules by their lift
rather than their confidence or their support.
So that's what we're going to do in this single line of code.
And to do this we're going to use the inspect function.
So we type here inspect.
And so in this inspect function
we can directly have a look at the rules by typing rules here.
And then you know if we want to have a look at the first ten rules
we need to specify here in brackets one colon ten.
So here we will get the first ten rules.
But that's not very interesting because this will just give us
the ten first rules found by our apriori model.
So that's not going to be the rules.
Having the highest lifts.
That is the most relevant rules.
So that's why right now we need to sort the rules
to get indeed the ten first rules that have the ten highest lifts.
So let's do this.
We need to add another function here, which is the sort function
which obviously is used to sort any table by a decreasing or increasing variable.
So actually the first argument of this sort function is rules
that contains all the rules found by our apriori model.
And then the second argument is by
which tells by what we want to sort the rules.
And of course we want to sort the rules by their lift.
So here we'll add by equals and then lift.
And we keep this one content here to get the ten first rules
that have the ten highest lifts.
All right.
So that's actually ready.
We are ready to visualize the result.
That is to clearly see what the rules are what the strongest rules are okay.
So let's execute this.
And here we go. All right.
That's the rules.
As I told you we get very explicit and clear rules.
The first rule is if people buy mineral water and holy pasta,
they will also buy olive oil in 40% of the cases.
Then the second rule is if people buy spaghetti in tomato sauce,
they will also buy ground beef and 48% of the cases.
Then if people buy French fries and herb and pepper, they will also buy
ground beef and 46% of the cases.
So that's how the rules work.
That's how they are presented thanks to this package.
And so these are the first ten rules with the ten highest lifts.
However do you see something that is not necessarily very relevant here.
Well if we have a better look at these rules we can notice
that some products are present in a set of items
not because they make a good association, but because they have a high support.
A good example of this is the chocolate here.
This set of items, composed of chocolate and herb and pepper, corresponds
to some customers that bought chocolate and herb and pepper in their basket.
And according to this rule, well, these customers
also buy ground beef in 44% of the cases.
But the thing is, they don't want to buy ground beef
because they had some chocolate in their basket in the first place.
Again, that doesn't make any sense, but it's not because the confidence is too low
this time, it's because the chocolate has a very high support.
We can see that here.
Chocolate is the fifth product most purchased by these French
customers in the south of France,
and therefore this product, chocolate here falls in a lot of baskets.
And especially in this basket number six here, composed of chocolate and herb
and pepper,
as well as this basket number seven here composed of chocolate,
mineral water and shrimp.
And by the way, in this same basket there is mineral water
that also falls in this eight basket here.
And that's because mineral water is the most purchased product in the store.
So of course it's falling in a lot of baskets.
So these products have high support.
However, this doesn't mean that we have to change the support now
because first we still want to validate our first business points, which was to
consider only the products that are bought at least 3 or 4 times a day.
But what might be helpful to change now is the confidence.
Because indeed, we asked for the rules that have at least a 40% confidence.
You can see that all the rules here have confidence over 40%.
And the reason the rules have high confidence
is because the rules are associated to the baskets
that contain the most purchased products in the store.
Because of course, if the baskets contain the most purchased products in the store,
well, these products will go together and the basket, and it won't be
because of some association rules related to this principle.
People who bought also.
But but it will be because
simply the baskets contain the most purchased products overall.
So in order to avoid this, the first idea would be to change the supports.
But we don't want to change our business related starting points, you know,
to consider the products, purchase at least 3 or 4 times a day.
So the other idea that remains left is, of course, to change to confidence.
Because if we reduce the confidence
now, well, we won't get these specific rules
that exist because they're
associated to the most purchased products that fall in the same baskets.
But we will get the most relevant rules that we are looking for
and that are related to this principle.
People who bought also bought.
So that's what we're going to do right now.
We're going to change the confidence will reduce it.
And what we're going to do is what we already did the first time.
You know, remember we had a confidence of 0.8.
That gave us no rule.
And then we divided it by two to obtain this confidence of 0.4.
That gave us these rules related to the most purchased products.
So now we're going to do the same and divide the confidence by two.
Again, to get a confidence of 20%.
And that should lead us to some more relevant rules
that are related to the association rules that we're looking for,
related to the principle.
People who bought also bought.
Okay, so let's do it.
Let's try these new rules.
Because by selecting this line and executing it,
the apriori model will be trained again.
And so we will get some new rules.
So let's do it. Execute.
And here it is okay first of all we get a lot more rules.
We get 1348 rules.
And that's expected of course because since we reduced
the confidence down to 0.2, well of course the algorithm found a lot more rules.
But don't worry, we will not have a look at the 1348 rules.
We will still look at the ten first rules with the ten highest lifts, and that's
what we're going to do right now by selecting this line and execute it.
And here are the new rules okay.
So let's have a look.
The first rule is if people buy mineral water and holy pasta
they will buy olive oil in 40% of the cases.
Okay.
So that's the rule
that makes a lot of sense even if there is still mineral water.
But, you know,
it's still a rule
that makes sense because, you know, this might be related to some people who
are looking to have some healthy meals with mineral water, whole wheat pasta,
and of course, olive oil is very healthy as well.
So these go well together and actually this is kind of a relevant rule.
Olive oil should be placed not too far from whole wheat pasta.
Okay.
Then the second rule is if people buy frozen vegetables,
milk, mineral water, then they will buy soup.
And 27% of the cases.
Again, this is a rule that actually makes sense.
Still, related to the need
of having healthy meals and milk can go quite well with soup.
Well, I know this is the case for French people.
French people do tend to put some milk in their soup.
And oh, by the way, speaking of a French tradition,
this is typically what French people love.
Absolutely. From us. Blown with honey.
So, for example, for those of you who don't know, it's
kind of some good cheese, I invite you to look at Wikipedia.
But anyway, this goes very well with honey.
You know, in a lot of French restaurants you will find for marshmallow with honey
in the dessert menu.
But, people also make this at their place.
So that's kind of a very good association rule,
even if for marshmallow is very different than honey.
Well, these two products associate very well.
And, you know, the direction counts here, because if we want to buy from us below,
then we want to buy honey.
Because honey goes very well onto from us below.
But if we buy honey, we don't necessarily want to buy from us below
because we don't want to have from us, not in our honey.
It's rather we want to have honey on our from us and not from us, but on our honey.
All right.
And then what do we have? We have.
If people buy spaghetti and tomato sauce, they want to buy ground beef.
Well, that makes a lot of sense.
That's of course, to make some spaghetti bolognese.
All right. So interesting. Quite classic.
You know, we don't need
a, machine learning algorithm to find out about this rule, but,
you know, it's actually what French people love associating in their basket.
So, of course, the ground beef shouldn't be too far from the spaghetti
and the tomato sauce.
And then this rule, if people buy light cream,
they will buy chicken in 29% of the cases.
So not necessarily obvious.
You know,
if the manager of the store had to place the product itself
without any algorithm, this manager would not have necessarily
thought to place the chicken next to the light cream.
And, you know, if we try to explain this rule, it's probably because,
you know, people who buy some light cream want to pay attention to what they eat.
And so since chicken is a lighter meat and maybe healthier meat than red meat
like ground beef, well, if they buy light cream and want to associate it
with some meat, well, they would rather choose chicken.
That's you know, if we try to explain the rules with some good sense
and then what do we have?
We have if people buy pasta, they will buy scallop in 37% of the cases.
Well why not? That goes well together.
Well, that's simply what
French people in the south of France love to eat in their meals.
And maybe that's also related to French taste
that French people associate like cream with chicken.
If we were in India, it would probably have been butter here
for, you know, butter chicken, which is a very good Indian meal.
Okay.
And then we have French fries and herb and pepper that go
very well with ground beef.
Of course, that's a classic French meal.
then cereal spaghetti, ground beef.
Okay.
That's a rule that doesn't make necessarily much sense.
And maybe it's due to the same logic that I was explaining earlier that is that,
you know, a lot of people buy cereals, that a lot of people buy spaghetti.
So spaghetti and cereals often fall in the same basket.
And since a lot of people associate spaghetti with ground beef, well,
we find this rule if people buy cereals and spaghetti, they will buy ground beef.
So let's be careful with this one.
Cereals is not necessarily associated to ground beef.
But you know we can investigate further okay.
And then we have these last two rules.
If people buy frozen vegetables mineral water and soup
they're likely to buy milk in 60% of the cases.
Well, this rule is probably related to two facts.
First, the fact that milk goes very well with soup for French people,
and also because, you know, all this looks very healthy.
So that goes quite well together.
And then the rest rules French fries and ground beef that go again
very well with herb and pepper. Yes.
Because of course
here we have French fries and herb and pepper that lead to ground beef.
Well of course an association rules the other direction can sometimes be true.
And that's exactly the case here.
Here we have
if people buy ground beef and French fries they will also buy herb and pepper.
That's kind of a triangle association rules here that we observe with the
three sides of the triangle being French fries, ground beef and herb and pepper.
So that's quite common,
but it's not always the case to observe the two directions okay.
So that's definitely very helpful for this tour.
Now thanks to these rules we can experience a new placement
of the products here.
We can also look at the 21st rules with the 21st lifts.
We won't do it now because we get the point here.
However, what we can do is you know, we actually try another support.
We tried three values for the confidence, but we only tried one
value of the support.
And remember we had this hypothesis.
I'm in this business starting point.
When we said that we wanted
to consider the products that are but at least 3 or 4 times a day.
Well, this support here, 0.03, is related to the business starting point
of considering products that are bought at least three times a day.
Then what if we consider now products that are bought at least four times a day?
So that's what we're going to do right now.
This is the only other support value that we're going to try.
So let's try this.
Remember the computation that leads to the support.
If we come to the products that are bought at least four times a day,
then on average these products will be bought
four times seven times a week.
And then in order to get the support, we need to divided
by the total number of transactions, which is 7005 hundreds.
All right. So let's see what we get.
We get a minimum support of
0.00 37.
So this time if we round it we get a minimum support of 0.04.
So let's try this 10.004 that corresponds to products
that were purchased at least four times a day instead of three previously.
So let's train the apriori model again to obtain the new rules.
So I'm going to execute this and we get the new rules.
And since we increase the support we find a smaller
number of rules 811 rules compared to previously.
You know, we had more than 1000 rules.
That's because we increased the support and kept the same 20% confidence.
Okay.
So now let's have a look at these new rules ordered by their decreasing lift
okay. So let's execute this.
And here are the new rules okay. Very interesting.
First by increasing the supports and therefore excluding some products
that have supports between 0.03 and 0.04.
Well remember this first rule.
If people buy light cream they will also buy chicken.
Well this rule now became the top rule.
That's definitely a great rule to consider.
And chicken should definitely be close to light cream.
Okay.
And then we have if people buy pasta they buy scallop.
Well we also had this rule before.
However we have a new rule here.
If people buy pasta they will also buy shrimp.
Yes of course this is the south of France.
People are close to the sea.
Well, either the Mediterranean Sea or the Atlantic Ocean.
And of course, people love associating shrimp with their pasta.
That's a very common and exquisite meal in the south of France.
And then what do we have?
We have if people buy eggs and ground beef,
they're likely to buy herb and pepper in 20% of the cases.
Well, of course, having pepper is very good on both eggs and ground beef.
And then we have the same kind of rules as before, except for this new rule here.
French people love to associate mushroom cream sauce with scallop.
So indeed it's very good meal.
Mushroom cream sauce goes very well with scallop.
Okay.
So interesting new rules that we obtain when we increase the support.
So the manager of the store now should, you know, experience
this new rules that we observe here by placing the new products of these rules
next to each other,
then experience for a few weeks and then observe the impact on the sales,
how much the sales increase, how much the revenue increases,
and then see if the business goals are achieved.
And if that's the case, try to strengthen the rules
or try some more powerful rules by changing the support in confidence or
on the other hand, if the business goals are not achieved, well, same.
We can try to get some new rules by increasing the confidence
and maybe the support.
Well, that's all related to experience.
That's what data analysts and managers do in retail stores,
whether there are online stores or grocery stores or any kind of stores.
Well, they use these association rules and update them.
And of course, combine these rules with other recommendation system
techniques like collaborative filtering with, you know, the user profiles
that can add some additional relevant info, and also other more advanced
techniques like the neighborhood model, the latent factor models.
Well, they combine a lot of models to increase the sales and the revenue.
but here you have a very good technique, very powerful technique,
association rules.
So congratulations for having implemented this first recommendation system
in some way.
And I really hope this will be useful for your business and your work.
And don't hesitate if you have any questions about this.
We will answer your questions very quickly.
So that was the apriori algorithm.
I was very happy to build this model with you.
Congratulations again.
Now in the next section we are going to implement the Eclat algorithm,
which is very close to the apriori algorithm but much more simple.
So that's another very good solution if you want to be very efficient
and don't have too much time to experience different values of the support
and the confidence, because indeed you will see that
there is no confidence parameter in the algorithm.
So I look
forward to building this new association rule model with you in the next section.
Until then, enjoy machine learning.