-
Notifications
You must be signed in to change notification settings - Fork 0
/
Class_RF.m
56 lines (47 loc) · 1.51 KB
/
Class_RF.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
% To perform classification for the horse colic
% dataset using Random Forest (RF).
% Dataset:
% LOAD horse_colic.MAT loads 3 variables but
% only these 2 are used for classification:
% inputs(input data)- a 22x368 matrix defining 22 attributes of
% 368 samples.
% targets(target data)- a 1x368 matrix which is set to 0 for
% non-surgical lesion and 1 for surgical lesion
% Pseudocode:
% 1. Load the data
% 2. Assign the input and target matrix to variables
% 3. Split data into training and testing
% 4. Train and test the RF model
% 5. Calculate the classification accuracy
% 6. Plot confusion matrix and ROC curve
% 7. Compute the AUROC.
%Load the data
load horse_colic
X = inputs.'
Y = targets.'
%Divide data for training and testing
cv = cvpartition(368,'HoldOut',0.3);
%Extract the test indices
idx = cv.test;
%Get the training data
XTrain=X(~idx,:);
YTrain=Y(~idx,:);
%Get the testing data
XTest=X(idx,:);
YTest=Y(idx,:);
%Train RF model using the training data
model=fitcensemble(XTrain,YTrain);
%Predict the model on testing data
[label] = predict(model,XTest);
%Calculate the prediction accuracy
accuracy = sum((predict(model,XTest) == YTest))/length(YTest)*100
% Plot confusion matrix
cm = confusionchart(YTest,label)
cm.Title = 'Confusion Matrix for Classification by RF';
% Plot ROC curve and compute AUC
[X,Y,T,AUC] = perfcurve(YTest,label,1);
AUC
plot(X,Y)
xlabel('False positive rate')
ylabel('True positive rate')
title('ROC for Classification by RF')