Hello my friends, and welcome back to the final step
of this implementation of the upper confidence bound algorithm.
We actually already implemented the algorithm itself in this cell
implementing UCB.
And now we can just, you know, sit comfortably in our chair and visualize
the results.
Because indeed implementing this final set will be super easy.
We just have to plot a histogram and make a nice title to the chart.
And also some labels for the x axis and the y axis.
So let's do this.
Let's create a new code cell.
Here I'm going to scroll down a bit.
All right.
And there we go.
Let's plot that histogram.
So just to make sure we all understand this histogram will plot
for each of the ad.
Well the number of times it was selected.
So on the x axis we will have the different ads.
You know from not 1 to 10 but you know 0 to 9
because we work with indexes and they start from zero.
And on the y axis will have the number of times each of these ads was selected.
All right.
So the good news now is that, you know, we already have the two.
We need to plot such a histogram which is matplotlib dot pi plot.
And for this we gave it a shortcut name plt.
So that's what we're going to start with plt calling matplotlib.pyplot
and from which we're going to call a specific function
from this pyplot module, which is the hist function.
And as you might guess well this this function can plot any histogram.
All right. So let's add some parenthesis.
And now according to you what do we have to plot in this hist function.
Well that's actually easy.
You know if we scroll down here you can see the parameters.
The only parameter that we'll need is this one x.
And this is supposed to be, you know, either an array or a sequence of arrays.
Basically it's going to be a list of values
which will be the values on the y axis of the histogram.
Right.
And therefore what we simply need to input here in our histogram
is, you know, this variable add selected which would create it,
which contains indeed the sequence of add that were selected over the round.
So keep in mind add selected is a list of 10,000 elements
where each element is the add that was selected at a particular round.
You know, the nth element of this add selected list is the add
that was selected at round in.
You know, shown to the user n. All right.
And this is exactly what is supposed to be the input of this hist function.
So let's just paste it inside.
And there we go. Basically here the job is done.
This will plot the histogram.
However of course we want to make it nice and to make it nice.
Well you know we will just add a title to this plot and we will choose,
you know, in quotes this title to be histogram
of Add selections.
All right.
Then we're going to add a nice label to the x axis.
And the function for this remember is x label same as the plot function actually.
So x label and in parentheses well we'll give and quotes
a nice label to the x axis.
And since the x axis corresponds to the different add
you know from 0 to 9 because of the Python indexes.
Well we'll set this x label as at okay
then same for the y label.
And in parenthesis we will enter in quotes.
Well this time well let's make it very clear.
You know let's just input number of times
each add was selected.
All right.
So that it is 100% clear.
And finally remember we have to end with PLT dot show
to display the graph in the output of the notebook okay.
So we're done.
Do you understand what is expected here.
Right in the histogram it is expected, you know if the UCB algorithm
was powerful, it is expected that one of the ads was selected.
The most you know, was selected way more than the others
because the goal of the UCB algorithm is to quickly,
you know, is to as fast as possible identify the ad that has the highest CTR.
And once it is identified, well, only this ad will be selected.
Okay, so we should see one ad that was selected way more than the others.
In other words,
we should see one bar in the histogram that is way higher than the others.
Are you ready?
Let's play the cell.
And there we go. We got what we want.
Indeed, this ad of index four here,
meaning the ad number five was clearly the ad selected.
The most, therefore was clearly the ad with the highest click through rate.
And in terms of our business case study, it corresponds indeed to the ad
that is the most attractive meaning, the ad that has the most beautiful image
of that car that will sell the most to the user as a future customers.
Okay, so good job.
The UCB algorithm did a very good job here.
However, remember that you know the goal is in fact to, you know, identify
this ad as fast as possible, you know, in a minimum number of rounds.
And therefore what we should experiment right now, you know, it's not quite over.
We should experiment to see in actually how many rounds the UCB
algorithm was able to identify this ad with the highest CTR.
And the way to check this is by you know,
changing that value of n here
because this algorithm was run with 10,000 rounds.
But what if we put instead, you know, 5000 rounds?
You know, we would like to see if the UCB is still able
to identify that ad of index four.
You know, with the high CR in 5000 rounds.
So that's exactly what we're going to check now.
But you know, rerun everything.
And the way to do this is by clicking runtime here and then restart and run.
and we're going to see if with 5000 rounds,
well that UCB algorithm is also able
to quickly figure out the best ad.
And yes, very good.
Even with 5000 rounds, you know, with 5000 users,
the UCB was able to identify the ad with the highest CTR.
And now let's make it even more challenging to the UCB.
Let's replace 5000 here by 1000.
And let's click runtime again and restart and run all.
And let's see if with only 1000 rounds well the UCB is able to.
Wow. Okay.
So still it was still able to do it.
You know identifying the ad with the highest CTR which is still
of course ad of index four, but quite just,
you know, so now I of course want to try with 500 rounds.
Right.
We're going to replace that 1000 here by 500.
Then rerun everything by clicking restart and run all.
And now let's see. But I'm not sure. Right.
Let's see if it is still able to identify that ad with the highest CTR.
And that's what I'm talking about.
You know, 500 rounds is not enough for the UCB algorithm
to identify that best ad.
You know, that ad with the highest CTR,
because indeed the ad with the highest, it is clearly ad number four.
But in 500 rounds, the UCB identified the best ad as AD of index seven.
So 500 is not enough.
And so now it will be very interesting to see with the Thompson
sampling algorithm, if you know it can beat the UCB algorithm
in the sense that it can find this ad of index four with the highest CTR
in 500 rounds, because the UCB can clearly find it in 1000 rounds.
That's for sure, but not in 500 rounds.
So we're going to see with the Thompson sampling algorithm
if this one is able to find that best ad, you know,
because we're going to work on the same data set.
Of course, if it is going to identify that best ad of index 4 in 500 rounds,
and if yes, we will even try lower numbers of rounds.
So I can't wait to now implement Thompson something with you.
We will work on the exact same data set, so we will just compare.
So let's do this in the next section.
And until then enjoy machine learning.