[bc3db1]: / Ensemble Learning / AdaBoost / buildOneDStump.m

Download this file

41 lines (39 with data), 959 Bytes

 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
function stump = buildOneDStump(x, y, d, w)
[err_1, t_1] = searchThreshold(x, y, w, '>'); % > t_1 -> +1
[err_2, t_2] = searchThreshold(x, y, w, '<'); % < t_2 -> +1
stump = initStump(d);
if err_1 <= err_2
stump.threshold = t_1;
stump.error = err_1;
stump.less = -1;
stump.more = 1;
else
stump.threshold = t_2;
stump.error = err_2;
stump.less = 1;
stump.more = -1;
end
end
function [error, thresh] = searchThreshold(x, y, w, sign)
N = length(x);
err_n = zeros(N, 1);
y_predict = zeros(N, 1);
for n=1:N
switch sign
case '>'
idx = logical(x >= x(n));
y_predict(idx) = 1;
y_predict(~idx) = -1;
case '<'
idx = logical(x < x(n));
y_predict(idx) = 1;
y_predict(~idx) = -1;
end
err_label = logical(y ~= y_predict);
%sum(err_label)
err_n(n) = sum(err_label.*w)/sum(w);
end
[v, idx] = min(err_n);
error = v;
thresh = x(idx);
end