forked from fanta-mnix/machine-learning-challenge1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
answer.js
175 lines (145 loc) · 4.65 KB
/
answer.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// CUIDADO: Você está prestes a olhar a resposta!
// CUIDADO: Você está prestes a olhar a resposta!
// CUIDADO: Você está prestes a olhar a resposta!
// CUIDADO: Você está prestes a olhar a resposta!
// CUIDADO: Você está prestes a olhar a resposta!
var fs = require("fs");
var d3 = require("d3");
var _ = require("lodash");
var assert = require("assert");
function getKeys(object) {
return Object.keys(object).filter(k => object.hasOwnProperty(k));
}
function getValues(object) {
return getKeys(object).map(k => +object[k]);
}
function select(dataFrame, columnName) {
return dataFrame.map(x => x[columnName]);
}
function dot(lhs, rhs) {
assert(lhs.length === rhs.length);
return lhs.map((element, index) => element * rhs[index]).reduce((a, b) => a + b);
}
function scale(scalar, x) {
return x.map(element => scalar * element);
}
function add(lhs, rhs) {
assert(lhs.length === rhs.length);
return lhs.map((element, index) => element + rhs[index])
}
function inPlaceAdd(lhs, rhs) {
assert(lhs.length === rhs.length);
for (var i = lhs.length - 1; i >= 0; i--) {
lhs[i] += rhs[i];
}
return lhs;
}
function sub(lhs, rhs) {
assert(lhs.length === rhs.length);
return lhs.map((element, index) => element - rhs[index])
}
function inPlaceSub(lhs, rhs) {
assert(lhs.length === rhs.length);
for (var i = lhs.length - 1; i >= 0; i--) {
lhs[i] -= rhs[i];
}
return lhs;
}
function norm(x) {
return Math.sqrt(x.map(x => x * x).reduce((a, b) => a + b));
}
function binarize(labels, targetLabel) {
return labels.map(label => label === targetLabel ? 1 : 0);
}
function calculateGradient(X, y, n, predict) {
const m = X.length;
const gradient = new Array(n).fill(0);
const predictions = X.map(x => predict(x));
for (var i = 0; i < m; i++) {
if (y[i] === predictions[i]) {
continue;
}
const x = X[i];
if (y[i] === 1) {
inPlaceAdd(gradient, x);
} else {
inPlaceSub(gradient, x);
}
}
return gradient;
}
function binaryThreshold(output) {
return output >= 0 ? 1 : 0;
}
function train(keys, X, y, calculateGradient, learningRate, maxIterations) {
const insertBias = function insertBias(x) {
return [1].concat(x);
};
const n = keys.length;
const Xt = X.map(x => insertBias(x));
const weights = new Array(n + 1).fill(0);
var predict = function predict(x) {
const output = dot(x, weights);
return binaryThreshold(output);
};
for (var iteration = 0; iteration < maxIterations; iteration++) {
const gradient = calculateGradient(Xt, y, weights.length, predict);
inPlaceAdd(weights, scale(learningRate, gradient));
const normalizedDelta = norm(gradient) / norm(weights);
console.log(`Iteration: ${iteration}, Delta: ${normalizedDelta}`);
if (normalizedDelta < 2 * learningRate) {
break;
}
}
console.log('Number of iterations: ' + iteration);
return {
predict: function (x) {
return predict(insertBias(x));
}
}
}
function trainTestSplit(X, y, testRatio) {
function fromIndexes(array, indexes) {
return array.filter((element, index) => indexes.includes(index));
}
assert(X.length === y.length);
const size = X.length;
const indexes = _.shuffle(_.range(size));
const cutoff = Math.round(size * testRatio);
const testIndexes = indexes.slice(0, cutoff);
const trainIndexes = indexes.slice(cutoff, size);
return {
train: {
X: fromIndexes(X, trainIndexes),
y: fromIndexes(y, trainIndexes)
},
test: {
X: fromIndexes(X, testIndexes),
y: fromIndexes(y, testIndexes)
}
};
}
function score(model, X, y) {
const predictions = X.map(x => model.predict(x));
return predictions.filter((p, idx) => p === y[idx]).length / y.length;
}
var model;
var splitData;
fs.readFile("features.csv", "utf8", function(error, data) {
if (error) {
throw error;
}
const dataFrame = d3.csvParse(data);
const keys = getKeys(dataFrame[0]);
const X = dataFrame.map(x => getValues(x));
fs.readFile("labels.csv", "utf8", function(error, data) {
if (error) {
throw error;
}
const y = select(d3.csvParse(data), 'digit');
const zeroes = binarize(y, '0');
splitData = trainTestSplit(X, zeroes, 0.2);
model = train(keys, splitData.train.X, splitData.train.y, calculateGradient, 1e-3, 50);
console.log(`Score on test data: ${score(model, splitData.test.X, splitData.test.y) * 100}%`);
});
});