-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrf.html
More file actions
277 lines (234 loc) · 6.32 KB
/
rf.html
File metadata and controls
277 lines (234 loc) · 6.32 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
---
layout: base2
title: Random Forest
permalink: /rf.html
---
<style>
svg {
vertical-align: top;
height: 250px;
width: 400px;
cursor: default;
user-select: none;
}
canvas {
cursor: pointer;
}
.content-container {
text-align: center;
}
.content-container-row {
text-align: center;
}
.content-container-cell {
display: inline-block;
}
.control-panel {
padding-top: 10px;
padding-bottom: 10px;
padding-right: 0px;
padding-left: 0px;
}
.control-row {
margin-bottom: 7px;
}
.control-cell {
text-align: center;
}
.control-cell-full {
text-align: center;
}
#data-canvas, .loss-plot-canvas {
box-shadow: 0px 1px 5px rgba(0, 0, 0, 0.5);
}
#data-canvas {
margin-top: 10px;
margin-left: 20px;
margin-right: 20px;
width: 250px;
height: 250px;
min-width: 250px;
}
.loss-plot-canvas {
width: 500px;
height: 100px;
cursor: default;
background-color: var(--headbackcol);
}
@media screen and (max-width: 500px) {
.loss-plot-canvas {
width: 300px;
height: 50px;
}
}
</style>
<link type="text/css" href="assets/css/jquery-ui-1.8.21.custom.css" rel="Stylesheet" />
<script type="text/javascript" src="assets/js/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="assets/js/jquery-ui-1.8.21.custom.min.js"></script>
<script src="assets/js/npgmain.js"></script>
<script src="assets/js/randomforest.js"></script>
<script type="text/javascript">
var N= 10; //number of data points
var data = new Array(N);
var labels= new Array(N);
var tree= new forestjs.RandomForest();
var dirty= true;
var ss= 50.0; // scaling factor for drawing
var density= 10; // density of drawing. Decrease for higher resolution (but slower)
var avgerr= 0;
var drawSoft = true;
var options = {};
options.type = 1;
function myinit(){
data[0]=[-0.4326 , 1.1909 ];
data[1]= [1.5, 3.0];
data[2]=[0.1253 , -0.0376 ];
data[3]=[0.2877 , 0.3273 ];
data[4]=[-1.1465 , 0.1746 ];
data[5]=[1.8133 , 2.1139 ];
data[6]=[2.7258 , 3.0668 ];
data[7]=[1.4117 , 2.0593 ];
data[8]=[4.1832 , 1.9044 ];
data[9]=[1.8636 , 1.1677 ];
labels[0]= 1;
labels[1]= 1;
labels[2]= 1;
labels[3]= 1;
labels[4]= 1;
labels[5]= -1;
labels[6]= -1;
labels[7]= -1;
labels[8]= -1;
labels[9]= -1;
data[10]=[-0.5 , -0.5 ];
labels[10]= -1;
data[11]=[1.0 , 2.0 ];
labels[11]= 1;
data[12]=[1.0 , -1.0 ];
labels[12]= 1;
N= data.length;
retrain();
}
function retrain(){
tree.train(data, labels, options);
dirty= true;
}
function update(){
}
function draw(){
if(!dirty) return;
ctx.clearRect(0,0,WIDTH,HEIGHT);
// draw decisions in the grid
for(var x=0.0; x<=WIDTH; x+= density) {
for(var y=0.0; y<=HEIGHT; y+= density) {
var dec= tree.predictOne([(x-WIDTH/2)/ss, (y-HEIGHT/2)/ss]);
if(!drawSoft) {
if(dec > 0.5) ctx.fillStyle = 'rgb(150,250,150)';
else ctx.fillStyle = 'rgb(250,150,150)';
} else {
var ri= 250*(1-dec) + 150*dec;
var gi= 250*dec + 150*(1-dec);
ctx.fillStyle = 'rgb('+Math.floor(ri)+','+Math.floor(gi)+',150)';
}
ctx.fillRect(x-density/2-1, y-density-1, density+2, density+2);
}
}
// draw axes
ctx.beginPath();
ctx.strokeStyle = 'rgb(50,50,50)';
ctx.lineWidth = 1;
ctx.moveTo(0, HEIGHT/2);
ctx.lineTo(WIDTH, HEIGHT/2);
ctx.moveTo(WIDTH/2, 0);
ctx.lineTo(WIDTH/2, HEIGHT);
ctx.stroke();
// draw datapoints.
ctx.strokeStyle = 'rgb(0,0,0)';
for(var i=0;i<N;i++) {
if(labels[i]==1) ctx.fillStyle = 'rgb(100,200,100)';
else ctx.fillStyle = 'rgb(200,100,100)';
drawCircle(data[i][0]*ss+WIDTH/2, data[i][1]*ss+HEIGHT/2, 5);
}
ctx.fillStyle= 'rgb(0,0,0)';
dirty= false;
}
function mouseClick(x, y, shiftPressed){
// add datapoint at location of click
data[N] = [(x-WIDTH/2)/ss, (y-HEIGHT/2)/ss];
labels[N] = shiftPressed ? 1 : -1;
N += 1;
retrain();
}
function keyUp(key){
if(key==82) { // 'r'
retrain();
}
}
function keyDown(key){
}
// UI stuff
function refreshTrees(event, ui) {
var numTrees = Math.floor(ui.value);
$("#treesreport").text("number of trees = " + numTrees);
options.numTrees= numTrees;
retrain();
}
function refreshDepth(event, ui) {
var maxDepth = Math.floor(ui.value);
$("#depthreport").text("max depth = " + maxDepth);
options.maxDepth = maxDepth;
retrain();
}
function refreshTries(event, ui) {
var tries = Math.floor(ui.value);
$("#triesreport").text("hypotheses / node = " + tries);
options.numTries = tries;
retrain();
}
$(function() {
$("#slider1").slider({
orientation: "horizontal",
slide: refreshTrees,
max: 200,
min: 1,
step: 1,
value: 100
});
$("#slider2").slider({
orientation: "horizontal",
slide: refreshDepth,
max: 12,
min: 2,
step: 1,
value: 4
});
$("#slider3").slider({
orientation: "horizontal",
slide: refreshTries,
max: 50,
min: 1,
step: 1,
value: 10
});
});
</script>
<body onLoad="NPGinit(10);" class="text-center">
<p>
<b><i class="fas fa-mouse"></i></b> Red <space style="padding:.5rem"></space>
<b>SHIFT + <i class="fas fa-mouse"></i></b> Green <space style="padding:.5rem"></space>
<b>R</b> Retrain
</p>
<canvas id="NPGcanvas" width="330" height="330">Browser not supported.</canvas><br /><br />
<center>
<button type="button" class="btn btn-new btn-sm" onclick="options.type = 1-options.type; retrain();">1D vs 2D</button>
<button type="button" class="btn btn-new btn-sm" onclick="drawSoft= !drawSoft; dirty= true;">Hard vs Soft</button>
<span style="display:block; height: 20px;"></span>
<div id="optsdiv">
<div style="width:300px; margin-left: 5px;"><div id="slider1"></div><br/><span id="treesreport"> # of trees = 100</span></div><br>
<div style="width:300px; margin-left: 5px;"><div id="slider2"></div><br/><span id="depthreport"> Max depth = 4</span></div><br>
<div style="width:300px; margin-left: 5px;"><div id="slider3"></div><br/><span id="triesreport"> Hypotheses per node = 10</span></div><br>
</center>
<br />
<p>Credit to @karpathy.</p>
</div>
</body>