-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
140 lines (103 loc) · 3.51 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
%% Initialize variables - care this will have to change.
filename = 'BreastCancerData.csv';
delimiter = ',';
%% Format string for each line of text:
formatSpec = '%f%f%f%f%f%f%f%f%f%f%f%[^\n\r]';
%% Open the text file.
fileID = fopen(filename,'r');
%% Read columns of data according to format string.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'ReturnOnError', false);
%% Close the text file.
fclose(fileID);
%% Allocate imported array to column variable names
VarName1 = dataArray{:, 1};
VarName2 = dataArray{:, 2};
VarName3 = dataArray{:, 3};
VarName4 = dataArray{:, 4};
VarName5 = dataArray{:, 5};
VarName6 = dataArray{:, 6};
VarName7 = dataArray{:, 7};
VarName8 = dataArray{:, 8};
VarName9 = dataArray{:, 9};
VarName10 = dataArray{:, 10};
VarName11 = dataArray{:, 11};
%% Clear temporary variables
clearvars filename delimiter formatSpec fileID ans dataArray;
%% Create the nx11 matrix
dataSet = [VarName1, VarName2, VarName3, VarName4, VarName5, VarName6, VarName7, VarName8, VarName9, VarName10, VarName11];
dataSet = double(dataSet);
labels = dataSet(:, 11);
%% Normalise values in [0,1]
normalisedData = [];
for k=2:size(dataSet, 2) - 1
normalisedData = [normalisedData, (dataSet(:, k) - min(dataSet(:, k))) / (max(dataSet(:, k)) - min(dataSet(:, k)))];
end
% for i = 1:10
% [train,test] = crossvalind('Kfold',labels,10);
% mdl = fitcknn(normalisedData(train,:),labels(train),'NumNeighbors',3);
% predictions = predict(mdl,normalisedData(test,:));
% classperf(cp,predictions,test);
% end
indices = crossvalind('Kfold',labels,10);
%% KNN model training
cpKnn = classperf(labels);
for i = 1:10
test = (indices == i);
train = ~test;
mdl = fitcknn(normalisedData(train,:),labels(train),'NumNeighbors',5);
predictions = predict(mdl,normalisedData(test,:));
classperf(cpKnn,predictions,test);
end
disp('KNN:');
fprintf('Accuracy: %f\n', 1 - cpKnn.ErrorRate);
fprintf('Sensitivity: %f\n', cpKnn.Sensitivity);
fprintf('Specificity: %f\n', cpKnn.Specificity);
fprintf('\n');
%% Naive Bayes model training
cpNBayes = classperf(labels);
for i = 1:10
test = (indices == i);
train = ~test;
mdl = fitcnb(normalisedData(train,:),labels(train));
predictions = predict(mdl,normalisedData(test,:));
classperf(cpNBayes,predictions,test);
end
disp('Naive Bayes:');
fprintf('Accuracy: %f\n', 1 - cpNBayes.ErrorRate);
fprintf('Sensitivity: %f\n', cpNBayes.Sensitivity);
fprintf('Specificity: %f\n', cpNBayes.Specificity);
fprintf('\n');
%% SVM model training
cpSvm = classperf(labels);
for i = 1:10
test = (indices == i);
train = ~test;
mdl = fitcsvm(normalisedData(train,:),labels(train));
predictions = predict(mdl,normalisedData(test,:));
classperf(cpSvm,predictions,test);
end
disp('SVM:');
fprintf('Accuracy: %f\n', 1 - cpSvm.ErrorRate);
fprintf('Sensitivity: %f\n', cpSvm.Sensitivity);
fprintf('Specificity: %f\n', cpSvm.Specificity);
fprintf('\n');
%% Decision tree model training
cpDTree = classperf(labels);
for i = 1:10
test = (indices == i);
train = ~test;
mdl = fitctree(normalisedData(train,:),labels(train));
predictions = predict(mdl,normalisedData(test,:));
classperf(cpDTree,predictions,test);
end
disp('Decision Tree:');
fprintf('Accuracy: %f\n', 1 - cpDTree.ErrorRate);
fprintf('Sensitivity: %f\n', cpDTree.Sensitivity);
fprintf('Specificity: %f\n', cpDTree.Specificity);
fprintf('\n');
%%dTree
%%cvmdl = crossval(dTree);
%%size(cvmdl.Y);
%%disp(all(cvmdl.Y==labels));
%%disp('Accuracy:');
%%disp(1 - kfoldLoss(cvmdl));