-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSimplePCA.java
275 lines (206 loc) · 8.6 KB
/
SimplePCA.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
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
import java.io.*;
import java.util.*;
import cern.colt.matrix.*;
import cern.colt.matrix.impl.*;
import cern.colt.matrix.linalg.*;
public class SimplePCA {
public static void main(String[] args) {
// double[][] input = {{2.5,2.4},{0.5,0.7},{2.2,2.9},{1.9,2.2},{3.1,3},{2.3,2.7},{2,1.6},{1,1.1},{1.5,1.6},{1.1,0.9}};
double[][] input = {{2.5,2.4,3},{0.5,0.7,2},{2.2,2.9,4},{1.9,2.2,2.4},{3.1,3,1.4},{2.3,2.7,0.5},{2,1.6,2.8},{1,1.1,5.1},{1.5,1.6,0.4},{1.1,0.9,2.6}};
cern.jet.math.Functions F = cern.jet.math.Functions.functions;
cern.colt.matrix.linalg.Algebra alg = new cern.colt.matrix.linalg.Algebra();
DoubleMatrix2D values = new DenseDoubleMatrix2D(input);
int n = values.rows();
int m = values.columns();
System.out.println("Original data:");
print(values);
// calculate mean for each dimension
double[] means = new double[m];
for (int j = 0; j < m; j++) {
means[j] = (values.viewColumn(j)).zSum() / (double) n;
}
// substract out the mean for each dimension
DoubleMatrix2D centered = new DenseDoubleMatrix2D(values.toArray());
for (int j = 0; j < m; j++) {
(centered.viewColumn(j)).assign(F.minus(means[j]));
}
System.out.println("Center transformed:");
print(centered);
// find covariance matrix
DoubleMatrix2D cov = alg.mult(centered.viewDice(), centered);
double denom = 1 / ((double) n - 1);
cov.assign(F.mult(denom));
System.out.println("Covariance matrix:");
print(cov);
// find the Eigen decomposition of the covariance matrix
EigenvalueDecomposition eigenSystem = new EigenvalueDecomposition(cov);
DoubleMatrix1D eigenValues = eigenSystem.getRealEigenvalues();
System.out.println("Eigenvalues:");
print(eigenValues);
DoubleMatrix2D eigenVectors = eigenSystem.getV();
System.out.println("Eigenvectors:");
print(eigenVectors);
// indices of top eigenvalues
int firstIndex = indexOfMaximum(eigenValues);
double firstValue = eigenValues.get(firstIndex);
int secondIndex = indexOfNextToMaximum(eigenValues);
double secondValue = eigenValues.get(secondIndex);
System.out.println("Top eigenvalues:");
System.out.println(firstIndex + " " + secondIndex);
System.out.println();
// constructing feature vectors from ordered eigenvectors
DoubleMatrix1D firstVector = eigenVectors.viewColumn(firstIndex);
System.out.println("First eigenvector:");
print(firstVector);
DoubleMatrix1D secondVector = eigenVectors.viewColumn(secondIndex);
System.out.println("Second eigenvector:");
print(secondVector);
DoubleMatrix2D featureVectors = new DenseDoubleMatrix2D(m,m);
(featureVectors.viewColumn(0)).assign(firstVector);
(featureVectors.viewColumn(1)).assign(secondVector);
System.out.println("Feature vectors:");
print(featureVectors);
// projecting original data onto new coordinate system
DoubleMatrix2D projected = (alg.mult(featureVectors.viewDice(),centered.viewDice())).viewDice();
System.out.println("Projected data:");
print(projected);
}
public static double[][] project(double[][] input) {
cern.jet.math.Functions F = cern.jet.math.Functions.functions;
cern.colt.matrix.linalg.Algebra alg = new cern.colt.matrix.linalg.Algebra();
DoubleMatrix2D values = new DenseDoubleMatrix2D(input);
int n = values.rows();
int m = values.columns();
// calculate mean for each dimension
double[] means = new double[m];
for (int j = 0; j < m; j++) {
means[j] = (values.viewColumn(j)).zSum() / (double) n;
}
// substract out the mean for each dimension
DoubleMatrix2D centered = new DenseDoubleMatrix2D(values.toArray());
for (int j = 0; j < m; j++) {
(centered.viewColumn(j)).assign(F.minus(means[j]));
}
// find covariance matrix
DoubleMatrix2D cov = alg.mult(centered.viewDice(), centered);
double denom = 1 / ((double) n - 1);
cov.assign(F.mult(denom));
// find the Eigen decomposition of the covariance matrix
EigenvalueDecomposition eigenSystem = new EigenvalueDecomposition(cov);
DoubleMatrix1D eigenValues = eigenSystem.getRealEigenvalues();
DoubleMatrix2D eigenVectors = eigenSystem.getV();
// ordering of eigenvalues
// indices of top eigenvalues
int firstIndex = indexOfMaximum(eigenValues);
double firstValue = eigenValues.get(firstIndex);
int secondIndex = indexOfNextToMaximum(eigenValues);
double secondValue = eigenValues.get(secondIndex);
// constructing feature vectors from ordered eigenvectors
DoubleMatrix1D firstVector = eigenVectors.viewColumn(firstIndex);
DoubleMatrix1D secondVector = eigenVectors.viewColumn(secondIndex);
DoubleMatrix2D featureVectors = new DenseDoubleMatrix2D(m,m);
(featureVectors.viewColumn(0)).assign(firstVector);
(featureVectors.viewColumn(1)).assign(secondVector);
// projecting original data onto new coordinate system
DoubleMatrix2D projected = (alg.mult(featureVectors.viewDice(),centered.viewDice())).viewDice();
return projected.toArray();
}
public static double[][] project3D(double[][] input) {
cern.jet.math.Functions F = cern.jet.math.Functions.functions;
cern.colt.matrix.linalg.Algebra alg = new cern.colt.matrix.linalg.Algebra();
DoubleMatrix2D values = new DenseDoubleMatrix2D(input);
int n = values.rows();
int m = values.columns();
// calculate mean for each dimension
double[] means = new double[m];
for (int j = 0; j < m; j++) {
means[j] = (values.viewColumn(j)).zSum() / (double) n;
}
// substract out the mean for each dimension
DoubleMatrix2D centered = new DenseDoubleMatrix2D(values.toArray());
for (int j = 0; j < m; j++) {
(centered.viewColumn(j)).assign(F.minus(means[j]));
}
// find covariance matrix
DoubleMatrix2D cov = alg.mult(centered.viewDice(), centered);
double denom = 1 / ((double) n - 1);
cov.assign(F.mult(denom));
// find the Eigen decomposition of the covariance matrix
EigenvalueDecomposition eigenSystem = new EigenvalueDecomposition(cov);
DoubleMatrix1D eigenValues = eigenSystem.getRealEigenvalues();
DoubleMatrix2D eigenVectors = eigenSystem.getV();
// ordering of eigenvalues
// indices of top eigenvalues
int firstIndex = indexOfMaximum(eigenValues);
double firstValue = eigenValues.get(firstIndex);
int secondIndex = indexOfNextToMaximum(eigenValues);
double secondValue = eigenValues.get(secondIndex);
int thirdIndex = indexOfThirdToMaximum(eigenValues);
double thirdValue = eigenValues.get(thirdIndex);
// constructing feature vectors from ordered eigenvectors
DoubleMatrix1D firstVector = eigenVectors.viewColumn(firstIndex);
DoubleMatrix1D secondVector = eigenVectors.viewColumn(secondIndex);
DoubleMatrix1D thirdVector = eigenVectors.viewColumn(thirdIndex);
DoubleMatrix2D featureVectors = new DenseDoubleMatrix2D(m,m);
(featureVectors.viewColumn(0)).assign(firstVector);
(featureVectors.viewColumn(1)).assign(secondVector);
(featureVectors.viewColumn(2)).assign(thirdVector);
// projecting original data onto new coordinate system
DoubleMatrix2D projected = (alg.mult(featureVectors.viewDice(),centered.viewDice())).viewDice();
return projected.toArray();
}
public static void print(DoubleMatrix2D values) {
int m = values.rows();
int n = values.columns();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%.4f ", values.get(i,j));
}
System.out.println();
}
System.out.println();
}
public static void print(DoubleMatrix1D values) {
for (int i = 0; i < values.size(); i++) {
System.out.printf("%.4f ", values.get(i));
}
System.out.println();
System.out.println();
}
public static int indexOfMaximum(DoubleMatrix1D values) {
int index = 0;
double max = -100000;
for (int i = 0; i < values.size(); i++) {
if (values.get(i) > max) {
max = values.get(i);
index = i;
}
}
return index;
}
public static int indexOfNextToMaximum(DoubleMatrix1D values) {
int index = 0;
double max = values.get(indexOfMaximum(values));
double nextToMax = -100000;
for (int i = 0; i < values.size(); i++) {
if (values.get(i) > nextToMax && values.get(i) < max) {
nextToMax = values.get(i);
index = i;
}
}
return index;
}
public static int indexOfThirdToMaximum(DoubleMatrix1D values) {
int index = 0;
double max = values.get(indexOfMaximum(values));
double nextToMax = values.get(indexOfNextToMaximum(values));
double thirdToMax = -100000;
for (int i = 0; i < values.size(); i++) {
if (values.get(i) > thirdToMax && values.get(i) < max && values.get(i) < nextToMax) {
thirdToMax = values.get(i);
index = i;
}
}
return index;
}
}