Switch to unified view

a b/Ensemble Learning/AdaBoost/buildOneDStump.m
1
function stump = buildOneDStump(x, y, d, w)
2
[err_1, t_1] = searchThreshold(x, y, w, '>'); % > t_1 -> +1
3
[err_2, t_2] = searchThreshold(x, y, w, '<'); % < t_2 -> +1
4
stump = initStump(d);
5
if err_1 <= err_2
6
    stump.threshold = t_1;
7
    stump.error = err_1;
8
    stump.less = -1;
9
    stump.more = 1;
10
else
11
    stump.threshold = t_2;
12
    stump.error = err_2;
13
    stump.less = 1;
14
    stump.more = -1;
15
end
16
end
17
18
function [error, thresh] = searchThreshold(x, y, w, sign)
19
N = length(x);
20
err_n = zeros(N, 1);
21
y_predict = zeros(N, 1);
22
for n=1:N
23
    switch sign
24
        case '>'
25
            idx = logical(x >= x(n));
26
            y_predict(idx) = 1;
27
            y_predict(~idx) = -1;
28
        case '<'
29
            idx = logical(x < x(n));
30
            y_predict(idx) = 1;
31
            y_predict(~idx) = -1;
32
    end
33
    err_label = logical(y ~= y_predict);
34
    %sum(err_label)
35
    err_n(n) = sum(err_label.*w)/sum(w);
36
end
37
[v, idx] = min(err_n);
38
error = v;
39
thresh = x(idx);
40
end