Are you ready?
We're going to first add a title,
you know, with the title function by Matplotlib.pyplot.
And this time let's say, you know,
we're just going to choose a simple title like dendrogram.
All right.
Then let's add X label.
There you go X label.
And then in quotes.
Well you tell me what is going to be on the x axis.
Let's not fall into the trap of, you know, answering too quickly.
You know.
The x axis is neither the annual income or the spending score.
You know, it's none of the features.
The x axis in the dendrogram is actually
the customers, you know, the observation points.
Basically, the x axis are not the columns as I just suggested, but the rows.
Because remember in this matrix X or even this data set,
each row corresponds to a customer.
In this customer is actually an observation points.
And that's what is on the x axis of the dendrogram.
You have all the observation points, you know, just given by indexes from 1 to 200.
Because we have 200 customers in this data set.
And then on the y axis what will you have.
Well you will have the Euclidean distances between each pair of customers.
And then each pair of groups of customers where the groups get bigger and bigger.
And then by considering bigger and bigger groups, you get also
the Euclidean distances between two of these bigger groups.
Right. So that's how the dendrogram works.
It will become more clear once we visualize this.
So there you go.
I just wanted to get you to think on what is the X label and the Y label.
So now that we know well let's input here the X label which will be.
Therefore you can either call it observation points.
If you want to generalize this or if you want to stay in the context
of this case study, well we can call it because to Murs.
All right customers that's for the X label.
And now for the Y label.
Well that will be always the same.
You know, independently of whether we want to stay in that case or not.
That's going to be always the you Kelly d and distances.
All right.
That's always what is on the y axis in a dendrogram.
And finally we end up remember with PLT dot show
to indeed display the graph in the output.
All right.
So now let's check it out. Let's see that dendrogram.
And I will explain again what we have on the x axis and the y axis.
All right let's do this. Let's play the cell.
And we're about to get the dendrogram in a second.
There we go.
All right. So that's our beautiful dendrogram.
And as we said in the x axis we have
the customers listed from 1 to 200.
Because there are 200 customers in the data set.
And on the Y axis.
Well, we have indeed that as you can see, Euclidean
distances first between each pair of customers.
That's the little pairs you see here.
And then as you see when you link two customers, you know,
within the same group, well, this forms a group.
And then with the group next to it linking to other customers.
Well, you link these two groups within a new pair
and you compute the Euclidean distance between these two groups
by, you know, taking the root of the sum of the squared distances
between the customers inside these groups.
All right.
And you do this for then each pair of bigger groups as we see,
for example, the two biggest groups that we see on this dendrogram are first,
this one, you know, that first group containing lots of customers.
And the second group is this one, right, which was linking these two subgroups.
And then all these subset groups inside.
And so now the question is how do we figure out
that optimal number of clusters.
Well it's super simple.
And to show you this I will actually click these three dots here
to view the output in full screen.
But then that's still not enough.
What I would like to do is now save image as
all right
I'm going to call this image then draw Graham I'm going to save it.
And now we're going to go onto my desktop which is right here.
Yes I'm recording at night.
And here is the dendrogram.
So let's see if we can zoom better now.
Oops.
Let me let me just there you go.
Let me enlarge this.
All right.
Well it's not that great,
but it's good because what I wanted to do is this horizontal line.
That's exactly what I wanted to get, which I couldn't get on Colab.
And the reason why I wanted to get this
horizontal line is because it is what will help us figure out
the optimal number of clusters, because very simply, on the dendrogram,
the optimal number of clusters can be found.
Where you have the largest distance, you can move vertically
without
touching one of these horizontal bar, you know, by
horizontal bar this is a first horizontal bar.
Then this is the second horizontal bar, the third horizontal bar
fourth horizontal bar.
And then you know this one is the next one horizontal
bar, horizontal bar, horizontal bar, etc..
And the optimal number of clusters can be found
where you can move that horizontal line.
You know, the ones that I'm creating with my mouse.
And then I'm going to try to move
that vertical bar inside the dendrogram, you know, starting from the top.
And we'll see where I managed to move the most
vertically before meeting one of these horizontal bar.
So let's do this together.
And you know,
we will very easily find where we have the largest vertical move, let's say.
And then the optimal number of clusters will actually be the number
of vertical bars we have inside that vertical move.