All right, my friends, let's do this.
Let's use elbow method to find optimal number of clusters.
So we're going to use of course the excess which is the within cluster
sum of squares.
I will remind what this is.
But first let's create a new code cell.
To start this new step of the implementation.
All right. So what are we going to start with here.
Well we're going to find back a very good friend scikit learn.
Because we will actually implement that elbow method with a class of scikit
learn which guess what is called well K-means
because indeed the way we will implement the elbow method will actually be
by running the K-means algorithm with several number of clusters.
So you see, we're going to run the K-means algorithm several
times, each time with a different number of clusters.
And that's why
we have to call that K-means class that can run this algorithm already.
And so well, our first step here will be to start from scikit learn,
from which we're going to get access to the module that contains
that K-means class, and that module is called cluster.
Just like that.
And then from which we are going to import
that k means class perfect.
And now what do you think the next step is going to be?
Well exceptionally this time.
The next step
won't be to create an instance or you know, an object of this K-means class,
because we are about to start a for loop, which will run the K-means
algorithm with ten different numbers of clusters.
So very simply, we will run the K-means algorithm with one cluster,
then with two clusters, three clusters, etc.
up to ten clusters and therefore the way to do this is through a loop.
And we will do a for loop, because we knew exactly the different numbers
of clusters we want to try, which are from 1 to 10.
And each time we run the K-means algorithm, you know,
with these different numbers of clusters, well, we will compute.
Of course, you know, that famous metric in clustering,
which is, as I told you at the beginning, w
to within
cluster sum of squares, which I remind
is defined as the sum of the squared distances
between each observation point of the cluster.
And it's essentially the centroid of the cluster.
So we're going to compute that some of these squared distances.
And this is exactly what will be on the y axis in the graph of the elbow method.
You know remember the graph in the elbow method contains in the x axis.
Well the different numbers of clusters we will try from 1 to 10.
And in the y axis it contains the w axis computed
for each of these numbers of clusters.
And therefore.
Here what we have to do right before starting this for loop is to create a list
which will, through the for loop, be populated with the successive
w x values you know, for each of the numbers of clusters,
and therefore we're going to call that list w x,
which we will initialize as an empty list.
Remember that lists in Python are written in a pair of square brackets.
So here in this pair of square brackets we're going to add one by one to different
w x values for each of the numbers of clusters okay.
And now we can start the for loop.
So the way to write a for loop in Python is to start with four.
Then we choose the name of the iterated variable,
which you know will be incremented by one each time in each iteration of the loop.
And the classic name for that variable is I.
And then we add in range.
And here we specify in parentheses.
Well the values we want this index of the loop to take over the iterations.
And here that's very simple I will actually take the different values
of the numbers of clusters we want to try, which are from 1 to 10.
Include it.
But remember ranges in Python include the lower bound
but exclude the upper bound.
That's actually what we see here.
You know start defaults to zero okay.
So that's the default lower bound.
And stop is emitted right.
It is excluded.
So that's why I also really like Google Colab.
You have all the info in this little help window.
But I'm also here for the explanation.
So there you go.
The range we have to input here is from one,
you know, the first number of classes we will try
and then up to not ten but 11 because we want to include ten.
And therefore we have to go up to 11 which is excluded.
All right.
And then we add just a little colon just like that.
And then we start the for loop.
All right.
And now now can come the next natural step.
You know after we import this class to K-means class.
Because indeed now we can create our first K-means object.
Why do I say our first K-means object?
That's because, once again, you know, we were going to create
ten different K-means object for each of these numbers of clusters from 1 to 10.
So here we're creating the first K-means algorithm, which will be run
with therefore one cluster, because I here starts at one.
All right.
So let's create our first K-means object, which represents
exactly the K-means algorithm, which will be run to identify.
Well, actually some one cluster.
You see what I mean? And then I will be equal to two.
So that new K-means algorithm will be run to identify two clusters.
And then a new K-means algorithm will be run to identify three clusters, etc.
up to ten. Okay, there you go.
That's our first object, which we create by calling, of course, the K-means class.
Be careful with the capital letters K-means class.
We add some parenthesis and now we import the arguments.