Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
400 lines (327 loc) · 11.2 KB
/
Copy pathindex.ts
File metadata and controls
400 lines (327 loc) · 11.2 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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import data from "./iris.json"; // Assume that have header with format: [param1, ..., paramN, class label]
// Associate index number with each class name
let classArr; // map from [index] => [name]
let classMap; // map from [name] => [index]
let classLabelNum; // length of the class
let dataColNum; // Number of columns in data
let trainArr;
let validArr;
let testArr;
enum NodeType {
root,
nonTerminal,
leaf
}
type feature = {
name: String,
fn: (params: (number | string)[]) => number,
numSplit: number
};
type leafResult = {
name: String,
percentage: number,
number: number
};
class TreeNode {
type: NodeType;
children: TreeNode[];
data: (number | string)[][];
vote: string;
result: leafResult[];
gini: number;
splitter: feature;
predictSample(sample:(number | string)[]):string {
if (this.type == NodeType.leaf) {
return this.vote;
}
else {
return this.children[this.splitter.fn(sample)].predictSample(sample);
}
}
constructor({type, dataset}) {
this.type = type;
this.data = dataset;
this.children = [];
this.result = [];
// Calculate gini index
let buckets: number[] = [];
for (let i = 0; i < classLabelNum; i++) {
buckets.push(0);
}
// Set gini impurity
this.data.forEach((line) => {
buckets[classMap.get(line[dataColNum - 1])]++;
});
this.gini = getGiniImpurity(buckets);
}
}
class RandomForest {
dataset: ((number | string)[])[];
featureSet: feature[];
decisionTrees: TreeNode[];
// Parameters to be set according to other inputs
numTrees: number;
numFeatures: number;
constructor({dataset, featureSet, numTrees, numFeatures}) {
this.dataset = dataset;
this.featureSet = featureSet;
this.numTrees = numTrees;
this.numFeatures = numFeatures;
this.decisionTrees = [];
}
// Create forest of decision trees
createForest() {
for (let i = 0; i < this.numTrees; i++) {
let root: TreeNode = new TreeNode({type: NodeType.root, dataset: this.dataset});
buildTree(root, randomSubSetReplacement(this.featureSet, this.numFeatures)); // Builds tree with random subset of features with replacement
this.decisionTrees.push(root);
}
}
ensemblePredict(sample: (number | string)[]):string {
let voteTallies:number[] = []; // Set up vote array
for (let i = 0; i < classLabelNum; i++) {
voteTallies.push(0);
}
this.decisionTrees.forEach(tree => voteTallies[classMap.get((tree.predictSample(sample)))]++);
let maxVotesIndex = 0;
for (let i = 1; i < classLabelNum; i++){
if (voteTallies[i] > voteTallies[maxVotesIndex]) {
maxVotesIndex = i;
}
}
return classArr[maxVotesIndex];
}
fullTest(sampleSet: (number | string)[][]) {
let correct = 0;
sampleSet.forEach((sample) => {
if (this.ensemblePredict(sample) == sample[dataColNum - 1]){
correct++;
}
})
console.log("Correct: " + String(correct) + "/" + String(sampleSet.length));
}
}
function processData(trainProportion: number, validProportion: number, testProportion: number) {
if ((trainProportion + validProportion + testProportion) != 1) {
throw new RangeError();
}
trainArr = [];
validArr = [];
testArr = [];
classMap = new Map();
classArr = [];
let curIndex = 0;
dataColNum = data[0].length;
// Calculate when to switch between train, valid, test data
let trainStop = trainProportion*data.length;
let validStop = trainStop + validProportion*data.length;
// Shuffle array before dividing it
let shuffledData = randomSubSet(data,data.length);
shuffledData.forEach((line, index) => {
// create copy in array format
let curLine: (number | string)[] = [];
line.forEach((elem) => {
curLine.push(elem);
});
if (index < trainStop){
trainArr.push(curLine);
}
else if (index < validStop) {
validArr.push(curLine);
}
else {
testArr.push(curLine);
}
// determine unique results
let curClass = line[dataColNum - 1];
if (!(classMap.has(curClass))) {
classArr.push(curClass);
classMap.set(curClass, curIndex++);
}
})
classLabelNum = classArr.length;
}
function getRandomIntInclusive(min:number, max:number):number {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
function randomSubSet(list: any[], subSetLen: number):any[] {
if (list.length < 2) {
return list;
}
let returnList = [];
let listLen = list.length;
for (let i = 0; i < subSetLen; i++) {
let swapIndex = getRandomIntInclusive(i, listLen - 1);
let swapValue = list[swapIndex];
list[swapIndex] = list[i]; // Make sure each item has equal chance, and no duplicates
returnList.push(swapValue);
}
return returnList;
}
function randomSubSetReplacement(list: any[], subSetLen: number):any[] {
let returnList = [];
let listLen = list.length;
for (let i = 0; i < subSetLen; i++) {
returnList.push(Math.floor(Math.random()*listLen));
}
return returnList;
}
function getGiniImpurity(buckets: number[]):number {
let gini = 1;
let total = 0;
buckets.forEach(bucket => total += bucket);
buckets.forEach(bucket => {
gini -= Math.pow(bucket/total, 2);
});
return gini;
}
// Recursive function to build tree one node at a time
function buildTree(node: TreeNode, featureSet: feature[]){
// See which feature best splits data
let maxGainIndex = null;
let maxGainValue = 0;
featureSet.forEach((feature, index) => { // See which feature results in best information gain
let buckets: number[][] = []; // Use to see gini impurity of each child after hypothetical split at current feature
for (let i = 0; i < feature.numSplit; i++) { // Initialize depending on how many categories feature divides into / how many class labels
let cur: number[] = [];
for (let j = 0; j < classLabelNum; j++){
cur.push(0);
}
buckets.push(cur);
}
node.data.forEach((datapoint) => { // Loop through data, increment bucket counters
buckets[feature.fn(datapoint)][classMap.get(datapoint[dataColNum - 1])]++;
});
let rawGini = 0;
let total = 0;
buckets.forEach(bucket => { // calculate raw (not normalized yet) gini impurity for choosing this feature
let weight = 0;
let gini = getGiniImpurity(bucket);
bucket.forEach(labelQuant => weight += labelQuant);
rawGini += gini*weight;
total += weight;
})
let totalGini = rawGini/total; // Normalize information gain
if ((node.gini - totalGini) > maxGainValue) { // Determine if current feature results in better information gain than previous features
maxGainValue = node.gini - totalGini;
maxGainIndex = index;
}
});
if (maxGainValue == 0) { // Case that impossible to gain more information, need to convert node to leaf
node.type = NodeType.leaf;
let buckets:number[] = [];
for (let i = 0; i < classLabelNum; i++) {
buckets.push(0);
}
node.data.forEach(line => buckets[classMap.get(line[dataColNum - 1])]++); // Increment counters to see current composition of class types
let majorityIndex = 0;
for (let i = 1; i < classLabelNum; i++) { // Determine majority class (Who current decision tree will vote for)
if (buckets[i] > buckets[majorityIndex]) {
majorityIndex = i;
}
}
node.vote = classArr[majorityIndex];
buckets.forEach((bucket, index) => { // Determine result (percentage, majority, etc) for debugging purposes
node.result.push({name: classArr[index], percentage: bucket/(node.data.length), number: bucket});
})
}
else { // Case that more information gain possible, create child node for each feature division, recursively call function on children
let childrenData: (number | string)[][][] = []; // Divide current dataset based on the feature splitter before give to children
for (let i = 0; i < featureSet[maxGainIndex].numSplit; i++) {
childrenData.push([]);
}
node.data.forEach(line => {
childrenData[featureSet[maxGainIndex].fn(line)].push(line);
});
let leftoverFeatures:feature[] = []; // Create feature subset with remaining features for children to use
featureSet.forEach((feature, index) => {
if (index != maxGainIndex) {
leftoverFeatures.push(feature);
}
else {
node.splitter = feature;
}
})
childrenData.forEach((newChildData, index) => { // Recursively call function on one child at a time
let child = new TreeNode({type: NodeType.nonTerminal, dataset: newChildData});
node.children.push(child); // link child to current node
buildTree(child, leftoverFeatures);
})
}
}
// This is just an example that is hardcoded in. In future will have function to automatically generate best split for features, not manual split.
let featureSetTest: feature[] = [
{
name: "sepal length",
fn: (params) => {
if (params[0] < 5)
{
return 0;
}
else if (params[0] < 6)
{
return 1;
}
else {
return 2;
}
},
numSplit: 3
},
{
name: "sepal width",
fn: (params) => {
if (params[1] < 3)
{
return 0;
}
else
{
return 1;
}
},
numSplit: 2
},
{
name: "petal length",
fn: (params) => {
if (params[2] < 2)
{
return 0;
}
else if (params[2] < 6)
{
return 1;
}
else {
return 2;
}
},
numSplit: 3
},
{
name: "petal width",
fn: (params) => {
if (params[3] < 1)
{
return 0;
}
else if (params[3] < 2)
{
return 1;
}
else {
return 2;
}
},
numSplit: 3
}
]
processData(.7,0,.3); // Read data to get basic info
// Train the random forest
let rf = new RandomForest({dataset: trainArr, featureSet: featureSetTest, numTrees: 100, numFeatures: Math.sqrt(featureSetTest.length)});
rf.createForest();
// Test the random forest
rf.fullTest(testArr);