## K-Mean Clustering Algorithm Implementation in C and Java

Posted 2 years ago

Last Updated 1 year ago

4273 views ## K-Mean

K-Means Algorithm is very simplest unsupervised learning algorithm that is used to solve clustering problem in data Mining. lets first understand how K-Mean algorithm works with example , lets say we have random data like this , it appears to be two clusters in this data and we can easily group them using K-Mean algorithm . K-Mean algorithm create clusters and add each data items into these clusters based on minimum value of  difference between cluster centroids and data item , in this example k-mean algorithm will create two clusters and add each data into its own cluster based on difference  centroid 1 and 2 with data item, here is the implementation of this algorithm in java.

## C++ Code

```#include
#include
#include
#include
#include
using namespace std;

int min(int arr[], int maxIndex)
{
int min=100000000000;
for(int i=0;i vc )
{
int sum=0;
for(int i=0;i vc )
{
for(int i=0;i> noOfItems;
cout<<"Enter value of K:"<> k;
int cluster[k];
int oldCluster[k];
int objects[noOfItems];
int row[k];
vector< vector > groups;

for(int i=0;i>objects[i];
if(i newGroup;
groups.push_back(newGroup);
}
int iter =1;
do
{
for(int i=0;i```

## Java Code

```import static java.lang.Math.abs;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class KMean {

int k;
int noOfItems;
ArrayList dataItems;
ArrayList cz;
ArrayList oldCz;
ArrayList row;
ArrayList> groups;
Scanner input;

public KMean(int k, int noOfItems) {
this.k = k;
this.noOfItems = noOfItems;
dataItems = new ArrayList<>();
cz = new ArrayList<>();
oldCz = new ArrayList<>();
row = new ArrayList<>();
groups = new ArrayList<>();
input = new Scanner(System.in);

for (int i = 0; i < k; i++) {
}

for (int i = 0; i < noOfItems; i++) {
System.out.println("Enter Value for: " + (i + 1) + " item");
if (i < k) {
System.out.println("C" + (i + 1) + " is " + cz.get(i));
}
}
int iter = 1;
do {
for (int aItem : dataItems) {
for (int c : cz) {
}
row.removeAll(row);
}
for (int i = 0; i < k; i++) {
if (iter == 1) {
} else {
oldCz.set(i, cz.get(i));
}
if (!groups.get(i).isEmpty()) {
cz.set(i, average(groups.get(i)));
}
}
if (!cz.equals(oldCz)) {
for (int i = 0; i < groups.size(); i++) {
groups.get(i).removeAll(groups.get(i));
}
}
iter++;
} while (!cz.equals(oldCz));
for (int i = 0; i < cz.size(); i++) {
System.out.println("New C" + (i + 1) + " " + cz.get(i));
}
for (int i = 0; i < groups.size(); i++) {
System.out.println("Group " + (i + 1));
System.out.println(groups.get(i).toString());
}
System.out.println("Number of Itrations: " + iter);
}

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Value of K");
int k = input.nextInt();
System.out.println("Enter No of Data Items");
int noOfItems = input.nextInt();
new KMean(k, noOfItems);
}

public static int average(ArrayList list) {
int sum = 0;
for (Integer value : list) {
sum = sum + value;
}
return sum / list.size();
}
}
```

i hope this post helped you to understand k-mean algorithm :)

#### Enter Email to Subscribe

Amazing tutorials deliver daily to you