|
| 1 | +% Ensemble Tree (10/12/2020) |
| 2 | + |
| 3 | +function ET = mEnsembleTree(feat,label,opts) |
| 4 | +% Default |
| 5 | +num_split = 50; |
| 6 | +kfold = 10; |
| 7 | +tf = 2; |
| 8 | + |
| 9 | +if isfield(opts,'kfold'), kfold = opts.kfold; end |
| 10 | +if isfield(opts,'ho'), ho = opts.ho; end |
| 11 | +if isfield(opts,'tf'), tf = opts.tf; end |
| 12 | +if isfield(opts,'nSplit'), num_split = opts.nSplit; end |
| 13 | + |
| 14 | +% [Hold-out] |
| 15 | +if tf == 1 |
| 16 | + fold = cvpartition(label,'HoldOut',ho); |
| 17 | + % Call train & test data |
| 18 | + xtrain = feat(fold.training,:); ytrain = label(fold.training); |
| 19 | + xtest = feat(fold.test,:); ytest2 = label(fold.test); |
| 20 | + % Train model |
| 21 | + Temp = templateTree('MaxNumSplits',num_split); |
| 22 | + Model = fitcensemble(xtrain,ytrain,'Learners',Temp,... |
| 23 | + 'Method','AdaBoostM1'); |
| 24 | + % Test |
| 25 | + pred2 = predict(Model,xtest); |
| 26 | + % Accuracy |
| 27 | + Afold = sum(pred2 == ytest2) / length(ytest2); |
| 28 | + |
| 29 | +% [Cross-validation] |
| 30 | +elseif tf == 2 |
| 31 | + % [Cross-validation] |
| 32 | + fold = cvpartition(label,'KFold',kfold); |
| 33 | + Afold = zeros(kfold,1); |
| 34 | + pred2 = []; |
| 35 | + ytest2 = []; |
| 36 | + for i = 1:kfold |
| 37 | + % Call train & test data |
| 38 | + trainIdx = fold.training(i); testIdx = fold.test(i); |
| 39 | + xtrain = feat(trainIdx,:); ytrain = label(trainIdx); |
| 40 | + xtest = feat(testIdx,:); ytest = label(testIdx); |
| 41 | + % Train model |
| 42 | + Temp = templateTree('MaxNumSplits',num_split); |
| 43 | + Model = fitcensemble(xtrain,ytrain,'Learners',Temp,... |
| 44 | + 'Method','AdaBoostM1'); |
| 45 | + % Test |
| 46 | + pred = predict(Model,xtest); clear Model |
| 47 | + % Accuracy |
| 48 | + Afold(i) = sum(pred == ytest) / length(ytest); |
| 49 | + % Store temporary |
| 50 | + pred2 = [pred2(1:end); pred]; |
| 51 | + ytest2 = [ytest2(1:end); ytest]; |
| 52 | + end |
| 53 | + |
| 54 | +% [Leave one out] |
| 55 | +elseif tf == 3 |
| 56 | + fold = cvpartition(label,'LeaveOut'); |
| 57 | + % Size of data |
| 58 | + num_data = length(label); |
| 59 | + Afold = zeros(num_data,1); |
| 60 | + pred2 = []; |
| 61 | + ytest2 = []; |
| 62 | + for i = 1:num_data |
| 63 | + % Call train & test data |
| 64 | + trainIdx = fold.training(i); testIdx = fold.test(i); |
| 65 | + xtrain = feat(trainIdx,:); ytrain = label(trainIdx); |
| 66 | + xtest = feat(testIdx,:); ytest = label(testIdx); |
| 67 | + % Train model |
| 68 | + Temp = templateTree('MaxNumSplits',num_split); |
| 69 | + Model = fitcensemble(xtrain,ytrain,'Learners',Temp,... |
| 70 | + 'Method','AdaBoostM1'); |
| 71 | + % Test |
| 72 | + pred = predict(Model,xtest); clear Model |
| 73 | + % Accuracy |
| 74 | + Afold(i) = sum(pred == ytest) / length(ytest); |
| 75 | + % Store temporary |
| 76 | + pred2 = [pred2(1:end); pred]; |
| 77 | + ytest2 = [ytest2(1:end); ytest]; |
| 78 | + end |
| 79 | +end |
| 80 | +% Confusion matrix |
| 81 | +confmat = confusionmat(ytest2,pred2); |
| 82 | +% Overall accuracy |
| 83 | +acc = mean(Afold); |
| 84 | +% Store |
| 85 | +ET.acc = acc; |
| 86 | +ET.con = confmat; |
| 87 | + |
| 88 | +if tf==1 |
| 89 | + fprintf('\n Accuracy (ET-HO): %g %%',100 * acc); |
| 90 | +elseif tf == 2 |
| 91 | + fprintf('\n Accuracy (ET-CV): %g %%',100 * acc); |
| 92 | +elseif tf == 3 |
| 93 | + fprintf('\n Accuracy (ET-LOO): %g %%',100 * acc); |
| 94 | +end |
| 95 | +end |
| 96 | + |
0 commit comments