-
Notifications
You must be signed in to change notification settings - Fork 0
/
ml.js
155 lines (100 loc) · 4.01 KB
/
ml.js
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
var math = require('mathjs');
sigmoid = (z) => {
return math.dotDivide(1.0 , math.add(1, math.dotPow(Math.E, math.multiply(-1, z))) )
}
normalize = (X_) => {
X = X_.clone();
var mean = math.zeros(1, X._size[1]);
var std = math.zeros(1, X._size[1]);
for (i = 0; i < X._size[1]; i++) {
col = getCol(X, i)
std = math.subset(std, math.index(0, i), math.std(col));
mean = math.subset(mean, math.index(0, i), math.mean(col));
}
var mu_matrix = math.multiply(math.ones(X._size[0], 1), mean)
var std_matrix = math.multiply(math.ones(X._size[0], 1), std)
var X_norm = math.dotDivide(math.subtract(X, mu_matrix), std_matrix);
return { 'X': X_norm, 'mean': mean, 'std': std };
}
//Compute cost and gradient for logistic regression with regularization
jCostLogisticRegression = (X, y, theta_, lambda) => {
let theta = theta_.clone();
let m = math.size(y).get([0]);
let thetaByX = math.multiply(X, theta);
let h = sigmoid(thetaByX); //hypothesis
theta.subset(math.index(0, 0), 0);
let lambdaBy2m = (lambda / (2 * m));
let regularization = math.multiply(lambdaBy2m, math.multiply(math.transpose(theta), theta));
let negativeY = math.multiply(-1, y);
let yEqual1 = math.multiply(math.transpose(negativeY), math.log(h) )
let yEqual0 = math.multiply(math.transpose(math.subtract(1, y)), math.log(math.subtract(1, h)))
let jcost = math.add(math.multiply((1/m), math.subtract(yEqual1, yEqual0)) , regularization)
J = jcost.subset(math.index(0, 0))
let gradRegularization = math.multiply(lambda/m, theta);
let grad = math.add(math.multiply((1/m), math.multiply(math.transpose(X) , math.subtract(h, y))), gradRegularization);
return {'JCost': J, 'grad': grad }
}
//DO we need alpha?
logisticGradientRegression = (jCostFunc, X, y, theta_, iterations, lambda = 0.01, alpha = 0.03) => {
let theta = theta_.clone();
let m = math.size(y).get([0]);
var jcost = new Array();
for (i = 0; i < iterations; i++) {
result = jCostFunc(X, y, theta, lambda)
jcost.push(result.JCost)
//alpha/m should be on the 1's place
theta = math.subtract(theta, math.multiply( 1 , result.grad))
}
return {'theta': theta, 'JCost': jcost } ;
}
oneVsAll = (X, y, lambda, iterations, progressCallBack = null) => {
const labels = y.toArray().map(x => x[0]).filter((x, i, a) => a.indexOf(x) == i);;
const m = X._size[0];
const n = X._size[1];
var allTheta = null;
console.log("total labels " + labels.length)
labels.forEach((v, i) => {
console.log("Leaning: " + v)
var newY = math.matrix(y.toArray().map(x => x[0] == v?[1]:[0]));
var theta = math.zeros(math.size(X).get([1]), 1)
result = logisticGradientRegression(jCostLogisticRegression, X, newY, theta, iterations, lambda)
theta = result.theta;
if (allTheta == null) {
allTheta = theta.clone();
}
else
allTheta = math.concat(allTheta, theta);
if (progressCallBack != null) {
progressCallBack((i + 1) * 100 / labels.length)
}
});
return allTheta;
}
/**
* For X, we have to normalise, add 1 then multiply by our theta
**/
predictLogisticGradient = (X_, mean, std, theta) => {
let X = X_.clone();
var mu_matrix = math.multiply(math.ones(X._size[0], 1), mean)
var std_matrix = math.multiply(math.ones(X._size[0], 1), std)
X = math.dotDivide(math.subtract(X, mu_matrix), std_matrix);
X = addInterceptTerm(X)
return sigmoid(math.multiply(X, theta))
}
addInterceptTerm = (M_) => {
M = M_.clone();
return math.concat(math.ones(M._size[0], 1), M)
}
getRow = (M, i) => {
return math.flatten(M.subset(math.index(i, math.range(0, M._size[1])))).toArray();
}
getCol = (M, i) => {
return math.flatten(M.subset(math.index(math.range(0, M._size[0]),i))).toArray();
}
getYCol = (M) => {
return M.subset(math.index(math.range(0, M._size[0]),[M._size[1] - 1]))
}
getXMatrix = (M) => {
return M.subset(math.index(math.range(0, M._size[0]), math.range(0, M._size[1] - 1) ))
}
module.exports = { sigmoid, normalize, jCostLogisticRegression, logisticGradientRegression, oneVsAll, predictLogisticGradient, addInterceptTerm, getRow, getCol, getYCol, getXMatrix }