You are on page 1of 12

EXPERIMENT NO.

AIM:-

Implementation of Clustering algorithm (K-means).

THEORY:-
K-means Algorithm:-
K-means algorithm is an iterative algorithm that tries to partition the
dataset into Kpre-defined distinct non-overlapping subgroups (clusters) where
each data point belongs to only one group. It tries to make the intra-cluster data
points as similar as possible while also keeping the clusters as different (far) as
possible. It assigns data points to a cluster such that the sum of the squared
distance between the data points and the cluster’s centroid (arithmetic mean of
all the data points that belong to that cluster) is at the minimum. The less
variation we have within clusters, the more homogeneous (similar) the data
points are within the same cluster.
The way kmeans algorithm works is as follows:
 Specify number of clusters K.

 Initialize centroids by first shuffling the dataset and then


randomly selecting K data points for the centroids without
replacement.

 Keep iterating until there is no change to the centroids. i.e assignment


of data points to clusters isn’t changing.

 Compute the sum of the squared distance between data points and
all centroids.

 Assign each data point to the closest cluster (centroid).


 Compute the centroids for the clusters by taking the average of the all
data points that belong to each cluster.
PROGRAM:-
#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace
std; int main()
{
int numbers, k, kvals[25], prevKvals[25], steps = 1, addition[25][100], count = 0,
groups[25][100], min, groupnum, value, sum, ok = 1, nums[100];
cout << "How many numbers you want to enter: ";
cin >> numbers;
cout << "Enter value of k:
"; cin >> k;
//get numbers
for(int i = 0; i < numbers; i++)

{
cout << "Enter Number " << i+1 << ": ";
cin >> nums[i];
}
// set values of C's
for(int i = 0; i < 3; i+
+)
{
kvals[i] = nums[i];
}
//show values of user
cout << "You have entered: ";
for(int i = 0; i < numbers; i+
+)
{
cout << nums[i] << ", ";
}
//while(steps < 10)
while(ok == 1)
{
cout << endl << "Itration Number: " << steps;
//make calculations (C - bla bla bla)
for(int i = 0; i < k; i++)
{
for(int j = 0; j < numbers; j++)
{
addition[i][j] = abs(kvals[i] - nums[j]);
}
}
//make groups of number(C)
for(int i = 0; i < numbers; i+
+)
{
min = 100000;
for(int j = 0; j < k; j++)
{
if(addition[j][i] < min)
{
min = addition[j]
[i]; value =
nums[i]; groupnum
= j;
}

groups[groupnum][i] = value;
}
//show results of calculations (C - bla bla bla)
cout << endl << "Calculations" << endl;
for(int i = 0; i < numbers; i++)
{
for(int j = 0; j < k; j++)
{cout << addition[j][i] << "\t";
}
cout << endl;
}
// show groups and get new C's
cout << endl << "Gruops" << endl;
for(int i = 0; i < k; i++)
{sum = 0;
count = 0;
cout << "Group " << i+1 << ":
"; for(int j = 0; j < numbers; j+
+)
{if(groups[i][j] != '\0')
{cout << groups[i][j] << "\t";
sum += groups[i]
[j]; count++;
}
}
prevKvals[i] =
kvals[i]; kvals[i] =
sum/count;
cout << "\t=\t" << kvals[i] << endl;
}
//make empty array of
groups for(int i = 0; i < 25;
i++)
{for(int j = 0; j < 100; j++)
{groups[i][j] = '\0';
}
}
//check condition of termination
ok = 0;
for(int i = 0; i < k; i++)
{
if(prevKvals[i] != kvals[i])
{
ok = 1;

}
}
if(ok != 1)
{
getch();
}
steps++;
} // end while
loop getch();
return 0;
}
OUTPUT:-
How many numbers you want to enter: 9
Enter value of k: 3
Enter Number 1: 2
Enter Number 2: 12
Enter Number 3: 67
Enter Number 4: 34
Enter Number 5: 56
Enter Number 6: 23
Enter Number 7: 78
Enter Number 8: 38
Enter Number 9: 28
You have entered: 2, 12, 67, 34, 56, 23, 78, 38, 28,
Iteration Number:
1 Calculations
0 10 65

10 0 55

65 55 0
32 22 33

54 44 11

21 11 44

76 66 11

36 26 29

26 16 39
Groups

Group 1: 2 = 2

Group 2: 12 34 23 38 28 = 27

Group 3: 67 56 78 = 67

Iteration Number:
2 Calculations
0 25 65

10 15 55

65 40 0

32 7 33

54 29 11

21 4 44
76 51 11

36 11 29

26 1 39
Groups

Group 1: 2 12 = 7

Group 2: 34 23 38 28 = 30

Group 3: 67 56 78 = 67

Iteration Number:
3 Calculations
5 28 65

5 18 55

60 37 0

27 4 33

49 26 11

16 7 44

71 48 11

31 8 29

21 2 39
Groups

Group 1: 2 12 = 7

Group 2: 34 23 38 28 = 30

Group 3: 67 56 78 = 67

CONCLUSION:-
We learnt to implement K-Means clustering algorithm successfully.

You might also like