load huppa;
x=x(:,45:end)

Labels = char(Ref{2});
y = double(Labels);
numel(find(y == 78));
% 78 = N
numel(find(y == 79));
% 79 = O
numel(find(y == 65));
% 65 = A
numel(find(y == 126));
% 126 = ~

y(y==78) = 1;
y(y==79) = 1;
y(y==65) = 1;
y(y==126) = 0;
% split the labels in signal and noise
Labels(y==1)='s';
Labels(y==0)='~';

% we will train with the full noise dataset, we can later decide to a
% priori undersample the rest

% fit the ensemble with deep trees
t = templateTree('MinLeafSize',5);
tic
noiseTree = fitensemble(x,Labels,'RUSBoost',1000,t,...
    'LearnRate',0.1,'nprint',100);
toc

% Plot the classification error against the number of members in the ensemble
figure;
tic
plot(loss(noiseTree,x,Labels,'mode','cumulative'));
toc
grid on;
xlabel('Number of trees');
ylabel('Test classification error');

% The ensemble is large. Remove the data using the compact method.
cmpctNoise = compact(noiseTree);

sz(1) = whos('noiseTree');
sz(2) = whos('cmpctNoise');
[sz(1).bytes sz(2).bytes]

% Remove half the trees from cmpctRus. This action is likely to have minimal effect on the predictive performance, based on the observation that 400 out of 1000 trees give nearly optimal accuracy.
cmpctRus = removeLearners(cmpctNoise,[500:1000]);
sz(3) = whos('cmpctNoise');
sz(3).bytes

