-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
171 lines (135 loc) · 4.75 KB
/
index.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
var _ = require('underscore');
/**
* Calculates the log to the base of 2
*/
function log2(val) {
if (val <= 0) {
return 0;
}
return Math.log(val)/Math.log(2);
}
/**
* Calculates the entropy of an attribute
*/
function entropy(samples, attribute) {
// Get distinct values of attribute out of the samples
var distinctValues = extractDistinctValues(samples, attribute);
// For each distinct values
return _.map(distinctValues, function (value) {
// Get the samples where the requested attribute
// has the value of the current iteration
var samplesOfValue = _.filter(samples, function (sample) {
return sample[attribute] === value;
});
// Get the ratio of the samples where the requested attribute
// has the value of the current iteration
// and the total samples length
var p = (samplesOfValue.length / samples.length);
return - p * log2(p);
})
// Sum everything up
.reduce(function (sum, elm) { return sum + elm });
}
function gain(samples, attribute, targetAttribute) {
// Extract distinct values for attribute
var distinctValues = extractDistinctValues(samples, attribute);
// Calculate entropy of target attribute
var entropyTargetAttribute = entropy(samples, targetAttribute);
// Calculate information gain of the requested attribute
// regarding the target attribute
return entropyTargetAttribute - _.map(distinctValues, function (value) {
// Get all samples where the requested attribute has the current value
var samplesOfValue = _.filter(samples, function (sample) {
return sample[attribute] === value
});
// Multipy the weight by the entropy of the target attribute out of
// the samples where the requested attribute has the current value
return (samplesOfValue.length/samples.length) * entropy(samplesOfValue, targetAttribute);
})
// Sum everything up
.reduce(function (sum, elm) { return sum + elm });
}
function extractDistinctValues(samples, attribute) {
return _.uniq(samples, function (sample) {
return sample[attribute]
}).map(function (sample) {
return sample[attribute];
});
}
function bestAttribute(samples, attributes, targetAttribute) {
// Determines the information gain for every attribute
var attributeGains = _.map(attributes, function (attribute) {
return {
attribute: attribute,
gain: gain(samples, attribute, targetAttribute)
};
})
// Return the attribute with the maximum information gain
return _.max(attributeGains, function (attributeGain) {
return attributeGain.gain;
}).attribute;
}
function isPure(samples, targetAttribute) {
return extractDistinctValues(samples, targetAttribute).length === 1;
}
function predict(node, sample) {
// If the node contains info about the
// target attribute
if (node.targetAttribute) {
// this node is a result and should be returned
return node;
}
// Node is not a result
// So, find the edge with the sample's attribute
// value
var edge = _.find(node.edges, function (edge) {
return edge.value === sample[node.attribute];
});
// Check if the edge's child node
// has information about a target attribute
return predict(edge.node, sample);
}
function createDecisionTree(samples, attributes, targetAttribute) {
// Parameter checks
if (samples.length === 0) throw Error("Empty samples set provided");
if (attributes.length === 0) throw Error("Empty attributes set provided");
if (!targetAttribute) throw Error('No target attribute provided');
var root = {};
root.predict = function (sample) {
return predict(root, sample);
};
// Recursion abort criterias
if (attributes.length === 1) {
root.attribute = attributes[0];
return root;
}
if (isPure(samples, targetAttribute)) {
return {
targetAttribute: targetAttribute,
value: samples[0][targetAttribute]
};
}
// Find best attribute
var best = bestAttribute(samples, attributes, targetAttribute);
// Create root element out of best attribute
root.attribute = best;
// Create edges to child nodes
root.edges = [];
var distinctValues = extractDistinctValues(samples, root.attribute);
_.each(distinctValues, function (value) {
var edge = {};
edge.value = value;
// The new sample list is the list where the current attribute
// has the given value
var newSamples = _.filter(samples, function (sample) {
return sample[root.attribute] === value;
});
// Reduce the number of attributes to consider
var newAttributes = _.without(attributes, root.attribute);
// Recursive call
edge.node = createDecisionTree(newSamples, newAttributes, targetAttribute);
root.edges.push(edge);
});
return root;
}
module.exports = createDecisionTree;