-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomForest_classifier.m
37 lines (26 loc) · 1.27 KB
/
RandomForest_classifier.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
function [accuracy_RF,cMat, precision, f1score] = RandomForest_classifier(healthy_features, pd_features, training_size, testing_size,length)
% Training Phase
train_labels = ones(training_size,1);
train_labels(1:40) = 0;
X_train = zeros(training_size,length);
X_train(1:40,:) = healthy_features(1:40,:);
X_train(41:80,:) = pd_features(1:40,:);
%makes the RF Tree model based on the labels given
rf_fit = TreeBagger(100,X_train,train_labels,'OOBPrediction','On',...
'Method','classification');
%Testing Phase
test_labels = ones(testing_size,1);
test_labels(1:10) = 0;
X_test = zeros(testing_size,length);
X_test(1:10,:) = healthy_features(41:50,:);
X_test(11:20,:) = pd_features(41:50,:);
% Prediction
rf_predict = predict(rf_fit,X_test); % predicts output of identified model Random Forest.
% Confusion Matrix
%Creates the confusion matrix for SVM based on the labels given
cMat = confusionmat(test_labels,str2double(rf_predict)); % returns the confusion matrix of known (labels) and predicted (C1) groups.
accuracy_RF = 100*(cMat(1,1)+cMat(2,2))/testing_size;
precision = 100*(cMat(1,1)/(cMat(1,1)+ cMat(2,1)));
recall = 100*(cMat(1,1)/(cMat(1,1)+ cMat(1,2)));
f1score = 2*(precision*recall)/(precision + recall);
end