-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggregate_rf_test.html
207 lines (181 loc) · 8.43 KB
/
aggregate_rf_test.html
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
<!DOCTYPE html>
<meta charset="utf-8">
<style>
</style>
<body>
<script src="http://d3js.org/d3.v4.min.js"></script>
<script>
var height = 400;
var width = 1700;
d3.select("body").append("h2").text("# Nodes Splitting on Feature");
var svg = d3.select("body").append("svg").attr("width", width).attr("height", 500);
d3.select("body").append("h2").text("Average Feature Importance");
var svg2 = d3.select("body").append("svg").attr("width", width).attr("height", 500);
function make_hist(ft_number, nodes, features) {
var hist_data = get_thresholds(features[ft_number], nodes);
d3.select("body").append("h3").text(features[ft_number]+" - % of nodes splitting on threshold")
var svg_hist = d3.select("body").append("svg").attr("width", 800).attr("height", 300);
var hist_x = d3.scaleLinear()
.domain(d3.extent(hist_data))
.rangeRound([50, 750]).nice();
var hist_y = d3.scaleLinear().range([275, 25]).domain([0, 0.5]); // truncate unbalanced distributions
var n = hist_data.length,
bins = d3.histogram().domain(hist_x.domain()).thresholds(50)(hist_data),
density = kernelDensityEstimator(kernelEpanechnikov(3), hist_x.ticks(50))(hist_data),
mean = d3.mean(hist_data);
// draw boxes - errors are expected because some features were never selected to split on
svg_hist.selectAll("rect")
.data(bins)
.enter().append("rect")
.attr("fill", "silver")
.attr("x", function (d) {
return hist_x(d.x0);
})
.attr("y", function (d) {
return hist_y(d.length / n);
})
.attr("width", function (d) {
return hist_x(d.x1) - hist_x(d.x0)+1;
})
.attr("height", function (d) {
return hist_y(0) - hist_y(d.length / n);
});
// draw density estimate line
svg_hist.append("path")
.datum(density)
.attr("fill", "none")
.attr("stroke", "gray")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("d", d3.line()
.curve(d3.curveBasis)
.x(function (d) {
return hist_x(d[0]);
})
.y(function (d) {
return hist_y(d[1]);
}));
// add the x Axis
svg_hist.append("g")
.call(d3.axisBottom(hist_x)).attr("transform", "translate(0,275)");
svg_hist.append("line")
.attr("x1", hist_x(mean)) //<<== change your code here
.attr("y1", 25)
.attr("x2", hist_x(mean)) //<<== and here
.attr("y2", 275)
.style("stroke-width", 1)
.style("stroke", "steelblue")
.style("fill", "none");
// add the y Axis
svg_hist.append("g").call(d3.axisLeft(hist_y)).attr("transform", "translate(50,0)");
}
// following two functions borrowed from: http://bl.ocks.org/jensgrubert/7777399
function kernelDensityEstimator(kernel, X) {
return function (V) {
return X.map(function (x) {
return [x, d3.mean(V, function (v) {
return kernel(x - v);
})];
});
};
}
function kernelEpanechnikov(k) {
return function (v) {
return Math.abs(v /= k) <= 1 ? 0.75 * (1 - v * v) / k : 0;
};
}
function get_thresholds(ft_name, nodes) {
return nodes.filter(n => n['feature'] === ft_name).map(n => n['threshold']);
}
d3.json("test_rf_dump.json",
function (error, data) {
// build list of nodes
var nodes = [];
data["trees"].forEach(tree => {
Object.entries(tree["nodes"]).forEach(([id, node]) => {
nodes.push(node);
});
});
var features = data["feature_names"];
// how many nodes split on each feature
var nodes_ft_count = {};
features.forEach(ftname => {
nodes_ft_count[ftname] = 0;
});
nodes.forEach(node => {
nodes_ft_count[node["feature"]] += 1;
});
nodes_ft_count_data = [];
Object.entries(nodes_ft_count).forEach(
([key, value]) => nodes_ft_count_data.push({
"feature": key,
"node_count": value
})
);
// average importance score of each feature across all trees in the rf
var avg_ft_importance = data["trees"].map(t => t.feature_importances).reduce(function (r, a) {
a.forEach(function (b, i) {
r[i] = (r[i] || 0) + b;
});
return r;
}, []).map(f => f / (data["trees"].length));
var ft_importance_data = [];
for (var i = 0; i < features.length; i++) {
ft_importance_data.push({
"feature": features[i],
"importance": avg_ft_importance[i]
});
}
// graph 1
var ft_barScale = d3.scaleBand()
.domain(nodes_ft_count_data.map(d => d.feature))
.range([100, width / 2])
.padding(0.1);
var ft_yScale = d3.scaleLinear()
.domain([0, 15000]).nice() //truncate COS_TRIGRAMS_FST which has 50k
.range([height - 25, 25]);
svg.append("g")
.attr("fill", "steelblue")
.selectAll("rect").data(nodes_ft_count_data).enter().append("rect")
.attr("x", d => ft_barScale(d.feature))
.attr("y", d => ft_yScale(d.node_count))
.attr("height", d => ft_yScale(0) - ft_yScale(d.node_count))
.attr("width", ft_barScale.bandwidth());
svg.append("g").attr("transform", "translate(0," + (height - 25).toString() + ")")
.call(d3.axisBottom(ft_barScale).tickSizeOuter(0))
.selectAll("text")
.attr("y", 0)
.attr("x", 9)
.attr("dy", ".35em")
.attr("transform", "rotate(90)")
.style("text-anchor", "start");
svg.append("g").attr("transform", "translate(100,0)")
.call(d3.axisLeft(ft_yScale).tickSizeOuter(0));
//graph 2
var importance_yScale = d3.scaleLinear()
.domain([0, d3.max(ft_importance_data, d => d.importance)]).nice()
.range([height - 25, 25]);
svg2.append("g")
.attr("fill", "steelblue")
.selectAll("rect").data(ft_importance_data).enter().append("rect")
.attr("x", d => ft_barScale(d.feature))
.attr("y", d => importance_yScale(d.importance))
.attr("height", d => importance_yScale(0) - importance_yScale(d.importance))
.attr("width", ft_barScale.bandwidth());
svg2.append("g").attr("transform", "translate(0," + (height - 25).toString() + ")")
.call(d3.axisBottom(ft_barScale).tickSizeOuter(0))
.selectAll("text")
.attr("y", 0)
.attr("x", 9)
.attr("dy", ".35em")
.attr("transform", "rotate(90)")
.style("text-anchor", "start");
svg2.append("g").attr("transform", "translate(100,0)")
.call(d3.axisLeft(importance_yScale).tickSizeOuter(0));
//make histogram of thresholds for all features
for (var i = 0; i < features.length; i++) {
make_hist(i, nodes, features);
}
});
</script>
</body>