0% found this document useful (0 votes)
83 views

K-Means Program

The document describes a K-means clustering program written in Java. The program takes a set of input data points, assigns them initially and randomly to K clusters, calculates the centroid means of each cluster, reassigns points to clusters based on nearest centroid, recalculates centroids, and repeats this process until the assignments stabilize. It includes functions for initializing the centroids, assigning points to clusters, calculating new centroids, and comparing assignments between iterations to check for convergence.

Uploaded by

chirusagar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views

K-Means Program

The document describes a K-means clustering program written in Java. The program takes a set of input data points, assigns them initially and randomly to K clusters, calculates the centroid means of each cluster, reassigns points to clusters based on nearest centroid, recalculates centroids, and repeats this process until the assignments stabilize. It includes functions for initializing the centroids, assigning points to clusters, calculating new centroids, and comparing assignments between iterations to check for convergence.

Uploaded by

chirusagar
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

K-MEANS PROGRAM

/* and open the template in the editor.


*/
package kmeans;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Admin
*/
public class Kmeans {
/**
* @param args the command line arguments
*/
double cluster[];
double ksets[][];
double tsets[][];
int n, k;
double means[];
double tmeans[];
Scanner input = new Scanner(System.in);
public Kmeans() {
System.out.print("enter number of items in the cluster");
n = Integer.parseInt(input.next());
cluster = new double[n];

for (int i = 0; i < n; i++) {


System.out.println("enter the item in the cluster");
cluster[i] = Double.parseDouble(input.next());
}
}

public void chosemeans() {


System.out.println("enter k value");
k = Integer.parseInt(input.next());
means = new double[k];
tmeans = new double[k];
for (int i = 0; i < k; i++) {
means[i] = cluster[i];
}
System.out.println("the means are");
for (int i = 0; i < k; i++) {
System.out.println(means[i]);
}
ksets = new double[k][n];
tsets = new double[k][n];
}

public void clustering() {


int ch = 0;
int p;
do {

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


for (int j = 0; j < n; j++) {
tsets[i][j] = 0.0;
}
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
tsets[i][j] = ksets[i][j];
}
}
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
ksets[i][j] = 0.0;
}
}
ch = ch + 1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
tmeans[j] = Math.abs(means[j] - cluster[i]);
}
Arrays.sort(tmeans);
for (int j = 0; j < k; j++) {
if (Math.abs(means[j] - cluster[i]) == tmeans[0]) {
for (p = 0; p < n; p++) {
if (ksets[j][p] == 0.0) {
break;

}
}
ksets[j][p] = cluster[i];
break;
}
}
}
System.out.println("================= ksets ============");
for (int i = 0; i < k; i++) {
System.out.println("for set k" + (i + 1));
for (int j = 0; j < n; j++) {
if (ksets[i][j] > 0) {
System.out.print(ksets[i][j] + ",");
}
}
System.out.println(" ");
}
int count = 0;
double temp = 0.0;
for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
if (ksets[i][j] > 0.0) {
count = count + 1;
}
}
for (int j = 0; j < n; j++) {

temp = temp + ksets[i][j];


}
means[i] = temp / count;
temp = 0.0;
count = 0;
}
for (int i = 0; i < k; i++) {
System.out.print("means are " + means[i]);
}
System.out.println("================================");
} while (compareKsets());
}

public boolean compareKsets() {


for (int i = 0; i < k; i++) {
for (int j = 0; j < n; j++) {
if (ksets[i][j] != tsets[i][j]) {
return true;
}
}
}
return false;
}

public static void main(String[] args) {


// TODO code application logic here

Kmeans k = new Kmeans();


k.chosemeans();
k.clustering();
}
}

Output
run:
enter number of items in the cluster9
enter the item in the cluster
2
enter the item in the cluster
4
enter the item in the cluster
10
enter the item in the cluster
12
enter the item in the cluster
3
enter the item in the cluster
20
enter the item in the cluster
30
enter the item in the cluster
11
enter the item in the cluster
25
enter k value
2
the means are
2.0
4.0
================= ksets ============
for set k1
2.0,3.0,

for set k2
4.0,10.0,12.0,20.0,30.0,11.0,25.0,
means are 2.5means are 16.0================================
================= ksets ============
for set k1
2.0,4.0,3.0,
for set k2
10.0,12.0,20.0,30.0,11.0,25.0,
means are 3.0means are 18.0================================
================= ksets ============
for set k1
2.0,4.0,10.0,3.0,
for set k2
12.0,20.0,30.0,11.0,25.0,
means are 4.75means are 19.6================================
================= ksets ============
for set k1
2.0,4.0,10.0,12.0,3.0,11.0,
for set k2
20.0,30.0,25.0,
means are 7.0means are 25.0================================
================= ksets ============
for set k1
2.0,4.0,10.0,12.0,3.0,11.0,
for set k2
20.0,30.0,25.0,
means are 7.0means are 25.0================================
BUILD SUCCESSFUL (total time: 7 minutes 13 seconds)

You might also like