0001
0002
0003
0004
0005
0006
0007
0008 datasetName = 'iris';
0009
0010
0011
0012
0013
0014
0015 load(strcat(datasetName, '_dataset'));
0016 eval(sprintf('x = %sInputs;', datasetName));
0017 eval(sprintf('y = %sTargets;', datasetName));
0018 x = x';
0019 y = y';
0020 numClasses = size(y, 2);
0021 [~,y] = max(y,[],2);
0022
0023 numFeatures = size(x, 2);
0024 numInstances = size(x, 1);
0025
0026
0027 disp(['Dataset Name ' datasetName]);
0028 disp(['Number of Classes ' num2str(numClasses)]);
0029 disp(['Number of Instances ' num2str(numInstances)]);
0030 disp(['Number of Features ' num2str(numFeatures)]);
0031
0032
0033
0034
0035 fprintf('===========\n');
0036 fprintf('Basic Usage\n');
0037 fprintf('===========\n');
0038
0039
0040
0041
0042 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0043
0044
0045 fprintf('\tTraining Classifier for 3 iterations\n');
0046 [radacl, learnErr] = learn(radacl, x, y, 3);
0047 fprintf('\tLearning Error %f\n', learnErr);
0048
0049 fprintf('\t-----\n');
0050 fprintf('\tTesting Classifier\n');
0051 outs = computeOutputs(radacl, x);
0052
0053 err = sum(outs ~= y) / numInstances;
0054 fprintf('\tLearning Error %f\n', err);
0055
0056
0057 fprintf('\t[predicted outputs : correct outputs]\n');
0058 fprintf('\t\t%d\t\t%d\t\n',outs(1:5, :), y(1:5 , :));
0059
0060
0061
0062 fprintf('=============\n');
0063 fprintf('Stage Details\n');
0064 fprintf('=============\n');
0065
0066 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0067
0068 radacl = learn(radacl, x, y, 3, true);
0069 fprintf('\tLearning Error %f\n', learnErr);
0070
0071
0072
0073 fprintf('==================\n');
0074 fprintf('Display Classifier\n');
0075 fprintf('==================\n');
0076
0077 fprintf('\tDisplay Classifier Before Learning\n\t===>\n');
0078 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0079 display(radacl);
0080 fprintf('\t<===\n');
0081
0082 fprintf('\tDisplay Classifier After Learning\n===>\n');
0083 radacl = learn(radacl, x, y, 2);
0084 display(radacl);
0085 fprintf('\t<===\n');
0086
0087
0088
0089 fprintf('================\n');
0090 fprintf('Iterations Error\n');
0091 fprintf('================\n');
0092
0093 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0094
0095 fprintf('\tTraining Classifier for 10 iterations\n');
0096 [radacl, learnErr, iterationsErrors] = learn(radacl, x, y, 10);
0097 fprintf('\tLearning Error %f\n', learnErr);
0098
0099 fprintf('\t------------\n');
0100 fprintf('\tPlotting Iteration Errors\n');
0101 plot(1:length(iterationsErrors), iterationsErrors);
0102 ylim([0 1]);
0103
0104
0105
0106 fprintf('========================\n');
0107 fprintf('Add More Boosting Stages\n');
0108 fprintf('========================\n');
0109
0110 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0111
0112 fprintf('\tTraining Classifier for 5 iterations\n');
0113 radacl = learn(radacl, x, y, 5);
0114 fprintf('\t------------\n');
0115 fprintf('\tTraining Classifier for 5 more iterations\n');
0116 [radacl, learnErr] = learn(radacl, x, y, 10);
0117 fprintf('\tLearning Error %f\n', learnErr);
0118
0119
0120
0121
0122 fprintf('===============================\n');
0123 fprintf('Using Different Weak Classifier\n');
0124 fprintf('===============================\n');
0125
0126 radacl = RealAdaBooster(DecisionTreeClassifier(numClasses));
0127
0128
0129 fprintf('\tTraining Classifier for 3 iterations\n');
0130 [radacl, learnErr] = learn(radacl, x, y, 3);
0131 fprintf('\tLearning Error %f\n', learnErr);
0132
0133
0134
0135
0136 fprintf('=======================================\n');
0137 fprintf('Save and Load Classifier to/from a file\n');
0138 fprintf('=======================================\n');
0139
0140 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0141 [radacl, learnErr] = learn(radacl, x, y, 1);
0142 fprintf('\tLearning Error %f\n', learnErr);
0143 saveToFile(radacl, 'test2.bin');
0144 radacl2 = loadFromFile(RealAdaBooster,'test2.bin');
0145
0146 outs1 = computeOutputs(radacl, x);
0147 err1 = sum(outs1 ~= y) / numInstances;
0148 outs2 = computeOutputs(radacl2, x);
0149 err2 = sum(outs2 ~= y) / numInstances;
0150
0151 fprintf('\tError Before Save %f, Error After Save %f\n', err1, err2);
0152
0153
0154
0155 fprintf('===================================================\n');
0156 fprintf('Add Boost Stages till reaching a required Err Bound\n');
0157 fprintf('===================================================\n');
0158
0159 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0160 [radacl, learnErr] = learn(radacl, x, y, Inf, true, '', '', NaN, 0.02);
0161 fprintf('\tLearning Error %f\n', learnErr);