-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathKMeans.java
184 lines (153 loc) · 5.36 KB
/
KMeans.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.awt.Color;
import java.util.Random;
/**
*
* @author bstornelli
*/
public class Kmeans {
private final static double CLUSTER_RADIO = 0.01;
private final static double POINT_RADIO = 0.005;
private final static double CIRCLE_RADIO = 0.002;
private static double LINE_RADIO = 0.00002;
private final static int SLEEP_TIME = 1;
private static Color[] colors;
private static Point2D[] clusters;
private static Point2D[] points;
private static int[] closestCluster;
private static int n = 2000;
private static int t = 20000;
private static int k = 20;
private static double[] clusterRadio;
private static int iter = 0;
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws InterruptedException {
StdDraw.setXscale(0, t);
StdDraw.setYscale(0, t * 1.1);
points = new Point2D[n];
for (int i = 0; i < n; ++i) {
double x = random(t);
double y = random(t);
points[i] = new Point2D(x, y);
// System.out.println(points[i]);
}
// Clustering:
clusters = new Point2D[k];
initializeColors(k);
// Inicialization: select k points randomly
for (int i = 0; i < k; ++i) {
clusters[i] = points[i];
}
closestCluster = new int[n];
clusterRadio = new double[k];
double[] distance = new double[n]; // Distance to the closest cluster's center
StdDraw.setPenColor();
StdDraw.textLeft(0, t * 1.1, "K-Means Clustering");
StdDraw.textLeft(0, t * 1.05, "Iteration " + iter);
StdDraw.textRight(t, t * 1.1, "n = " + n);
StdDraw.textRight(t, t * 1.05, "k = " + k);
StdDraw.show(0);
drawPoints(false);
StdDraw.show(0);
// Thread.sleep(5000);
StdDraw.show(0);
drawClusters();
StdDraw.show(0);
// Thread.sleep(1000);
int changed;
do {
++iter;
changed = 0;
double[] newX = new double[k];
double[] newY = new double[k];
int[] sizeOfCluster = new int[k];
clusterRadio = new double[k];
for (int p = 0; p < n; ++p) {
distance[p] = Double.POSITIVE_INFINITY;
for (int c = 0; c < k; ++c) {
double d = clusters[c].distanceTo(points[p]);
if (d < distance[p]) {
closestCluster[p] = c;
distance[p] = d;
}
}
newX[closestCluster[p]] += points[p].x();
newY[closestCluster[p]] += points[p].y();
if (distance[p] > clusterRadio[closestCluster[p]]) {
clusterRadio[closestCluster[p]] = distance[p];
}
++sizeOfCluster[closestCluster[p]];
}
// Start the drawing
draw();
Thread.sleep(SLEEP_TIME);
// StdArrayIO.print(newX);
// StdArrayIO.print(newY);
// StdArrayIO.print(sizeOfCluster);
for (int i = 0; i < k; ++i) {
newX[i] /= sizeOfCluster[i];
newY[i] /= sizeOfCluster[i];
Point2D newP;
try {
newP = new Point2D(newX[i], newY[i]);
} catch (IllegalArgumentException e) {
newP = clusters[i];
}
if (!newP.equals(clusters[i])) {
++changed;
}
clusters[i] = newP;
}
} while (changed > 0);
draw();
Thread.sleep(SLEEP_TIME);
}
/**
* Generates a random double greater than or equal to 0 and less than t
*
* @param t
* @return
*/
private static double random(int t) {
return StdRandom.uniform() * t;
}
private static void draw() throws InterruptedException {
StdDraw.show(0);
StdDraw.clear();
StdDraw.setPenColor();
StdDraw.textLeft(0, t * 1.1, "K-Means Clustering");
StdDraw.textLeft(0, t * 1.05, "Iteration " + iter);
StdDraw.textRight(t, t * 1.1, "n = " + n);
StdDraw.textRight(t, t * 1.05, "k = " + k);
drawClusters();
drawPoints(true);
StdDraw.show(SLEEP_TIME / 2);
}
private static void initializeColors(int n) {
colors = new Color[n];
for (int i = 0; i < n; i++) {
colors[i] = Color.getHSBColor((float)StdRandom.uniform(), 0.85f, 1.0f);
}
}
private static void drawPoints(boolean colored) {
for (int i = 0; i < points.length; ++i) {
if (colored) {
StdDraw.setPenColor(colors[closestCluster[i]]);
StdDraw.setPenRadius(LINE_RADIO);
points[i].drawTo(clusters[closestCluster[i]]);
}
StdDraw.setPenRadius(POINT_RADIO);
points[i].draw();
}
}
private static void drawClusters() {
for (int i = 0; i < clusters.length; ++i) {
StdDraw.setPenRadius(CLUSTER_RADIO);
StdDraw.setPenColor(colors[i]);
clusters[i].draw();
// StdDraw.setPenRadius(CIRCLE_RADIO);
// StdDraw.circle(clusters[i].x(), clusters[i].y(), clusterRadio[i]);
}
}
}