Hello and welcome to this art tutorial.
So in the previous tutorial, we plotted our dendrogram
to find the optimal number of clusters and the result was five clusters.
So now in this new step, since we have this
optimal number of clusters, we are going to fit the hierarchical
clustering algorithm to our data X with obviously five clusters.
Okay.
So the first thing that we need to do is to create an object of the H class class.
And actually this is something that we already did.
Because if we go back to step two here when we wrote dendrogram
equals h class, then the distance matrix and the word method
we actually created an object of the H class class.
And we're going to do the exact same thing right now
we're going to create the exact same object from the H class class.
So I'm going to copy this line of code,
paste it here in step three.
And I'm just going to change the name of the object
just to make things nice and clear.
So let's replace here dendrogram by HC.
And now we have the same object as before only with a different name.
And this actually makes sense because by building a dendrogram
we actually do all the steps of hierarchical clustering.
And among all these steps there is the step in which
the algorithm found five clusters.
So that means that this object contains the information
when we have five clusters.
And so now let's use this object to build our vector of clusters.
That is the vector that will tell us which cluster each customer belongs to.
And to build this vector we're going to use one of the method
of the H class class which is the cut tree method.
So we're going to call our vector of clusters y underscore hc equals.
Then we use the country method of the h class class.
So let's type cut tree and then press f1.
And then here
we see that this method requires three arguments.
The first argument is tree.
And of course it's our dendrogram that we just renamed HC.
Then the second argument is k the number of clusters.
So here we input five
and we will leave the default value for the third parameter.
Okay.
Now it's interesting to take a step back because actually this method is called
cut tree which is a very well chosen name because indeed we are cutting the tree
to take the part of the tree that contains our five clusters.
So that makes pretty much sense.
And now, since we are actually done with fitting hierarchical clustering
to our data set, let's select this code section here and execute
to find our y hc vector of clusters that appears right here.
And now if we
type in the console y hc here and press enter, we have all the clusters
that the hierarchical clustering algorithm assigned to each customer.
So for example customer number one belongs to cluster one.
And customer number two belongs to cluster two.
Customer number 106 belongs to cluster three.
And our last customer belongs to cluster four.
Okay.
So congratulations we found the right number of clusters.
And we fit it correctly to hierarchical clustering to our model data set.
And now time for fun. In the next tutorial
we will be visualizing a hierarchical clustering results.