-
Notifications
You must be signed in to change notification settings - Fork 1
/
KMeans2D.java
125 lines (125 loc) · 3.16 KB
/
KMeans2D.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.lang.Math.*;
import java.util.*;
public class KMeans2D {
Scanner sc = new Scanner(System.in);
int n_elements, n_clusters, flag;
int ptr[] = new int[100];
double centroid[][] = new double[100][2];
double oldCentroid[][] = new double[100][2];
static double elements[][] = new double[100][2];
double k[][][] = new double[100][100][2];
double dist(double x1, double y1, double x2, double y2)
{
return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
void input() throws InvalidClusterSize
{
System.out.print("\nEnter number of Clusters: ");
n_clusters = sc.nextInt();
System.out.print("\nEnter number of Points: ");
n_elements = sc.nextInt();
if (n_elements < n_clusters)
{
throw new InvalidClusterSize(
"\nNumber of Elements should be greater than or equal to the number of Clusters\n");
}
// input points
System.out.println("\nEnter Points");
for (int i = 0; i < n_elements; i++)
{
System.out.print("Enter Point " + (i + 1) + ": ");
elements[i][0] = sc.nextDouble();
elements[i][1] = sc.nextDouble();
}
// input centroids
System.out.println("\nEnter Centroid");
for (int i = 0; i < n_clusters; i++)
{
System.out.print("Enter Centroid " + (i + 1) + ": ");
centroid[i][0] = sc.nextDouble();
centroid[i][1] = sc.nextDouble();
}
}
void find() {
int clusterIndex = flag = 0;
double leastDiff, tempDist;
// initialise pointer for every cluster
for (int i = 0; i < n_clusters; i++) {
ptr[i] = 0;
}
// store elements in cluster
for (int i = 0; i < n_elements; i++) {
leastDiff =
dist(elements[i][0], elements[i][1], centroid[0][0], centroid[0][1]);
clusterIndex = 0;
for (int j = 0; j < n_clusters; j++) {
tempDist =
dist(elements[i][0], elements[i][1], centroid[j][0], centroid[j][1]);
if (tempDist < leastDiff) {
leastDiff = tempDist;
clusterIndex = j;
}
}
k[clusterIndex][ptr[clusterIndex]][0] = elements[i][0];
k[clusterIndex][ptr[clusterIndex]][1] = elements[i][1];
ptr[clusterIndex]++;
}
// calculate centroid
for (int i = 0; i < n_clusters; i++) {
centroid[i][0] = centroid[i][1] = 0;
for (int j = 0; j < ptr[i]; j++) {
centroid[i][0] += k[i][j][0];
centroid[i][1] += k[i][j][1];
}
centroid[i][0] /= ptr[i];
centroid[i][1] /= ptr[i];
}
// break the recursive call if clusters are same
for (int i = 0; i < n_clusters; i++) {
if (
(oldCentroid[i][0] != centroid[i][0]) ||
oldCentroid[i][1] != centroid[i][1]
) {
flag = 1;
break;
}
}
if (flag == 1) {
for (int i = 0; i < n_clusters; i++) {
oldCentroid[i][0] = centroid[i][0];
oldCentroid[i][1] = centroid[i][1];
}
find();
}
}
void display() {
for (int i = 0; i < n_clusters; i++) {
System.out.println("\nCluster " + (i + 1) + ":");
for (int j = 0; j < ptr[i]; j++) {
System.out.println(k[i][j][0] + ", " + k[i][j][1]);
}
System.out.printf(
"Centroid %d = (%.2f, %.2f)\n",
(i + 1),
centroid[i][0],
centroid[i][1]
);
}
}
public static void main(String args[]) {
try {
KMeans2D km = new KMeans2D();
km.input();
km.find();
km.display();
System.out.println("");
} catch (InvalidClusterSize e) {
System.out.println("\nException occured: " + e);
}
}
}
class InvalidClusterSize extends Exception {
public InvalidClusterSize(String s) {
super(s);
}
}