Hello, my friends.
All right, here we are at the final, very exciting step
of this implementation where we visualize the results.
So we're going to clearly visualize the rules and see for each of them,
you know, their support, their confidence and their lift.
And mostly we're going to see which are going to be the best deals
by one product and get another one for free.
Will clearly see which food goes well with some other food
for these French people in the south of France.
Okay, so let's do this.
We already have the rules here, which were the output of this primary function.
So you will see that it will be very simple to display them.
Therefore let's select that cell in order to create a new code cell just below.
And indeed, in order to have a first look at these rules, well,
we simply need to create a new variable which we're going to call results.
You know, the results of this, a model which are nothing else than the rule.
But see what I'm going to do?
I'm going to set this result variable as a list of
the rules, which will just put these rules into a list.
Right? As simple as that.
We just want to have our rules into a list.
So that then when we display the results, you know, by
just calling this variable and executing this cell, indeed
we will get in the output all our rules in this list.
You recognize the square brackets here and at the end as well.
All right.
So all these rules are listed one by one, you know in different rows.
All right so there you go. These are all the rules.
You know all the rules following these criteria a minimum support
of 0.3 percent, a minimum confidence of 0.2, a minimum lift of three.
And of course
two products in the rules, you know, one on the left and one on the right.
All right.
So let's have a look at them one by one starting with the first one.
So that's the first rule here.
And what do we see.
Well I'm just going to you know highlight it.
All right. So that's the first rule.
All right. So let's see.
First we see that the two products in this rule are live cream and chicken.
But be careful the order is not.
If customers buy light cream then they will buy chicken.
No it's actually the other way around.
Because indeed if we scroll to the right well that's important.
You see, items base is equal to light cream and then items add
is equal to chicken.
So that means that actually the left hand side of the rule is light cream.
And the right hand side of the rule is chicken okay.
So that means that if people buy live cream
then they will have a high chance to buy chicken.
And that high chance is actually measured by the confidence, which is 0.19 here.
So that means that if customers buy light cream, then they will have
29% chance of buying chicken, which makes sense, right?
Well, at least in France, because usually French people love to put
some nice light cream with lemon as a sauce for their chicken.
Right? It's a common traditional French meal.
I actually really enjoyed myself.
All right.
And then we can also see the lift, which is very good. 14.
84 I remind that all our rules here will have lift larger than three.
And then where is the support?
The support is right here.
The support is 0.0 45, meaning that the rule containing
these two products appears in oh point 45% of the transactions.
All right then, let's see what we have.
So the second one is mushroom cream sauce.
But same the items base is mushroom cream sauce.
And the items at is zero, meaning that if customers buy
mushroom cream sauce then they will have a high chance of buying scallop.
And that high chance is 30%, right, 30%,
and the lift is 3.793.8
and the support is 0.05.
Okay. Good.
So you see you have all these rules. They look great.
But we would actually appreciate a nice way to have a look at them
because here you see we have to do these scrolls to the right and left.
And it's a bit overwhelming.
So don't worry I actually found a good code snippet online.
I think it was from StackOverflow or another source like that.
So we're going to use that code snippet to actually put the results
well organized into a pandas dataframe.
We won't have to re-implement that code snippet from scratch, because, you know,
it is very specific to this association rule learning implementation.
And therefore you will just see that once in your life.
So it's not essential.
And therefore we will just copy and paste it here from our original implementation.
But of course I will explain to you the code.
So let's get it first.
It's actually at the bottom and it is this one.
All right.
We make an inspect function which will return the rules.
Meaning these rules will organized into a pandas dataframe.
And the cool thing about this is that we will even be able
to sort the rules by a descending metric, right?
Because if we notice here, well, they're not sorted.
You know, the lift store at 4.84 then goes down to 3.79
and then goes back up to 4.7 and here 5.16.
And remember that the lift is actually the most relevant
metric to measure the strength of a rule, you know.
So if we want to store the rule, you know, to get the strongest one, if we want to,
you know, select a few deals, well, we would rather do it with the lift.
So what will be really nice here.
You know, when putting these results into a well organized pandas dataframe is that
then we will be able to sort easily the rules by the lift or another metric.
All right. So let's get this function.
I'm going to copy it here
and we will paste it inside our implementation right here.
There we go.
Paste.
And now let me explain to you how it works.
So as we can see it is a function that takes as input the results meaning
these results, the rules as they are right now, you know, organized this way.
And then it will take separately the left hand
side of the rule, meaning the product at the left hand side of the rule,
then the product at the right hand side of the rule,
then the support of all the rules, then the confidences,
the lift of these rules,
and then it will return all the rules with the left hand sides
and the right hand side.
And there are supports, confidences and lifts inside the list.
Right. That's why we have this list function here again.
All right. So that's what this inspect function will do.
And then at the end we create the final pandas dataframe
which takes as input the output of this inspect function.
And besides we add the column names.
You know the first column will be the left hand side of the rule.
The second column will be the right hand side of the rule.
The third column will be the support for the confidence.
And finally the lift.
So we will have a super nice table with these columns
and giving all the important informations for each of the rules.
Okay, so that will be super practical.
And now let me explain how you know we get each of these elements,
you know the left hand side and then the right hand side.
All right.
So the first thing to notice is that for loop inside a single
row indeed we take the full list of all the rules here.
You know that's the full list of results.
And then for each of the rule in this full list of rules,
well we're going to access each of these elements and take them separately.
And so let's start with left hand side.
How do we take that left hand side which is you know right here
the item space frozen set light cream basically light cream.
Well let's see.
So first we take the single rule.
You know let's say for example this one to explain
how we're going to get these elements.
So we take these rules.
And then in this rule we first access the index to.
All right. So let's access it step by step.
This element here you know up to the comma
is the element of index zero okay.
Then this element is the element of index one.
This is the second element.
And then this full element you know up to the closing
square brackets here is actually the element of index two.
All right.
So all I've highlighted here is the element of index two.
So now in that element of index two then we're going to access
the element of index zero meaning the first element.
And the first element
is actually the first element of that list, you know in square brackets.
And that is of course all this, you know, ordered statistic up to be careful,
you know, up to the closing parenthesis here, meaning right here.
So all this is the first element inside that,
you know, big element which I've just highlighted before,
you know, from ordered statistics up to actually the end.
All right.
So this is the element of index zero.
And then in this new element of index zero
we're going to access the first element you know of index zero again.
And well you know we have to look inside this parenthesis and that
of course this element which is indeed the left hand side right.
Because this is what we want to get.
Left hand side item space. That's the same.
And that is light cream.
And then for the right hand side that's the same.
You know we took the same first element of the next two.
Then that whole element of index
zero inside this first element of the next two.
And then instead of taking the index zero here we take the index one,
which is that element items add frozen set chicken.
All right.
So that element is you know that element of index two then index zero
then next one.
And then that index is to indeed catch what's inside, you know chicken.
And same for that index zero here there was two catch light cream.
All right.
So you see we just play with the indexes to access what we want.
But really you will only have to do that once in your life.
So don't worry too much about this kind of overwhelming code, even if it's quite
funny to, you know, follow the indexes path to find the products.
But then the good news is that for the support, it's going to be much simpler.
Indeed, we just need to take the full rule.
You know, the single rule.
So you know, this whole rule.
And then very simply we access the element of index one directly.
So remember and this full single rule.
Well all this is the element of index zero.
And then all this is elements of index one.
And that's exactly the support which is what we want.
And we do this for each of the rules in this whole list of rules okay
then confidences.
Once again it's a bit more complicated.
But just like before first we get the element of index two.
So let's take it again.
This is the element of index zero.
Then this is the element of the next one.
And then all this is the element of index two okay.
Then inside this whole element of index two well we're going to access
the element of index zero which is let's take it again.
So we start from here you know ordered statistic.
And then we go up to actually the closing
parenthesis of here which should be right here.
Right. Let's check the parenthesis here.
So that's the first opening one then the second opening one.
Closing here.
Then this is a new opening one, the closing here.
And then there you go okay.
So that's the closing parenthesis of indeed this element of index two.
Then index zero. And then inside this element.
Now we're going to get access to the element of index two.
So that's the element of index zero.
Then that's the element of index one.
And then finally that's the element of index two.
And that's exactly the confidence which is what we want to get right now.
And then same for the lift.
So same same path.
First we get access to the index two then zero.
So just like we were right now.
But then instead of taking the index two like for the confidence
we take the next one which is the index three
and which corresponds to of course, the lift.
All right. And that's how we get the lift.
So you see the idea just again with the indexes,
we access the elements inside this whole complicated list of rules.
But at least we will get a nice result in the end.
And really you don't have to worry too much about this code.
It is very unique, very specific, and you will probably not have to
re-implement it again in your life.
You can just take it, you know, I just took it
online and indeed it is very practical for what we're doing right now.
Meaning association rule learning.
Okay.
So good.
Now let's have a look at the beautiful result.
So we are going to play the self first to,
you know build that inspect function and then create that new data frame
which I called results in data frame and which will contain the same rules
here, but better organized and more beautifully organized, let's say.
And so now we're going to create a new code cell here
just before this text cell to first display the results non sorted.
And to do this well we simply need to call what we created here meaning results
in data frame.
All right.
And then when pressing play you will see that
we will get a very nice table containing all the rules
with the different elements and separate columns.
So that's the left hand side of the rule, the right hand side of the rule.
And then the support of the rule,
the confidence of the rule and the lift of the rule.
So let's have a look at the rules one by one for the first one.
As we saw earlier, if customers buy light cream,
then they will have actually 29% chance of buying chicken.
All right.
And this rule appears
0.4 percent of the time, you know, and 0.4 percent of the transactions.
And it has left a 4.84, which is indeed very good.
Then the second rule, if people buy mushroom cream sauce,
then they have a good chance of buying scallop
and that good chance is 30% and it has a lift of 3.79.
Pretty good still.
Then if people buy pasta, then they will have a good chance of buying a scallop.
And that good chance is actually pretty good indeed.
If customers buy pasta, then they will have 30%
chance of buying a scallop and the lift is 4.7.
Then check this out.
We have a good lived here for from us the honey.
Yes of course from us non honey is a delicious French dessert.
I actually really enjoyed that.
Dessert is one of my favorites.
And indeed when you buy from us as a French person,
you will definitely enjoy putting some honey on it.
All right, so indeed, that's a great rule.
And I would definitely make this deal, you know, buy from us below
and get honey for free, right?
Because indeed the lift is 5.6.
So very very good then.
Herb and pepper, ground beef.
Yes of course French people or even, you know, other people
from other nationalities love to put herb and pepper on ground beef.
This is actually exquisite.
And indeed we have a good confidence for that rule and actually, well,
not that high of olives.
Okay then.
Same for tomato sauce.
It's nice to put tomato sauce on ground beef.
The lift is still close to for light cream, olive oil.
Well I don't really see the association, but why not?
I'm not really a good cook, but maybe there is a recipe
where we can combine olive oil and like cream.
I'm not so sure about that.
But however, this one is
definitely something I would do whenever I buy whole wheat pasta.
I also get olive oil and indeed the lift is pretty high.
And then pasta and shrimps.
Yes, that's a very good specialty of south of France, especially near the coast.
French people, of course, love mixing bass and shrimps all right.
And the lift is 4.5 so very good.
So you see, these are much better to observe.
But then we still don't have them salted which here is fine because you know
we don't have that many rules.
But in other situations you can end up with many more rules
and therefore you would like to sort them,
for example by descending lift, because the lift is a very relevant measure.
So I'm going to show you right now how to do this.
It's very, very simple thanks to blenders.
So let's create a new cocktail.
And here we're going to take again.
Well our new beautiful data frame results in data frame.
And now we're going to use a pre-built function by the library
to indeed sort the results of this data frame
by a specific column, which we are of course going to specify here.
All right.
So let's do this.
To do this we need to add a dot here
because we're going to call a specific method of the data frame class.
You know because this results in data
frame is actually an object of the data frame class by pandas.
And that method is called and largest.
There we go this one.
And inside we just have to enter three arguments.
As we can see here.
You know you have all the info thanks to Google Collab.
So the three parameters are first n which is the number of rows we want to return.
So I'll just put that as you know, if I was doing a code template
I'll just put n equals ten to you know in general return the ten best rules.
So n equals ten then keep we will keep the default values.
You know that's in case we have duplicates.
But that's fine for us I actually check them and then columns.
All right.
So that's the other parameter I want to input here.
And that's of course
you know to specify by which column you want to have your results sorted.
All right.
So columns equals and
well you know the most relevant metric is perhaps to lift.
So we're going to sort these rules by the lift.
And to do this well we simply need to specify
the lift value in quotes for that columns parameter.
So in quotes I'm going to enter here lift.
All right.
And that's it.
This is simply what you have to do in order to sort your rules by the column.
You want.
Here.
Lift if you want to order them by confidence which is also great.
You would just specify here in quotes confidence.
All right. So let's try.
Let's play the cell and there we go.
Now we have indeed the rules sorted by the lift.
This is the highest lift 5.16 down
to the lowest lift 3.11.
And so let's see.
Well as I told you,
you know that's a very popular association especially for French people.
They love to put honey in from each below.
Each time you go to a French restaurant, you will see in the dessert menu, well,
from us with honey.
And even if you don't see it, you can always ask the restaurants.
They always have honey, not too far.
So that's definitely a very strong rule here.
And you know, if I was the owner of the shop, I would definitely make that too.
You buy from us and get honey for free then same.
This is the second strongest rule light cream chicken.
So another great deal would be
if you buy light cream, you will get chicken for free.
And that's actually a very good deal.
But you know, as I told you, the owner can put some smart prices that you know
would amount to a good price for, you know, buying the two.
Right.
But that's another story here.
We're doing association rule not pricing strategy.
So there we go. Let's move on to the next rule.
If people buy pasta then they will enjoy buying scallop as well.
So a third great deal would be buy pasta and get scallop for free.
And then same with pasta and shrimp.
Of course if you bypass so you can get shrimps for free,
then if you buy whole wheat pasta you can get olive oil for free.
You know these deals will be very attractive to the customers
because indeed customers buy these association of products.
Okay. All right.
So there you go.
These rules, you know for most of them actually make sense.
And there you go my friends. Congratulations.
You built your first association rule learning model to identify clear rules
that will create some added value to a retail business.
So your job is done and now you can enjoy the beautiful days of South of France
after completing successfully this new data science mission.