We will study in continuation of our Machine Learning's previous tutorial.
So, let's see.
In today's tutorial we are seeing K means Clustering, meaning grouping and unsupervised approach.
So, we are going to practically see this first algorithm K means Clustering.
Unsupervised that means we are going to have just x with us as an input data.
And y will not be there.
So, without y we will have to find out as to how in this input x groups will be formed.
So, let's begin,
So, we will follow the usual steps, but there will be some slight changes, which I will highlight and you will be able to understand.
And accordingly in this flow we will proceed ahead and see other algorithms also.
So, here first I will start with ,
From sklearn dot cluster, so I will first connect this, so that it will give me suggestions,
So, from sklearn dot cluster import K (it should be in capital), m e a n s (k means),
Now, second I will show you a demo,
Import numpy as np,
So, let's execute this, and
Now, I will create a sample x,
Is equal to np dot a r r a y(array), and here I will pass a list.
So again as I had earlier also passed so in the same way, I will pass in one list 2 elements, here I passed 1 and 2.
And here I will pass 1 and 4.
Next I will pass 1 and 0, so I passed 1 and 0
Next item is ofn10, 2, so here .. I passed 10 and 2.
Next item that we have after this.. we will put two more items, so we will put 10 and 4.
And last one more item we will add, and then conclude this array.
So I will put one more in the list,and then I will pass this, so I am putting 10 and 0.
So, this is basically my x that is formed.
Now, I don't need y, because you know that it is unsupervised, so here there is no supervisor, output or label.
Now, here I will directly create a model for Kmeans.
So to create the K -means model, I will need this package here, and here I need the data as to how many clusters I have to form, so here by looking at the data I am creating only 2 clusters because this is a demo example.
And along with that what is going to be the random state, that also I will mention, so we will keep the random state as zero.
So, in this way my model is created.
Now, in this model through the dot fit method, I will pass the data, now with this input data only the model is learning.
After learning, now from here I will also make predictions.
So, k means dot.. now you see without any label also it can give the prediction, so here I will pass any two values.
…. So I will take it like this.
So, the first value that I will pass will be of 0 comma 0.
And the second value that I will pass will take it to the higher end, so we will take 12 comma 3.
So, it has to give prediction for this,
So you can see it has predicted one and zero.. so here it has mentioned that this is in the second cluster, and the above one is in the first cluster.
So, how has it done this, If we have to find that.? Then it is through centroid that we had understood conceptually, but here I will show you.
K means dot, here you can see the cluster centres, these are your cluster centres.
10 and 2 ; is one cluster center.
And, 1 and 2 is the other cluster center.
So, whichever point is close to a specific cluster centre..here you can see zero and zero, is close to this, therefore we got one, 12 and 3 is close to 10 and 2 , therefore it got zero.
So, wherever your datapoint is close to whichever cluster centre, then they will form a group.
So, in this way it worked on the initial data, and organise them in groups, through calculating a centroid value or centre value in iterative fashion,
So, this is how k means algorithm is implemented.
So, let's see with the help of an example also,
So, here I will use sklearn dot datasets to import load iris, so we loaded an iris dataset here.
Now, here in x comma y in this we will use load iris, so load underscore iris.., and here we will use the return function and grab the values.
So, I just want x and don't need y, so here in k mean,, here I will take k means1 is equal to k means and here I will put n clusters is equal to 3, because I know it, therefore I am putting 3, otherwise it is also a way to guess or assume as to What we want?
So k means1 dot fit, and here we will fit x.
So we fit x over here, and after fitting x we will do predictions for this x only,
So, here we will give prediction,
So y pred is equal to k means1 dot here predict, and in predict again we will pass x,
So, here in y pred you can see it has given us predictions; so these are our predictions, Ok!
Now, you can see this through plotting also, as to whether it's done correctly or not.
So, I will also plot and show it to you.
So, from matplotlib import pyplot as PLT.
So, plt dot, here we will use scatter plot, so plt dot scatter, and in scatter here,
So, let's treat x first..
So, this is our x,
So, as x is an array so here we want all the rows but columns we want zero, in this way we will use it. Ok!
So, we will perform upon two values.
So, scatter here in x I put this, and in y I will put the first column, and after this c which is the colouring scheme, so c is equal to here first I will take y, and after this we will perform the second plot.
So, here we will do the second plot also, PLT dot scatter, and here again we will use the same.. features, and c,that is, colouring in that we are keeping y pred.
So, there can be differences in overall colouring, so you can see here. Those that were yellow, became greenish here.
So, the above ones are looking perfect, but here in this you can see there is so much overlapping, Right! So here in this yellowish it's all overlapping.
So, these are misgroups, that means they have come out from their actual groups.
So, in this way we can visualise also and see how many of them are matching and how many are not.
So, friends let's conclude here for today, we will stop this session here, and it's further parts we will cover in Next session.
So, practice this,
Keep Learning and keep watching, now we will move towards other algorithms.
If you have any queries or comments, click the discussion button below the video and post there. This way, you will be able to connect to fellow learners and discuss the course. Also, Our Team will try to solve your query.
good learning but the content titles are jumbled up, like first title of this module is decision tree dichotomiser which is practical part ahead of theory part. Same with the SVM practical 1 title has
Isakki Alias Devi P
yes, i am happy to learning for machine learning in LearnVern.it i s easily understanding for Beginners.
Superb and amazing 😍🤩 enjoyable experience.
Muhammad Nazam Maqbool
Absolutely good course... will suggest it to everyone. has superb content that is covered in a fantastic way.
super course and easily understanding and Good explaned
Ruturaj Nivas Patil
Very well explained in entire course. Great course for everyone as it takes from scratch to advance level.