-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKmeans.java
executable file
·149 lines (132 loc) · 4.83 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
import java.util.ArrayList;
import java.util.Random;
public class Kmeans {
private final int NUM_CLUSTERS; // the number of clusters
private final int TOTAL_DATA; // Total data points(how many pictures)
private final int y[][]; // multiple one dimensional Y array
private ArrayList<Data> dataSet = new ArrayList<Data>(); // store every data
private ArrayList<Centroid> centroids = new ArrayList<Centroid>(); // store centroids
// Constructor
public Kmeans(int y[][], int numCluster){
this.TOTAL_DATA = y.length;
this.y = y;
NUM_CLUSTERS = numCluster;
}
public ArrayList<Data> start() {
// Randomly choose centroids and add to ArrayList
Random rand = new Random();
for(int i = 0 ; i< NUM_CLUSTERS ; i++){
int rm = rand.nextInt(y.length);
double[] tmp = new double[y[0].length];
for(int j = 0 ; j < tmp.length ; j++){ // int array to double array
tmp[j] = y[rm][j];
}
centroids.add(new Centroid(tmp));
}
// Clustering
return cluster();
}
private ArrayList<Data> cluster() {
final double bigNumber = Math.pow(10, 10); // some big number that's
// sure to be larger than our
// data range.
double minimum = bigNumber; // The minimum value to beat.
double distance = 0.0; // The current minimum value.
int sampleNumber = 0;
int cluster = 0;
boolean isStillMoving = true;
Data newData = null;
// Add in new data, one at a time, recalculating centroids with each new
// one.
while (dataSet.size() < TOTAL_DATA) {
newData = new Data(y[sampleNumber]);
dataSet.add(newData);
minimum = bigNumber;
for (int i = 0; i < NUM_CLUSTERS; i++) {
distance = dist(newData, centroids.get(i));
if (distance < minimum) {
minimum = distance;
cluster = i;
}
}
newData.cluster(cluster);
// calculate new centroids
update();
sampleNumber++;
}
// Now, keep shifting centroids until equilibrium occurs.
while (isStillMoving) {
// calculate new centroids.
update();
// Assign all data to the new centroids
isStillMoving = false;
for (int i = 0; i < dataSet.size(); i++) {
Data tempData = dataSet.get(i);
minimum = bigNumber;
for (int j = 0; j < NUM_CLUSTERS; j++) {
distance = dist(tempData, centroids.get(j));
if (distance < minimum) {
minimum = distance;
cluster = j;
}
}
// tempData.cluster(cluster);
if (tempData.cluster() != cluster) {
tempData.cluster(cluster);
isStillMoving = true;
}
}
}
return dataSet;
}
// Update centroids
private void update(){
// calculate new centroids.
for (int i = 0; i < NUM_CLUSTERS; i++) {
int totalInCluster = 0; // how many in a cluster
int[] total = new int[256];
double[] rslt = new double[256];
// initial
for (int j = 0; j < total.length; j++) {
total[j] = 0;
rslt[j] = 0;
}
for (int j = 0; j < dataSet.size(); j++) {
if (dataSet.get(j).cluster() == i) { // if it belongs to
// cluster i
int[] data = dataSet.get(j).pos();
for (int k = 0; k < data.length; k++) {
total[k] += data[k];
}
totalInCluster++;
}
}
if (totalInCluster > 0) {
for (int j = 0; j < total.length; j++) {
rslt[j] = total[j] / (double)totalInCluster;
}
centroids.get(i).pos(rslt);
}
}
}
// Calculate Euclidean distance.
private static double dist(Data d, Centroid c) {
int[] pos_data = d.pos();
double[] pos_centroid = c.pos();
int sum_square = 0;
if (pos_data.length != pos_centroid.length) {
System.out.println("different");
return 0;
}
for (int i = 0; i < pos_data.length; i++) {
sum_square += Math.pow(pos_data[i] - pos_centroid[i], 2);
}
return Math.sqrt(sum_square);
}
public static void main(String[] args) {
//int[][] array = new int[300][256];
//For test
//Kmeans t = new Kmeans(array);
//t.kmeans();
}
}