-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSpectralClustering.java
More file actions
executable file
·297 lines (245 loc) · 8.76 KB
/
SpectralClustering.java
File metadata and controls
executable file
·297 lines (245 loc) · 8.76 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package bric.Cluster;
//IMORTS
import Jama.Matrix;
import Jama.EigenvalueDecomposition;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import static java.lang.Math.*;
/**
*
* @author BrianCarlsen
*/
public class SpectralClustering {
private ArrayList<double[]> data;
private Matrix affinity;
private Matrix laplacian;
private PriorityQueue<Eigenvector> eVectors;
private Cluster[] clusters;
public SpectralClustering (double[][] d) {
data = new ArrayList<double[]>(d.length);
data.addAll(Arrays.asList(d));
affinity = new Matrix(data.size(), data.size());
updateAffinity();
eVectors = new PriorityQueue<Eigenvector>();
updateEigenvectors();
laplacian = new Matrix
(affinity.getRowDimension(), affinity.getRowDimension());
updateLaplacian();
}
private void updateAffinity(double sigma) {
//the affinity matrix is symmetric along the diaganol so
// we only have to do the comutations for the lower half
for (int i = 0; i < affinity.getRowDimension(); ++i) {
for (int j = 0; j < i; ++j) { //lower diaganol
//double dist = getEuclideanDistance(data.get(i), data.get(j));
double v = getGaussianDistance(data.get(i), data.get(j), sigma);
affinity.set(i, j, v);
affinity.set(j, i, v); //symmetric matrix
}
}
//the diaganol entries are always 0 becuase it is measuring
// the distance from a point to itself
for (int i = 0; i < affinity.getRowDimension(); ++i) {
affinity.set(i, i, 0);
}
}
private void updateAffinity() {
updateAffinity(1);
}
/**
*
* @param p1 row vector
* @param p2 row vector
* @return Euclidean distance between p1 and p2
*/
private static double getEuclideanDistance(double[] p1, double[] p2) {
double dist = 0;
for (int i = 0; i < p1.length; ++i) {
dist += pow( p1[i] - p2[i], 2);
}
return sqrt(dist);
}
private static double getGaussianDistance(double[] p1, double[] p2, double sigma) {
double dist = getEuclideanDistance(p1, p2);
return exp( -pow(dist, 2) / (2*pow(sigma, 2)) );
}
private Matrix getDegreeMatrix() {
Matrix d = new Matrix
(affinity.getRowDimension(), affinity.getRowDimension());
for (int i = 0; i < affinity.getRowDimension(); ++i) {
double val = 0;
for (int j = 0; j < affinity.getColumnDimension(); ++j) {
val += affinity.get(i, j);
}
d.set(i, i, val);
}
return d;
}
private void updateLaplacian() {
Matrix degree = getDegreeMatrix();
for (int i = 0; i < degree.getRowDimension(); ++i) {
degree.set(i, i, sqrt(degree.get(i, i)));
}
degree = degree.inverse();
//construct Symmetric Laplacian Matrix
// D^(-1/2) W D^(-1/2)
laplacian = degree.times( affinity.times(degree) );
}
private void updateEigenvectors() {
EigenvalueDecomposition e = affinity.eig();
Matrix eVec = e.getV();
Matrix eVal = e.getD(); //assume all eigenvalues are real
for (int i = 0; i < eVec.getColumnDimension(); ++i) {
double[] vec = eVec.getMatrix(0, eVec.getRowDimension() - 1, i, i)
.getColumnPackedCopy();
double val = eVal.get(i, i);
eVectors.add( new Eigenvector(vec, val) );
}
}
private Matrix getNormalEigenMatrix(int numClusters) {
Matrix X = new Matrix(laplacian.getRowDimension(), numClusters);
for (int i = 0; i < numClusters; ++i) {
double[][] t = {eVectors.poll().getVector()};
Matrix tm = new Matrix(t);
X.setMatrix
(0, X.getRowDimension() - 1, i, i, tm.transpose() );
}
//normalize rows
for (int i = 0; i < X.getRowDimension(); ++i) {
double norm = 0;
for (int j = 0; j < X.getColumnDimension(); ++j) { //calculate norm
norm += pow( X.get(i, j), 2);
}
for (int j = 0; j < X.getColumnDimension(); ++j) { //normalize each element
X.set(i, j, X.get(i, j)/ sqrt(norm));
}
}
return X;
}
public static Cluster[] cluster(double[][] d, int numClusters) {
SpectralClustering sc = new SpectralClustering(d);
sc.cluster(numClusters);
return sc.getClusters();
}
public void cluster(int numClusters) {
Matrix Y = getNormalEigenMatrix(numClusters);
//cluster using kMeans
KMeans km = new KMeans
(Y.getArray(), numClusters);
km.cluster(100);
Cluster[] clust = km.getClusters();
ArrayList<Cluster> clusterList = new ArrayList<Cluster>();
ArrayList<double[]> dataClone = new ArrayList<double[]>(data.size());
dataClone.addAll(data);
for (Cluster c : clust) {
Cluster tc = new Cluster(data.get(0).length);
for (int i = 0; i < Y.getRowDimension(); ++i) {
double[] eVec = Y.getArray()[i];
//assign original point si to cluster j if row i in X
// was assigned to cluster j
for (double[] r : c.getData()) {
if (Arrays.asList(r).equals(Arrays.asList(eVec))) {
double[] g = data.get(i);
tc.addData(data.get(i));
}
}
}
tc.centerOnData();
clusterList.add(tc);
}
clusters = new Cluster[clusterList.size()];
clusterList.toArray(clusters);
}
/* TODO
*
public void smartCluster(int start, int end) {
ArrayList<Cluster> bestCluster = new ArrayList<Cluster>();
double bestCost = Double.POSITIVE_INFINITY;
for (int i = start; i <= end; ++i) {
cluster(i);
double cCost = cost();
if (cCost < bestCost) {
bestCost = cCost;
bestCluster.clear();
bestCluster.addAll(Arrays.asList(clusters));
}
}
bestCluster.toArray(clusters);
}
private double cost() {
double[] distError = new double[clusters.length];
int i = 0;
for (Cluster c : clusters) { //get distance between each point and its associated points
ArrayList<double[]> cData = c.getData();
double[] cPos = c.getPosition();
for (double[] d : cData)
distError[i] *= getGaussianDistance(cPos, d, 1);
++i;
}
double error = 0;
for (double de : distError)
error += de;
double cost = 10/error + 10*clusters.length;
System.out.println(error);
return cost;
}
*/
public Cluster[] getClusters() {
return clusters;
}
public void show() {
int i = 0;
for (Cluster c : clusters) {
System.out.println("Cluster " + (i++) + ":");
c.show();
}
}
private class Eigenvector implements Comparable<Eigenvector>{
private double[] vector;
private double value; // eigenvalue
public Eigenvector(double[] vec, double val) {
vector = vec;
value = val;
}
public double[] getVector() {
return vector;
}
public void setVector(double[] vec) {
vector = vec;
}
public double getValue() {
return value;
}
public void setValue(double val) {
value = val;
}
@Override
public int compareTo(Eigenvector e) {
Double v = new Double(value);
Double v2 = new Double(e.getValue());
return -v.compareTo(v2);
}
public int equals(Double oVal) {
return this.equals(oVal);
}
}
public static void main(String[] args) {
int N = 10;
double[][] points = new double[N][2];
for (int i = 0; i < N; ++i) {
double val = i;
if(i > N/2)
val +=50;
points[i][0]= random()*100;
points[i][1] = points[i][0];
}
SpectralClustering sp = new SpectralClustering(points);
sp.cluster(5);
sp.show();
}
}