Home > demos > realada_demo.m

realada_demo

PURPOSE ^

% RealAda Classifier Demo

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

% RealAda Classifier Demo
 Several ways to use RealAda Boosting Algorithm classifier
%
% Load dataset

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 %% RealAda Classifier Demo
0002 % Several ways to use RealAda Boosting Algorithm classifier
0003 %%
0004 %% Load dataset
0005 
0006 % Name of matlab dataset
0007 % Available names : [cancer, wine, iris, crab, glass, simpleclass, thyroid]
0008 datasetName = 'iris';
0009 
0010 % This is just an example to load matlab datasets
0011 % you can load datasets from different sources with different ways
0012 % as long as the you provide x the training instance which is a matrix
0013 % of size(Number of Instance,Number of Features) and y which is the
0014 % label matrix having size(Number of Instance, 1)
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 % display dataset info
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 %% Basic Usage
0034 
0035 fprintf('===========\n');
0036 fprintf('Basic Usage\n');
0037 fprintf('===========\n');
0038 
0039 % create RealAdaBoost classifier Using svm as weak classifier
0040 % NOTE : weak classifier must support probability estimates and
0041 % SVMClassifier supports probability estimates by bassing -b argument
0042 radacl = RealAdaBooster(SVMClassifier(numClasses, '-b 1', '-b 1'));
0043 
0044 % train RealAda classifier
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 % other way to calculate error either on training dataset or other dataset
0053 err = sum(outs ~= y) / numInstances;
0054 fprintf('\tLearning Error %f\n', err);
0055 
0056 % comparing outputs of first 5 instances in predicted and target outputs
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 %% Stage Details
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 %% Display Classifier
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 %% Iteratins Errors
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 %% Add More Boosting Stages
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 %% Using Different Weak Classifier
0121 
0122 fprintf('===============================\n');
0123 fprintf('Using Different Weak Classifier\n');
0124 fprintf('===============================\n');
0125 
0126 radacl = RealAdaBooster(DecisionTreeClassifier(numClasses));
0127 
0128 % train RealAda classifier
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 %% Saving and Loading classifier to and from a file
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 %% Add Boost Stages till reaching a required Err Bound
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);

Generated on Sun 29-Sep-2013 01:25:24 by m2html © 2005