Hello my
friends, and welcome to this new practical activity where we're
going to build together this time the hierarchical clustering algorithm.
And now we're all going to go into part for clustering
to this time tackle the hierarchical clustering model.
And we're going to start with Python as usual we're going to work
on the same data set mode customers right where each row corresponds to a customer.
And for each of these customers world of mode gathered some info
like the customer ID to join the age, the annual income, and the spending score.
And we're actually going to work only with these two features.
The annual income and the spending score to identify these clusters,
but this time with hierarchical clustering.
All right.
So executives same and therefore let's proceed directly to the implementation
which you can open with either Google Collaboratory as we're about to do.
Or if you don't like Google Colaboratory, you can open it with Jupyter Notebook.
And for Google Colaboratory lovers will follow me here
to open this implementation in Google Colab.
And there we go.
So that's the hierarchical clustering implementation.
As you can see, it follows the exact same structure as K-means.
We first import the libraries.
We then import the data set exactly the same way as how we did for K-means.
You know, we select these two columns of index three and four, which corresponds,
of course, to the annual salary and the spending score.
So executives same we want re-implement this together.
And then this time instead of using the elbow method,
well we're going to use the dendrogram to find the optimal number of clusters.
And I will explain not only the implementation.
You know, we will re-implement this together.
And also I will explain how to find that
optimal number of clusters in this graph.
And finally we will train the hierarchical
clustering model on the data set using the agglomerative clustering class.
And finally we will visualize the clusters
exactly the same way as what we did with K-means.
And actually here the code is exactly the same.
The only thing that changes is the name of the dependent variable which we create.
Right?
Because still with hierarchical clustering we're going to create
that dependent variable.
But this time, instead of calling it why K-means.
As we did for K-means, we're calling it simply y HC.
And therefore here it's exactly the same code with only
that different name for that created dependent variable.
So we want to re-implement this either we'll just keep the code
and therefore there we go.
We are only going to re-implement two cells, which is just one,
to build the dendrogram and figure out that optimal number of clusters,
and to build a hierarchical clustering model and train it on the whole data set.
Are you ready?
Let's do this.
And in order to do this, we have to create a copy of this notebook.
Because this is in read only mode.
And therefore we're going to go to file here.
And then click save Copy and Drive to indeed create
a copy of this notebook.
Perfect. All right so there we go.
That's our copy.
Now we can modify it.
Now we can re-implement it.
But as we've just said we won't re-implement everything.
We will just re-implement these two cells here.
First the dendrogram how to build it and how to read it.
And then of course, well,
how to build the hierarchical clustering model on the data set.
And then we keep the other cells because they're exactly the same as in K-means.
And if you want let's just,
you know, remove this so that we don't see the final result.
And perfect now all right.
All you see here is executive same as with K-means.
The only thing that will change
are these two cells which we will re-implement together.
Okay. Perfect.
So first step let's just execute these two first cells here.
And to do this we need of course to upload the data set.
So let's click this folder here.
Now it is connecting to a runtime to enable file browsing.
You know in your computer in your machine.
And in a second we should see the upload button.
There we go. Upload.
And and well I'm already in the K-means folder.
But let me show you again the whole path.
So that's the folder you were given at the beginning of each section,
including this one.
Hierarchical clustering, which you could download on your machine.
So I hope you have it right now.
Otherwise you would just need to go back to the previous article.
And now we're all going to go inside.
Then we're going to go to part four.
Of course
then section 25 hierarchical clustering, then Python and then there we go.
Mode customers dot CSV.
This will upload the data set into the notebook.
And so now we can run these two first cells first importing the libraries.
And now that we have pandas we can import that data set
which at the same time creates this matrix
of two features containing only.
Let's see in the data set containing only the annual income and the spending score.
In other words, X is just these two columns here with all the rows okay.
All right. So data preprocessing phase done.
Now we can focus on the heart of the hierarchical clustering model
which is first to build the dendrogram to indeed find
the optimal number of clusters.
And of course
the optimal number of clusters that will result from this dendrogram
will be the same number as the one we found with K-means, meaning five clusters.
But I will explain how to read the dendrogram in order to indeed end up
with an optimal number of five clusters.
All right, so that was the introduction.
And as you know, I like to take it step by step.
So we will implement that next step of building the dendrogram
in the next tutorial.
So get ready.
And until then enjoy machine learning.