-
Notifications
You must be signed in to change notification settings - Fork 0
/
RF_LOSOCV.m
31 lines (29 loc) · 1.13 KB
/
RF_LOSOCV.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
function accuracy = RF_LOSOCV(feat, y, songList, songIDvector, numTrees)
for k=1:length(songList)
predicted = [];
train = find(songIDvector ~= songList(k));
test = find(songIDvector == songList(k));
test_hist = hist(y(test),[0:13]);
train_hist = hist(y(train),[0:13]);
fair_test = [];
for i = 1: length(test_hist)
if test_hist(i) > 0 && train_hist(i) > 0 %% testing chords are available on the training set
fair_test = [fair_test find(y(test) == (i - 1))];
end
end
% Normalize
MEAN = mean(feat(train,:));
STD = std(feat(train,:));
for sample = 1: length(feat)
norm_feat(sample,:) = (feat(sample,:) - MEAN)./STD;
end
norm_feat(find(isnan(norm_feat))) = 0;
% Random Forest classifier
b = TreeBagger(numTrees,norm_feat(train,:),y(train),'SampleWithReplacement','off','NVarToSample',30);
y_predict = predict(b,norm_feat(fair_test,:));
for i = 1: length(y_predict)
predicted(i) = str2num(y_predict{i});
end
% Compute the performance measures:
accuracy(k) = numel(find(predicted == y(fair_test)))/length(y(fair_test))*100;
end