-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpca.html
More file actions
81 lines (68 loc) · 1.78 KB
/
pca.html
File metadata and controls
81 lines (68 loc) · 1.78 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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Streamgraph</title>
<style>
body {
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
margin: auto;
position: relative;
width: 960px;
}
button {
position: absolute;
right: 10px;
top: 10px;
}
</style>
<script src="./js/numeric-1.2.6.min.js"></script>
<script>
function pca(X) {
console.log(X);
/*
Return matrix of all principle components as column vectors
*/
// console.log(X[0], X);
console.time("pca");
var m = X.length;
var sigma = numeric.div(numeric.dot(numeric.transpose(X), X), m);
console.timeEnd("pca");
return numeric.svd(sigma).U;
}
function pcaReduce(U, k) {
/*
Return matrix of k first principle components as column vectors
*/
return U.map(function(row) {
return row.slice(0, k)
});
}
function pcaProject(X, Ureduce) {
/*
Project matrix X onto reduced principle components matrix
*/
return numeric.dot(X, Ureduce);
}
function pcaRecover(Z, Ureduce) {
/*
Recover matrix from projection onto reduced principle components
*/
return numeric.dot(Z, numeric.transpose(Ureduce));
}
window.onload = function() {
var x, y,z, X = [];
var noise = function() {return Math.random() * 0.2 - 0.1};
// Create random dataset with slope of 0.357 and noise
for (var i = 0; i < 1000; i++) {
x = Math.random() * 2 - 1;
y = x * 0.357;
z = Math.random() * 2 - 1;
a = Math.random() * 2 - 1;
X.push([x + noise(), y + noise(), z, a]);
}
// Get principle components
var U = pca(X);
console.log('U', U);
// Print slope of first principle component
document.write(Math.abs(U[0][1] / U[0][0]).toFixed(3));
};
</script>