Adaboost: Help on matlab code error
显示 更早的评论
function [tree_node_left, tree_node_right, split_error] = ...
do_learn_nu(tree_node, dataset, labels, weights, papa)
tree_node_left = tree_node;
tree_node_right = tree_node;
if(nargin > 4)
tree_node_left.parent = papa;
tree_node_right.parent = papa;
end
Distr = weights;
trainpat = dataset;
traintarg = labels;
tr_size = size(trainpat, 2);
T_MIN = zeros(3,size(trainpat,1));
d_min = 1;
d_max = size(trainpat,1);
for d = d_min : d_max;
[DS, IX] = sort(trainpat(d,:));
TS = traintarg(IX);
DiS = Distr(IX);
lDS = length(DS);
vPos = 0 * TS;
vNeg = vPos;
i = 1;
j = 1;
while i <= lDS
k = 0;
while i + k <= lDS && DS(i) == DS(i+k)
if(TS(i+k) > 0)
vPos(j) = vPos(j) + DiS(i+k);
else
vNeg(j) = vNeg(j) + DiS(i+k);
end
k = k + 1;
end
i = i + k;
j = j + 1;
end
vNeg = vNeg(1:j-1);
vPos = vPos(1:j-1);
Error = zeros(1, j - 1);
InvError = Error;
IPos = vPos;
INeg = vNeg;
for i = 2 : length(IPos)
IPos(i) = IPos(i-1) + vPos(i);
INeg(i) = INeg(i-1) + vNeg(i);
end
Ntot = INeg(end);
Ptot = IPos(end);
for i = 1 : j - 1
Error(i) = IPos(i) + Ntot - INeg(i);
InvError(i) = INeg(i) + Ptot - IPos(i);
end
idx_of_err_min = find(Error == min(Error));
if(length(idx_of_err_min) < 1)
idx_of_err_min = 1;
end
if(length(idx_of_err_min) <1)
idx_of_err_min = idx_of_err_min;
end
idx_of_err_min = idx_of_err_min(1);
idx_of_inv_err_min = find(InvError == min(InvError));
if(length(idx_of_inv_err_min) < 1)
idx_of_inv_err_min = 1;
end
idx_of_inv_err_min = idx_of_inv_err_min(1);
if(Error(idx_of_err_min) < InvError(idx_of_inv_err_min))
T_MIN(1,d) = Error(idx_of_err_min);
T_MIN(2,d) = idx_of_err_min;
T_MIN(3,d) = -1;
else
T_MIN(1,d) = InvError(idx_of_inv_err_min);
T_MIN(2,d) = idx_of_inv_err_min;
T_MIN(3,d) = 1;
end
end
dim = [];
best_dim = find(T_MIN(1,:) == min(T_MIN(1,:)));
dim = best_dim(1);
tree_node_left.dim = dim;
tree_node_right.dim = dim;
TDS = sort(trainpat(dim,:));
lDS = length(TDS);
DS = TDS * 0;
i = 1;
j = 1;
while i <= lDS
k = 0;
while i + k <= lDS && TDS(i) == TDS(i+k)
DS(j) = TDS(i);
k = k + 1;
end
i = i + k;
j = j + 1;
end
DS = DS(1:j-1);
split = (DS(T_MIN(2,dim)) + DS(min(T_MIN(2,dim) + 1, length(DS)))) / 2;
%
split_error = T_MIN(1,dim);
%
tree_node_left.right_constrain = split;
tree_node_right.left_constrain = split;
I have dataset D x N and label 1 x N. And when I call this function by
load databaseAB
MaxIter = 100; % boosting iterations
% Step2: splitting data to training and control set
TrainData = ALoad(:,1:2:end);
TrainLabels = BLoad(1:2:end);
ControlData = ALoad(:,2:2:end);
ControlLabels = BLoad(2:2:end);
% Step3: constructing weak learner
weak_learner = tree_node_w(3); % pass the number of tree splits to the constructor
%
% Step4: training with Gentle AdaBoost
[RLearners RWeights] = RealAdaBoost(weak_learner, TrainData, TrainLabels, MaxIter);
%
% Step6: evaluating on control set
ResultR = sign(Classify(RLearners, RWeights, ControlData))
%
% Step7: calculating error
ErrorR = sum(ControlLabels ~= ResultR) / length(ControlLabels);
gave me this error
Undefined function 'sort' for input arguments of type 'struct'.
Error in tree_node_w/do_learn_nu (line 65)
[DS, IX] = sort(trainpat(d,:));
Any one can help me for this error? Thank you.
2 个评论
per isakson
2016-10-11
There is no function, sort, for structures.
Guesses:
- trainpat is not intended to be a struct
- the intention is to sort regarding a field of the structure, trainpat.
- other mistake
Tuffahatul Ummah
2016-10-13
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Guidance, Navigation, and Control (GNC) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!