-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBaggedC45_multiclass.asv
73 lines (66 loc) · 2.75 KB
/
BaggedC45_multiclass.asv
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function [methodstring,stats] = BaggedC45(training_set , testing_set, training_labels, testing_labels)
% format labels from (-1,1) to (0,1)
if training_labels < 0
training_labels(training_labels==-1) = 0;
testing_labels(testing_labels==-1) = 0;
end
methodstring = 'BaggedC45';
unique_testing_labels = unique(testing_labels);
% [final, labels, error] = baggingdtrees(training_set, training_labels, testing_set, testing_labels);
%
%
% tp=0; tn=0; fp=0; fn=0; prediction = zeros(size(final));
% for i=1:size(labels,1)
% prediction(i) = round(mean(labels(i,:)));
%
% if testing_labels(i)==1
% if prediction(i) == testing_labels(i)
% tp = tp + 1;
% else
% fp = fp + 1;
% end
% elseif testing_labels(i)==2
% if prediction(i) == testing_labels(i)
% tn = tn + 1;
% else
% fn = fn + 1;
% end
% end
% end
% Check for matlabpool
try
if matlabpool('size')==0, matlabpool open; end
catch e
fprintf('Could not open matlabpool...skipping\n');
end
% Options for TreeBagger
options = statset('UseParallel','always','UseSubstreams','never');
B = TreeBagger(50,training_set,training_labels,'FBoot',0.667,'oobpred','on','Method','classification','NVarToSample','all','NPrint',4,'Options',options); % create bagged d-tree classifiers from training
[Yfit,Scores] = predict(B,testing_set); % use to classify testing
% probs = Scores(:,1); % Select probabilities -- check manual entry for 'predict', look at 'B' to make sure your reqd class is the second column
% if numel(unique(testing_labels))>1
% [FPR,TPR,T,AUC,OPTROCPT,~,~] = perfcurve(testing_labels,probs,min(unique(testing_labels))); % calculate AUC. 'perfcurve' can also calculate sens, spec etc. to plot the ROC curve.
% [TP FN] = perfcurve(testing_labels,probs,1,'xCrit','TP','yCrit','FN');
% [FP TN] = perfcurve(testing_labels,probs,1,'xCrit','FP','yCrit','TN');
% [~,ACC] = perfcurve(testing_labels,probs,1,'xCrit','TP','yCrit','accu');
% [~,PPV] = perfcurve(testing_labels,probs,1,'xCrit','TP','yCrit','PPV');
%
% optim_idx = find(FPR == OPTROCPT(1) & TPR == OPTROCPT(2));
% stats.tp = TP(optim_idx);
% stats.fn = FN(optim_idx);
% stats.fp = FP(optim_idx);
% stats.tn = TN(optim_idx);
% stats.auc = AUC;
% stats.spec = 1-FPR(optim_idx);
% stats.sens = TPR(optim_idx);
% stats.acc = ACC(optim_idx);
% stats.ppv = PPV(optim_idx);
stats.prediction = Scores;
[C, I] = max(Scores,[],2);
stats.decision = unique_testing_labels(I);
stats.tp = nnz(testing_labels == stats.decision);
stats.tn = nnz(~(testing_labels==
% stats.threshold = T(optim_idx);
% stats.decision = stats.prediction >= stats.threshold;
% stats.trained_classifier = B;
% end