Hello and welcome to this art tutorial.
This tutorial is the fun tutorial
because we did our job by finding the optimal number of clusters.
Thanks to the dendrogram,
and by fitting the hierarchical clustering algorithm to our data x.
And now.
So time we will be visualizing our clusters using the class plot method.
However, we will not rewrite the whole code because
as you will see, we made the exact same code structure as we did for K-means.
So now what I'm going to do is I'm going to open our K-means code,
and I will show you that because we have the same code
structure between K-means and hierarchical clustering,
we just need to replace one tiny little thing to plot
our five clusters obtained by hierarchical clustering.
So let's go to files here.
Then let's click on these two dots here to go back to our part
three clustering folder to find our K-means clustering folder.
And in this K-means folder let's open the K-means r r file.
And now let's compare our two codes for hierarchical clustering
and K-means clustering.
So in step one, we both did exactly the same thing.
We imported our small data set.
Then in step two we looked for the optimal number of clusters
using two different methods the elbow method for K-means
and the dendrogram for hierarchical clustering.
Then in step three, we fitted our algorithm to our data X
and we created our vector of clusters y k means in K-means.
And you see in hierarchical clustering.
And now if we look
at the last step visualizing the clusters in the K-means algorithm, we can see that
the only thing in our code that is related to K-means is a y k K-means vector.
Here.
And we have the same vector of clusters in our hierarchical clustering code.
It's y h c.
So basically the only thing
that we have to do to plot our cluster is in hierarchical clustering
is to take our class plot function here with the same parameters.
Copy it
and paste it in our hierarchical clustering visualizing the clusters.
And then the only thing that we have to do is to replace
y k means by y axis.
And now it's ready.
Let's select our code section here.
Execute it.
And voila that's our hierarchical clustering results.
That's the end of this tutorial.
But the fun continues in the next tutorial,
where we'll be analyzing our results and explain what those clusters are.
I will also explain how you can reuse this code for any other business problem.
And this will conclude our our tutorials.
So I look forward to seeing you in the next tutorial.
And until then, enjoy machine learning.