-
Notifications
You must be signed in to change notification settings - Fork 1
/
adaboost.m
89 lines (65 loc) · 1.99 KB
/
adaboost.m
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
function [mu, sigma, p, alpha, classes] = adaboost( data, t )
%ADABOOST Summary of this function goes here
% Detailed explanation goes here
%
%
%
%
class = data(:,end);
classes = unique(class);
nr_classes = length(classes);
features = data(:,1:end-1);
[m, n] = size(data);
% Pre allocate return variables
alpha = ones(t, 1);
p = ones(t, nr_classes);
mu = ones(n-1, nr_classes, t);
sigma = ones(n-1, nr_classes, t);
% Optional ?
error = zeros(t, 1);
% Initiate default weight wector
w = ones(m, 1) ./ sum(m);
% Call bayes_weight repeatedly
for i=1:t
% Use the new adjusted weights as input
% w_ = w;
%
% Calculate the hypothesis and prior using new weights w_
p_ = prior(data, w);
[mu_, sigma_] = bayes_weight(data, w);
% Call the discriminant and predict the values
g = discriminant(features, mu_, sigma_, p_);
% Find the maximum of each feature
% dummy contains the max(g(:,x), g(:,y))
% class_ contains either x or y depending on which is greatest
[~, class_] = max(g, [], 2);
class_ = class_ - 1;
% 1 iff correctly classified, 0 otherwise
delta = (class_ == class);
% Index of the incorrect and correct classified features
correct = find(delta == 1);
incorrect = find(delta == 0);
% Compute the error with respect to w
e = 1.0 - (delta' * w);
% Compute the alpha value
a = 0.5*log( (1-e) / e );
w_next = w;
% Re-compute all the weights for w(t+1) to match the current classification
% Z is a normalizing constant
w_next(correct) = w(correct) .* exp(-a);
%Z = sum(w_(correct));
%w(correct) = w_(correct) ./ Z;
w_next(incorrect) = w(incorrect) .* exp(a);
%Z = sum(w_(incorrect));
%w(incorrect) = w_(incorrect) ./ Z;
Z = sum(w_next);
w_next = w_next ./ Z;
% Update all the variables
w = w_next;
mu(:,:,i) = mu_;
sigma(:,:,i) = sigma_;
error(i, 1) = e;
alpha(i, 1) = a;
p(i, :) = p_;
end
end