All right, my friends.
Are you ready to.
Train your primary model on the whole.
Data set?
Well, I bet you are.
Because, indeed, we actually did. The most.
Difficult. Parts. You know, the most. Difficult part was to.
Make that transaction's list containing all the different transactions of our.
Data set.
You know, to 7501 transactions.
We all.
Put them into this transactions list with strings for.
Each of the products.
And this transactions list.
Will be the input of the primary.
Function, which we will use right now to train the primary model on this data set.
And so that's what I'm talking about.
Most of.
The job. Is done because now the only thing that we.
Have to do is just call this primary.
Function from this a. Binary package.
Which we installed in the first cell and call that function with some relevant.
Values of the. Parameters.
And that will be most of the.
Reflection will have when building and training this.
Apriori. Model. All right. Are you ready?
Let's do this.
Let's create a new code cell.
So the first thing to do will be to import effectively that a primary function.
Because so far. Make sure to understand that we.
Only installed the primary package but we.
Haven't imported anything.
The only libraries we import were numpy, matplotlib and pandas.
So now we need.
Indeed to upload this a primary.
Function. And this primary function belongs.
To the binary package which we installed first.
Therefore we need to.
Start. From.
That a binary package.
There you go from.
Which we import.
That a primary function. All right.
There we go again a primary.
And now now we can call that function.
All right.
So first thing to understand is that this function will.
Actually return the rules.
You know not only will train the primary model on the data.
Set, but also at.
The same time this function will indeed train
this primary model and in the end return the final rules,
you know, with the different supports, confidences and lifts.
And therefore, since we're now.
Ready to call. This function, and since.
This function returns the rules, well, let's create a new variable here
which we're simply. Going to. Call rules.
All right.
And that will be the output of that function.
And speaking of that function well let's.
Call it. Right now. A priori.
That's the function.
Therefore I'm adding some parentheses.
And now there we go.
Let's see what parameters we have to input.
All right.
So this function takes as input some very intuitive arguments.
We could actually you know almost guess all of them.
The first one is of course.
Well the data sets. You know, the. Data set.
On which you're going to train your. A priori. Model.
And the name for that parameter is transactions.
Actually, you know. Because the. Primary model is.
Mostly used to.
Compute some correlations and association. Rules.
Among transactions.
So that's actually the name of the parameter.
And of course the value.
For that parameter must. Be well that.
Same transactions.
List which we created.
In the right format.
Right before this tutorial. In the.
Data preprocessing phase.
Okay. So this is the name of the parameter.
And this is the name of our transactions.
List which is the value of that parameter.
Okay. Good. So that's for the first argument.
That was an obvious one.
And now.
According to you what would.
Be the next argument.
Well, the next argument has to do.
With the support.
Of course, it's
not going to be a simple support because we have a support for each rule.
But what we can set is actually.
A minimum support, you know, in order not to compute.
All the rules, but only the rules that have at least.
Some certain relevance.
And therefore we will.
Set a minimum support. Value.
In order to take not.
All the rules, but only the rules that have a support higher.
Than this minimum. Support.
All right. So let's first. Enter the name of the.
Parameter here, which.
Is min underscore.
Support. Right.
And now.
According to you what should we choose.
As a minimum support. Here.
Well this has to do of course with our situation.
You know the problem itself and of course some common sense.
So let's recap.
We have in total 7501 transactions.
That were recorded over. One full week.
And among these 7501 transactions, we want to get the most relevant rules.
You know, the were, you know, the.
Strongest rules of two.
Elements, you know, with one element of the left hand side of the rule,
you know, one product and one element in the right hand side of the.
Rule, which is another product.
And we want. Therefore, these products to.
Appear a minimum amount of time.
And that's exactly what the support is about.
Remember the support.
Of a couple of products A and B.
Is the number of transactions containing.
These two.
Products A and B.
Divided by the total.
Number of transactions.
So we need to see here.
You know, for.
A couple of products A and B.
How many tons per. Week.
We need to have at. Least these two products.
In the transactions.
Well you know. Let's. Do some common sense.
Let's say that each day we would like to consider
the products that appear in at least three.
Transactions in the day.
All right.
Three transactions in a day.
Because all the products that appear.
In only one transaction or two transactions, you know,
are actually not frequent, and we would not build some strong rules.
Out of these products.
So our common sense here.
Is to. Only consider the.
Products that appear at least.
Three times a day.
And therefore.
Since the 7501 transactions.
Were recorded. During the full. Week.
Well, we need to multiply this number of three transactions.
Per day by.
Seven in order to get, you know, that minimum number of times.
We want to see these products in the transactions per week.
And therefore that number of times is three times seven equals 21.
And since the. Support is.
The number of times the. Products.
Appear in the transactions divided by the total number of transactions.
Well. The minimum. Support.
Considering that we want to see minimum three times.
Per day, the products must be.
Three times seven.
Divided by. 7000 and 501.
All right.
So that's purely.
Based on common sense.
You could choose another minimum support.
But there you go. That's the minimum support.
That goes well with our scenario.
You know with our business. Case study.
And therefore what I'm simply going to do now.
Is just open a new tab here to quickly compute.
Well, three as. In minimum three times one.
To see the products appear. In the transactions per. Day.
Then times seven.
Because. The 7501 transactions are.
Recorded over a week.
And therefore, when calculating the support
and dividing by the total number of transactions,
the numerator and the denominator
must be in the same unit of time, which is one week, and then divided.
By seven.
Thousand 501.
Total. Transactions.
There's just press.
Enter.
We'll get the result which is oh point.
Oh 27.
And we can round that up to 0.03.
And 0.03 will be exactly our minimum support.
So I'm going to close this.
And I'm going to enter.
Here 0.003.
Perfect. So that's for. Our minimum support.
Now the next argument what do you think.
It's going to be.
Well you probably guess that this.
Time we're going to choose a minimum confidence.
Right. A minimum confidence.
All right. So this time what. Should we set.
As the minimum confidence.
Should we use. Again common sense. Or should we.
Try some different values.
Well this time we won't do the same kind of calculation as we did for the support.
This time I'm rather going to give you some rule of sums.
You know, which you can try when doing association rule learning.
So I.
Know from the other packages, you know the one from.
R, because there is actually.
A great function in. Order to do association. Learning.
And it. Has indeed a default value.
For the minimum confidence. Which is 0.8.
So what I actually. Did, you know, for this problem.
Is to start first with 0.8, but this was way too high
because 0.88 would require the rule to be correct 80.
Percent of the time.
And therefore I ended up with actually no rule.
So I had to reduce the confidence.
So I. Divided by two so.
That I can try a minimum confidence of 0.4.
But still I got very few rules.
And so I divided by two again.
And this.
With 0.2, I actually got. Some. Great rules.
You know, not too much, not too. Few, but a. Dozen of them.
So that was a good choice.
And that's.
How I chose. This minimum.
Confidence. Therefore here for this.
Minimum confidence we will. Set it equal to oh.
Point two.
Okay. Once again no rule of thumb.
You can try different values depending on your business requirements.
All right.
Then next.
Parameter I'm sure you guessed it as well.
That is this time the minimum lift.
You know that other.
Metric which.
Measures the quality of a rule or the relevance of a rule?
And so now.
According to you, what would be a good.
Minimum lift?
Well, that.
Kind of decision to make, you know, you give them with experience.
You will see through the.
Many. Association rule.
Learning models that you're going to build on your data.
Sets, that generally a good lift is at least three, you know, 3456.
7789. You know, these are.
Good lifts, but lift below three.
Make the rules not that relevant.
And therefore this is kind.
Of a rule of thumb that I'm giving you here.
It is not based on common sense, rather based on experience.
And therefore I recommend.
To choose. A minimum lift. Of three.
All right.
And with this minimum lift we. Will get good rules.
You know relevant rules.
Okay. So minimum lift equals three.
And then we have two less.
Arguments that are. Actually very.
Very important and in fact.
Compulsory for our.
Business problem.
It has to do with the fact.
That, you know, we want to identify.
The best deals of by.
One product A, and get another.
Product B for free, and therefore the rules we.
Want to get in the end must have.
Only two products, one.
Product in the left hand side of the rule and one product in the right hand
side of. The rule.
All right. And therefore to make sure.
We have this, you know.
One product A on the left and one. For a B on the right.
Well, we need to add two more arguments here.
Which is. First min length.
Okay. And then max.
Length
where of.
Course min length is the minimum number of elements you want to have.
In your rule.
You know, left or right.
And max length is the maximum number of elements you want to.
Have in your rule, left or right.
And therefore here to make sure.
We only have two.
In our rule, you know, one on the left and one on the.
Right.
Well, obviously we need to set mid-length.
To two.
And max length same.
To two.
And that's only because in our business problem we want to find.
These best deals of.
Buying one product A and get.
One product for free.
Therefore our rules must have exactly two elements.
Then imagine you wanted.
To find the best deals of buy two products and get a third one for free.
Then you would set min lengths to. Three.
And max length. To three.
And if you want to be very, very.
Flexible on your deals, like you know, you.
Could have deals with by one product A.
And get one product B. For free or by two products.
A and.
Get one product B for free.
Or, you know, by ten products and get one for. Free.
Well, in this. Case you would.
Set min lengths to two and then max length.
To 11.
Okay, so that really. Depends on your. Business.
Requirements, your business problem.
Here we.
Just want to find the best. Deals of two products.
By one product A, get. One product B for free and.
That's it.
And that's why we set min links to two and max links to two.
So that our. Rule can.
Have only two products.
All right. And that's it.
Well you know we're done.
With this a priori.
Function which will return the.
Rules respecting all these values.
We said for the parameters.
A minimum support.
Of 0.03, which. Means that.
The. Products in the rules.
Appear at least 0.3 percent of. The time.
Then the minimum confidence, which means that.
For each product A.
In the left hand side.
Of the rules, well, we will have product B in the.
Right hand side.
Of the rule at least 20% of the.
Time, and then we have a minimum lift of. Three.
And we have only two products.
In our rules thanks to this min max equals to and max length.
Equals two.
All right. So are you ready.
Are you ready to run that cell to get the rules.
We won't have them displayed in the output.
But don't worry we will visualize.
Them right after this tutorial in the last. Part.
So let's do this.
Let's play the cell. And there we go.
Now my friends, we have the rules.