Lab8 K Mean Clustering
Lab8 K Mean Clustering
Lab Report # 8
Name: Hammad Ali
Zaryab Ali Haider
Registration No: 21-cs-039
21-cs-119
CODE:
data = 2 + rand(100,2) * (1000 - 2);
K = 3;
initialCentroids = datasample(data, K);
Output:
TASK
Apply k-mean clustering algorithm on the given below data matrix:
Data = [
1 150 15.4 50400200 18
2 144 11.3 42100650 15
3 120 9.9 39440420 12
4 110 12.5 36500520 16
5 100 9.7 40650005 10]
CODE
data = [
, 150, 15.4, 50400200, 18;
,144, 11.3, 42100650, 15;
, 120 9.9 39440420 12;
, 110 12.5 36500520 16;
, 100 9.7 40650005 10
];
K = 2;
initialCentroids = datasample(data, K);
maxIterations = 100;
TASK
Analyze the working of k-mean clustering for above task 1and take k= 4,
k=8, k=12 and k=20 for 100 and 500 iterations.
CODE
data = 2 + rand(100,2) * (1000 - 2);
K = 20;
initialCentroids = datasample(data, K);
maxIterations = 500;
CONCLUSION
In this lab we learned how to create a k mean clustering algoritham and use it. K represents
the number of centroids which is generated randomly from the dataset. Max iterations are
the maximum iterations for this algoritham. Maximum iteration depends on the data rows
of our data. If there is no difference between current iteration assignment of clusters, we
break the loop. In task 3 , there wont be any difference if we change the iteration from 100
to 500 because the data is only of 100 rows, it will produce better clustering if we have
greater data size. Higher number of k in this case will give us good result as there is good
amount of data to have 20 centroids. After every cluster assignment, the value of centroid
is updated based on the average of that cluster. So gradually the cluster becomes more
generalised