Hello and welcome to this art tutorial.
So in the previous section, we implemented the apriori model
to find some relevant association rules that could help us find a more
strategic placement of some products in a grocery store in the South of France.
And therefore, thanks to these association rules, the manager of this store
is likely to optimize the sales and therefore increase the revenues.
And accordingly, we managed to create some added values
in this business thanks to association rule learning.
And today we are going to implement a new model of association
rule learning, which is called the E-Class model.
So the first thing I want to say is to prevent any disappointment you might have.
The E-Class model is actually very simple compared to what we did previously,
because the model is basically the apriori model,
simplified because previously we had two parameters.
We had the support and the confidence, and we also had the lift, by the way,
when we ranked our rules by their decreasing lifts.
But here in the E-Class
we will only have one parameter which will be the support parameter.
And therefore when we obtain our rules, that will not be the rules
that we obtained earlier.
Like people who bought this also, but that but we will just get some
different sets of products frequently, but together we will get
the different sets of products.
There are the most frequently purchased together, so that's what we'll get.
We have to expect this.
But you know, the exam model can be very useful if you don't have much time.
If you want to get some simple results,
if you don't want to play
with too many parameters like the support and the confidence here,
we won't even have to compute a value for the support.
And moreover, we won't even have to
choose a value for the confidence because there is no confidence parameter.
So you'll see
this is the way of using association rule learning in the simplest way.
So let's do it right now.
You'll see what I mean at the end of this tutorial.
So let's start with the very first step.
Setting the right folder is working directory.
So we'll go to our Machine Learning A-Z folder.
Part five Association Rule Learning.
And right now we are in a club.
So here it is.
We have the market basket optimization.
We will work on the same business problem.
And so we can click on this more button here.
And now set as working directory.
And you know since I told you the model is a simplified version
of the apriori model, what we'll do now is take our apriori model
and you'll see that it will almost be the same.
And we will just need to change one thing.
So I'm going to select everything from here
to here copy.
And I'm going to paste it in the model.
All right.
So here in this section data preprocessing.
We don't have anything to change.
We are just going to import the data set with the read transaction functions.
Be careful not with the red dot CSV file.
Because we still need to have our sparse matrix.
That will also be an input of the function,
like here for the apriori function.
So we will select this and execute.
And of course we get the same number of duplicates.
The CSV file hasn't changed.
We have five duplicates and no duplicates.
Just five duplicates okay.
Then of course
we can use the summary function to get some info about this data set.
But of course these are going to be the same enforce before.
So we have 7500 transactions
119 products, and the density is 0.03.
That means that the proportion of non-zero values in the matrix is 0.03 3%.
And then of course we have the most frequent items.
Mineral water comes first and eggs.
And we can actually have a better look at these
most frequent items by selecting this line and execute.
And of course we get the same frequency plot.
So that's exactly like before.
We don't have anything to change here.
However, now we enter the second code section which is to train
the model on the data set.
So first we will just replace apriori here by Eclat.
Here we go.
And guess what? It is so simple.
Here we have the apriori function to train the apriori model.
And guess what the function is going to be to train the model.
It's going to be a class. So very simple.
We are almost ready to train it on our data set.
But of course the economical is much more simple.
It doesn't include the confidence in the parameters.
If right now we get to the slide of the Eclat algorithm from the intuition
tutorial, well, we can see that the algorithm has three steps.
And the first step is to set a minimum support.
Remember before, for the apriori algorithm, the first step
was to set a minimum support and a minimum confidence.
Now we only need to set a minimum support so we don't need this.
Actually this parameter is only for the apriori model.
So if we keep that we will get an error.
So I'll just remove this and
we can leave the minimum support to 0.04.
But you will see that it's even not necessary here.
You'll see why at the end.
However we might need to add another parameter.
It's because since, you know, I mentioned that the algorithm
is just going to return the sets of items most frequently bought together.
Well, it wouldn't be interesting to have sets of only one item.
So in order to get some sets
of at least two items, we will add another parameter here,
which we actually encountered earlier and which is the Midlane parameter.
And we will set it to two, because we want to have the different sets
of at least two items most frequently purchased together.
Okay. And now we're ready.
See how it was? Simple.
So let's select this and train the model on our data set.
Done.
Okay.
So now things change a little bit.
First of course we see that we just trained the model obviously.
Then we have the parameter specification like before.
But this time we don't have the confidence parameter.
We have the support that we set to 0.04.
That is the minimum support.
And of course this time we have the middle and equals to two.
Remember before the million parameter was set to one.
But we didn't have any problem with that because all our rules contained
at least two products.
But when using class we need to set this Midlane parameter to two,
because otherwise we will get some sets of only one item.
Okay.
Then we have these other more advanced informations here.
And then there is something that I would like to highlight here.
It's the number of sets and not rules.
Remember before we had you know, let's say we had 845,
it was written 845 rules because we had the rules of the forum.
If people buy live cream,
then they're likely to buy chicken with a confidence of 40%.
That is, with 40% chance.
And here, since, you know, we don't get these kind of rules of this form
and we only get two sets of items, well, this time we have indeed 845 sets.
So even if Eclair is considered as a association
rule learning model, well this doesn't return rules.
This actually returns some sets.
Okay. So we're going to have a look at the sets.
Right now.
We are ready to move to the last step of our model.
Well we can actually jump back to the slide.
And as we can see the step two is to take all the subsets and transactions
having higher support than minimum support.
So that's what the function excluded itself.
And then the last step step three is to sort this subset by decreasing support.
So this time it's not by decreasing lift like for Priory
there is no lift in Aguila.
So this time we're going to sort the rules by decreasing
support.
All right.
And we're going to take the ten first rules with the ten highest support.
So we are actually ready.
Well the whole code is actually ready.
We did it very efficiently.
Well it was very simple.
So eventually let's have a look at the rules.
And you'll see that it's much more simple and honestly less
interesting than apriori.
But that's what we'll get.
So I'm going to select this line and execute.
And as I just told you, we simply get the different sets of items
most frequently purchased together.
So for example, the set of items most frequently purchased together
is mineral water and spaghetti with the support of 0.059.
And then it's chocolate, and then it's chocolate and mineral water,
then eggs and mineral water and that's, of course, strongly related to this
most frequently purchased products in this store.
So that's the éclair results, much less interesting than the apriori.
But that can be very useful if you're looking for some very simple information.
And by the way, since I was telling you, if we, you know, change the support
and set it to 0.03 like we did for the apriori model in the first place,
well, you'll see that
we'll get the same ranking here because as you can see, the supports
of these ten first sets of items most frequently purchase together,
all have supports higher than 0.04 or 0.03.
So indeed, if we retrain the model with the minimum support of 0.03,
and if we select that again and execute,
we get the same ranking with mineral water and spaghetti coming first
and then frozen vegetables, mineral water
as the 10th set of products most frequently purchased together.
All right.
So that was the Éclair model.
Again, if you want to have a serious analysis
about how to create some added value for your business,
optimize the sales and the revenue, you should go for apriori.
But if you are looking for some very simple informations like
the sets of products most frequently purchased together,
then you can go for it.
So congratulations!
Anyway, you now know how to implement two association rule learning models
the apriori and Eclat.
And you know what to use them for.
So thank you very much for watching these tutorials
and I look forward to seeing you in the next part.
Reinforcement learning.
Until then, enjoy machine learning.