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 svmcl = SVMClassifier(numClasses);
0041
0042
0043 disp('Training Classifier');
0044 [svmcl, learnErr] = learn(svmcl, x, y);
0045 fprintf('Learning Error %f\n', learnErr);
0046
0047 disp('Testing Classifier');
0048 outs = computeOutputs(svmcl, x);
0049
0050 err = sum(outs ~= y) / numInstances;
0051 fprintf('Learning Error %f\n', err);
0052
0053
0054 disp('[predicted outputs : correct outputs]');
0055 disp([outs(1:5, :) y(1:5 , :)]);
0056
0057
0058 fprintf('=================\n');
0059 fprintf('Instances Weights\n');
0060 fprintf('=================\n');
0061
0062 svmcl = SVMClassifier(numClasses);
0063 wts = ones(numInstances, 1) / numInstances;
0064
0065
0066 svmcl = learn(svmcl, x, y, wts);
0067
0068 outs = computeOutputs(svmcl, x);
0069
0070 err = sum(outs ~= y) / numInstances;
0071 fprintf('Error %f\n', err);
0072
0073
0074
0075 fprintf('====================================\n');
0076 fprintf('Passing arguments and prob estimates\n');
0077 fprintf('====================================\n');
0078
0079
0080
0081 svmcl = SVMClassifier(numClasses,'-c 10 -g 1 -b 1','-b 1');
0082 svmcl = learn(svmcl, x, y);
0083
0084 [outs, prob] = computeOutputs(svmcl, x);
0085
0086 disp('[predicted output : correct output : class probabilities]');
0087 disp([outs(1:5, :) y(1:5 , :) prob(1:5, :) ]);
0088
0089
0090
0091
0092 fprintf('=========================\n');
0093 fprintf('Displaying The Classifier\n');
0094 fprintf('=========================\n');
0095
0096
0097 svmcl = SVMClassifier(numClasses);
0098
0099 disp('Display before training');
0100 display(svmcl);
0101
0102 disp('-----------------------');
0103
0104 svmcl = learn(svmcl, x, y);
0105 disp('Display after training');
0106 display(svmcl);
0107
0108
0109
0110
0111
0112 fprintf('=======================================\n');
0113 fprintf('Save And Load Classifier To/From A File\n');
0114 fprintf('=======================================\n');
0115
0116 svmcl = SVMClassifier(numClasses);
0117 svmcl = learn(svmcl, x, y, wts);
0118
0119
0120 saveToFile(svmcl, 'test.bin');
0121
0122
0123 svmcl2 = loadFromFile(SVMClassifier, 'test.bin');
0124
0125 outs1 = computeOutputs(svmcl, x);
0126 err1 = sum(outs1 ~= y) / numInstances;
0127 outs2 = computeOutputs(svmcl2, x);
0128 err2 = sum(outs2 ~= y) / numInstances;
0129
0130 fprintf('Error Before Save %f, Error After Save %f\n', err1, err2);
0131
0132
0133 fprintf('=========================\n');
0134 fprintf('Using Kfold with SVM\n');
0135 fprintf('=========================\n');
0136
0137 svmcl = SVMClassifier(numClasses);
0138 cp = kfold(x, y, 10, svmcl);
0139
0140 fprintf('Accuracy of 10 fold-cross validation %f\n', cp.CorrectRate * 100);