Hello everyone and welcome to the K means clustering with Python lecturer and this lecture will show
you how to use sikat learn in Python to actually implement the K means clustering algorithm will also
show you how you can create artificial data with psychic learning capabilities.
Let's go and jump to Jupiter notebook to get started.
OK here I am at the Jupiter notebook.
Remember that k means clustering is an unsupervised learning algorithm and it tries to cluster data
based on their similarity.
What I'm going to go ahead and do to get started is import some visualization of libraries Seaborn is
Asinus important map plot lib pipe plots as Piazzi.
And since I'm using a different notebook map plot live in line setting.
Remember this unsupervised learning means that we're not actually trying to predict any sort of outcome
.
What we're trying to do is actually find patterns in the data in Kamins clustering.
We're going to specify the number of clusters we want the data to be grouped in.
What I'm going to go ahead and do now is show you how you can use sikat learn to generate some artificial
data we'll use artificial data for this lecture and in the next lecture for your portfolio project we'll
go ahead and switch back to using some real data.
But this is a useful thing to know when you want to just kind of player on of some algorithms.
You can say from S-K learn dot datasets imports and you can import a method called Make underscore Blob's
.
And if you go in and check out the documentation train for make underscore Blob's.
This basically generates blob like groups of data.
They you can go ahead and add cluster standard deviations to say how many features you want how many
centers and it will allow you to basically make artificial data that you can then play with.
Let me go ahead and use it to make some data so I'm going to say a number of samples will go ahead to
make 200 samples will state that the number of features is two sisters two features here.
Centers will say that there's four centers to these blobs and we'll go ahead and add a little bit of
cluster standard deviation to be 1.8.
And then finally I'm going to put in a random states so say random state is equal to 101 and you should
use the same or in that state I do.
So your Blob's are the same as mine.
Now that we've created this data we can go ahead and explore it a bit if we just look at data itself
.
You'll notice that it's actually a tuple.
And if you grab the first element in that tuple you'll have a PI array and this array is basically your
number of samples and then two columns of features so if we actually grab the shape of this it's 200
by two.
So this was the 200 samples and it has two features.
And within this this random data it will actually have four blobs in it.
Let's go ahead and plot out these blobs so we can get a better idea of what this is actually doing.
I'm going to say data zero and then grab all the rows in the first column and plot those against data
zero all the rows in the second column.
And if I just plot this I get this and I can see that there's seems to be two blobs here.
So it actually need to do is grab the second item in that tuple and if I look at that second item this
is actually the cluster that it belongs to.
So 0 1 2 and 3.
And that's from the center so there's four centers to these blobs.
So I'm going to say C is equal to data of 1.
And by default I'll show a black and white or a gray scale color mapping.
So I'm going to go in and say See map is equal to the string rainbow.
So it's a little more clear.
The four blobs here and this is how you can use sikat learn to create some artificial data for clustering
.
And this is what I actually use back in your earlier exercises where you had some artificial or fake
data.
I just used the make wavs method in order to do that.
So it's a pretty cool tool you can use to play around of some of these machine learning algorithms when
you don't want to deal if a real data set you can just make your own add cluster standard deviations
so make it harder for yourself to actually distinguish blobs.
But all of this for now I can see one blob is quite distinct and the other three are a little noisy
between each other.
We're going to go ahead and do now is show you that you can use sikat learns to create a k means clustering
algorithm will say from S-K learn dot cluster imports.
K means
remember Kamins is unsupervised wups there should be a capital M means.
And remember like I was saying Kamins is unsupervised and this algorithm going to just start off by
randomly assigning each observation to a cluster and then finds a centroid of each cluster.
Then the algorithm will iterate through the two steps.
Remember it reassigned data points to the cluster whose centroid is closest.
It calculates the new central for each cluster and then it just repeats that over and over again until
the cluster variation can't be reduced any further.
All right let's go ahead and show you then how we can use this k means I'll make a object called K means
lowercase K and lowercase M and then call k means and remember if k means I have to know the number
of clusters beforehand.
In this case I'm lucky.
I expect for clusters.
Since I created the data myself.
So I will go in and say and cluster is equal to 4 and then we're going to go ahead and k means fit it
to the features and the features are just data zero where we plotted earlier.
And now this report back to me.
Lots of methods and also attributes.
If I just want the centers of the clusters I can ask for those in or return back an array of the actual
sensors of the clusters.
So you can go ahead and plot those if you want but more importantly it will report back with labels
underscore the labels it believes to be true for the clusters.
Now something that's really important to keep in mind is if you are working of real data and you didn't
have the labels you would be done at this stage you would not be able to actually compare anything to
the real values.
But since we actually do know the labels from data ones if we take a look at data one again these are
the correct labels that we created for this artificial data.
And these are our predicted labels.
Now again k means clustering is unsupervised.
So you wouldn't use it to actually predict labels you would use it to try to find labels in your data
and you would do up a kind of exploring the number of clusters.
But let me show you by plotting this out how the Kamins compares to our original.
So I'm going to use Map plotted to do this I will say Fig.
And then x1 x2 is equal to peel t thought subplots one by two.
I'm going to specify Shehri is equal to true.
So they share axes.
And then finally I I to say Fix size is 10 by six and I'm going to state that x 1 and essentially just
using tuple and packing here from say X one the underscore title is k mean so we'll do the first one
is Kamins.
So I'll take x 1 and call scatter off of it.
We have data of 0
and we'll go ahead and plot the data again.
And all the rows in the second column and this color will be defined by Kamins thought.
Labels underscore and we'll go ahead and leave it at that.
I'm going to copy and paste this code on the second set of axes x 2.
This is going to be original.
So the original data.
Now we're going to go ahead and say instead of this color being by the labels the color will just be
like we did previously by one and then we're going to go ahead.
And so I see map here say See map is rainbow for both of them see map equals rainbow.
Let me break down what we're doing real quick before I actually run this.
I'm creating a subplot of one row by two columns.
Share equals true allows them to show the same axes and then using tuple and packing to actually grab
these two set of axes.
This first set of axes is going to plot my original data and it's going to color that data based off
of what my algorithm for the label should look like.
My other plot here x 2 again is going to plot the original data but it's going to color them by They're
actually factually correct.
Blob's assignments Let's go ahead and do this and see what we get.
All right.
So the coloring is a little different so you don't worry about that too much.
What we can do is kind of zoom in on some of these points and see how they differ from the original
.
So right here in this noisy section is where our algorithm wasn't able to really detect which group
belonged to which point or which point belong to which group.
So you can see here in the true dataset there's some noise here but essentially Kamins just decided
it can to deal with that noise and assign them all to the same centroid cluster there.
So you should note again that these colors are just meaningless and they only really referenced within
each other own plots.
So that doesn't mean that yellow is the same as yellow over here overhand the original it's just the
way of coloring stuff.
Let me go ahead and show you what would happen if we set Kamins number of clusters equal to two and
just ran all this again.
So if I just do shift enter all the way this would be a Kamins equal to 2.
So here's our original with the four clusters and this is what Kamins would think if you just had to
do this with two clusters at about three clusters.
Let's go ahead and see what k means we're think three appropriate clusters where and this is also what
I believe a human would kind of guess.
One cluster here is kind of larger cluster in this cluster and we can actually keep going and get some
let's say eight clusters so just doubled in a matter of clusters and you'll see here what Kamins thinks
the clusters are.
All right.
So keep in mind that k means is an unsupervised learning algorithm.
You would usually not have labelled data like we did.
We were lucky enough that we actually knew the labels.
That was data one that second array and the tuple and Kamins what it tries to do is actually give those
labels.
And here we're fortunate enough where we can actually compare in order to understand means more fully
.
All right.
Hope you enjoy that lecture.
Again it's a pretty simple process for using K means of sikat learn your say from S-K learn about cluster
import Kamins then instantiate k means of the number of clusters you expect or want to explore.
Fit it to your features your data again is unsupervised you're not going to have those labels and then
you can report back to cluster centers if you wanted them.
But more importantly you can report back the labels.
All right.
Hope you enjoyed that.
And as a little side note again maybe play around with the data equals make blobs and see what else
you can do with this particular notebook.
Thanks everyone and I'll see you at the next lecture or we will go over your portfolio project for Kamins
clustering