-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex.m
39 lines (32 loc) · 1.48 KB
/
ex.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
% clc; clear all;
load fisheriris
[~,~,labels] = unique(s); % Labels: 1/2/3
data = zscore(meas); % Scale features
numInst = size(data,1);
numLabels = max(labels);
%# Split training/testing
idx = randperm(numInst);
numTrain = 100;
numTest = numInst - numTrain;
trainData = data(idx(1:numTrain),:);
testData = data(idx(numTrain+1:end),:);
trainLabel = labels(idx(1:numTrain));
testLabel = labels(idx(numTrain+1:end));
restoredefaultpath % This will remove any custom paths
rehash toolboxcache
savepath
%# Train one-against-all models
model = cell(numLabels,1);
for k=1:numLabels
model{k} = svmtrain(double(trainLabel==k), trainData, '-c 1 -g 0.2 -b 1');
end
%# Get probability estimates of test instances using each model
prob = zeros(numTest,numLabels);
for k=1:numLabels
[~,~,p] = svmpredict(double(testLabel==k), testData, model{k}, '-b 1');
prob(:,k) = p(:,model{k}.Label==1); % Probability of class==k
end
% Predict the class with the highest probability
[~,pred] = max(prob,[],2);
acc = sum(pred == testLabel) ./ numel(testLabel); % Accuracy
C = confusionmat(testLabel, pred); % Confusion matrix