Hello, my friends.
All right. Are you ready for that
last tutorial of the hierarchical clustering implementation?
Here we go.
We now have the dendrogram, which gave us the optimal number of clusters,
which turned out to be five.
And so now we're going to build, train and run the hierarchical clustering
to indeed identify five clusters.
All right so let's do this.
Let's create a new code cell here to build train
and run this hierarchical clustering model.
All right.
So remember to build our dendrogram.
We actually used the site by library because it contains
this dendrogram function which directly returned the dendrogram which was perfect.
But now in order to build the hierarchy clustering model with five clusters,
well we're going to go back to our best friend scikit learn.
Because indeed scikit learn contains.
If you remember the clustering module which contains the agglomerative
clustering class, which is exactly, you know, the classic version
of hierarchical clustering, the one that you studied in the intuition lectures.
All right.
So we're going to start from scikit learn.
There we go from which we're going to get access to cluster
the cluster module from which we're going to import
that aglow mirror Tiff clustering class.
Perfect. Thank you so much Google Colab.
All right.
Now the next natural step as usual as most of the time,
is to create an object or an instance of this class.
And we're going to call it h c because this object will be nothing else.
Then the hierarchical
clustering model itself, you know, with all its algorithm inside.
So h c.
And therefore now we're going to call the class to indeed
create an instance of this class.
And then adding some parentheses.
And now let's see what we have to input.
All right.
So can you guess the first parameter.
It's pretty obvious it's actually the same as in the K-means class.
The first parameter is the number of clusters
we want to identify in our data set.
And we know that it's five.
But you know I'm very curious about that.
Three you know that number three is the other option
of the optimal number of clusters.
So, you know, we'll try that at the end we will see what we get.
But let's start first with ten clusters equals five.
All right five clusters.
And now we need to add two more arguments.
The second one is affinity
which is simply the type of distance that will be computed
in order to measure the variance within your clusters.
Because then you're going to see that we will use again this word method
which corresponds to the minimization of the variance within your clusters.
So for affinity here we're going to choose well the
you CLI d and distance.
And that last parameter that we need to add is of course that method.
But this time the name of the parameter is not method it is directly linkage.
All right.
And so linkage here should be equal.
You know there are several options.
But the one we recommend is the ward method
which corresponds to the minimum variance method.
All right.
And that's it.
So now we have our hierarchical clustering model.
But of course it is not yet trained or fitted to the data set.
And so that's exactly our next step here.
But remember that at the same time we want to create this dependent variable
which contains for each customer or the future class they will belong to,
you know, the future cluster they will belong to.
And therefore instead of only using the fit method, which you know,
usually trains your machine learning models on your data
set, well, we're going to use the fit predict method,
which will not only train your clustering model on your data set,
but also will create at the same time this dependent variable
containing for each of the customers the cluster they belong to.
All right.
And speaking of this future created dependent variable,
well we're going to introduce here
a new variable which we're going to call y underscore HC.
And this is exactly you know that
dependent variable you see here with the five clusters.
All right so y h c and let's go back that
y hc variable well will be equal to
what is returned by this fit predict method.
Not only training the hierarchical clustering model on the data set,
but also returning the clusters to which each customer belongs to.
All right.
And therefore what we have to do here is just take RHC object
because that's from this object.
That's we have to call this fit underscore predict method.
And inside we of course input x just x.
Right. Because we just need to connect.
We just need to fit RHC object a hierarchical clustering model
to the data set, which is exactly x.
But only you know.
Remember containing the two last features the annual income and spending score.
All right.
So exactly the same as with K-means okay.
And that's it.
Once again thanks to our best friend scikit learn.
Well, in only three lines of code, we build, train and run
the hierarchical clustering model to identify five clusters.
So let's do this.
Let's run this cell.
And done.
We have our model and it is already trained.
So now let's actually do a little print to see
you know that created dependent variable y h c.
And let's play the cell and we'll see what we.