clear;clc;
%----------------------------------------------
% training data (9)
feature_train_all = [2 1 1;
                     2 0 1;
                     1 1 1;
                     0 1 0;
                     0 0 1;
                     0 1 0;
                     1 0 0;
                     1 0 1;
                     1 1 1];

target_train_all    = {'Party';
                       'Study';
                       'Party';
                       'Party';
                       'Pub';
                       'Party';
                       'Study';
                       'TV';
                       'Party'};     % class
%----------------------------------------------

%----------------------------------------------
% test data
feature_test_all = [2 0 0];
target_test_all  = {'Study'};
%----------------------------------------------

%----------------------------------------------
% para for bagging
num_samples_all = 9;          % number of training samples totally
num_bootstrap = 10;            % number of bootstrap times
num_samples_in_bootstrap = 5;  % number of samples in each bootstrap
%----------------------------------------------

%---- classifiers -----
tree_bootstrap = cell(1,num_bootstrap);

%----------------------------------------------
% learn classifiers (trees in each bootstrap)

for i_bootstrap = 1:num_bootstrap
    
    % ---- randomly select # samples ----
    id_random = randperm(num_samples_all);
    id_select = id_random(1:num_samples_in_bootstrap);
    
    feature_train_current = feature_train_all(id_select,:);
    target_train_current  = target_train_all(id_select,:);
    
    % ---- using these samples to train a classifier (tree) ----
    tree_current = classregtree(feature_train_current,target_train_current,...
                        'method','classification',...
                        'categorical',1:4,...
                        'names',{'Party'    'Deadline'    'Lazy'},...
                        'minleaf',1,...
                        'minparent',1);
    
    % ---- save current classifier (tree) ----
    tree_bootstrap{i_bootstrap} = tree_current;  
    
%     view(tree_bootstrap{i_bootstrap})
end


%--------------------------------------------------------------------------------
% test

%---- test results -----
results = cell(1,num_bootstrap);
for i_bootstrap = 1:num_bootstrap
    
    % ---- load current classifier ----
    tree_current = tree_bootstrap{i_bootstrap};
    
    % ---- test using current classifier ----
    result_current = eval(tree_current,feature_test_all);
    
    results{i_bootstrap} = result_current;
end
%--------------------------------------------------------------------------------

% ---- disp all the result ----
for i=1:num_bootstrap
    fprintf('result of %d th classifer is %s \n',i,char(results{i}));
end

