-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.m
222 lines (156 loc) · 4.3 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
%% Quick Test : Classify a single feature
clc
clear all
close all
load SubSetNormalizedFeaturesSet2.mat
y=SubSetNormalizedFeaturesSet2;
clear SubSetNormalizedFeaturesSet2;
% load NormalizedFeaturesSet2.mat
% y=NormalizedFeaturesSet2;
% clear NormalizedFeaturesSet2;
num_features=size(y,2)-1
[class1, class2 ,class3]=prepareData(y);
percentage_training=30;
feature=class1(200,1:num_features);
label=svm_classifyHighLevel(percentage_training,feature)
%%
%% %% Calculate average confusion matrix.For a quick test set numIterations=2 and percentage_training=40
clc
clear all
num_Iterations=1;
percentage_training=70;
avgConfusion=statisticalAvgConfusionMatrix(num_Iterations,percentage_training)
% percentage_training=70;
%
% avgConfusion =
%
% 0.9960 0.0040 0
% 0 0.9996 0.0004
% 0.0058 0.0173 0.9769
%%
%% To visualize flow
clc
clear all
close all
load SubSetNormalizedFeaturesSet2.mat
y=SubSetNormalizedFeaturesSet2;
clear SubSetNormalizedFeaturesSet2;
% load NormalizedFeaturesSet2.mat
% y=NormalizedFeaturesSet2;
% clear NormalizedFeaturesSet2;
num_features=size(y,2)-1
[class1, class2 ,class3]=prepareData(y);
percentage_training=70;
percentage_testing=30;
[train_samples_class1 test_samples_class1]=selectSamples(class1,percentage_training,percentage_testing);
percentage_training=70;
percentage_testing=30;
[train_samples_class2 test_samples_class2]=selectSamples(class2,percentage_training,percentage_testing);
percentage_training=70;
percentage_testing=30;
[train_samples_class3 test_samples_class3]=selectSamples(class3,percentage_training,percentage_testing);
% Note that feature 1 is in the columns,feature num_features is in the columns
c1=[test_samples_class1 ones(length(test_samples_class1),1)];
c2=[test_samples_class2 2*ones(length(test_samples_class2),1)];
c3=[test_samples_class3 3*ones(length(test_samples_class3),1)];
testFeatures=[c1;c2;c3];
predicted=[];
actual=[];
%%
X= [train_samples_class1 ;train_samples_class2];
% Y=[ones(2000,1) ;2*ones(2000,1);3*ones(2000,1)];
Y=[ones(length(train_samples_class1),1);2*ones(length(train_samples_class2),1)];
inputs=(X);
targets=transpose(Y);
SVMstruct12 = svmtrain(inputs,targets,'Kernel_Function','rbf');
%%
X= [train_samples_class1 ;train_samples_class3];
% Y=[ones(2000,1) ;2*ones(2000,1);3*ones(2000,1)];
Y=[ones(length(train_samples_class1),1);3*ones(length(train_samples_class3),1)];
inputs=(X);
targets=transpose(Y);
SVMstruct13 = svmtrain(inputs,targets,'Kernel_Function','rbf');
%%
X= [train_samples_class2 ;train_samples_class3];
% Y=[ones(2000,1) ;2*ones(2000,1);3*ones(2000,1)];
Y=[2*ones(length(train_samples_class2),1);3*ones(length(train_samples_class3),1)];
inputs=(X);
targets=transpose(Y);
SVMstruct23 = svmtrain(inputs,targets,'Kernel_Function','rbf');
%% Testing
%%
features=[c1;c2];
predicted=[];
actual=[];
for i=1:length(features)
predicted=[predicted svmclassify(SVMstruct12,features(i,1:num_features))];
actual=[actual features(i,num_features+1)];
end
confusionMatrix=confusionmat(predicted,actual);
normal=(1/length(c1))*confusionMatrix;
normMat=transpose(normal)
%%
clear features
clear confusionMatrix
clear normal
features=[c2;c3];
predicted=[];
actual=[];
for i=1:length(features)
predicted=[predicted svmclassify(SVMstruct23,features(i,1:num_features))];
actual=[actual features(i,num_features+1)];
end
confusionMatrix=confusionmat(predicted,actual);
normal=(1/length(c1))*confusionMatrix;
normMat=transpose(normal)
%%
clear features
clear confusionMatrix
clear normal
features=[c1;c3];
predicted=[];
actual=[];
for i=1:length(features)
predicted=[predicted svmclassify(SVMstruct13,features(i,1:num_features))];
actual=[actual features(i,num_features+1)];
end
confusionMatrix=confusionmat(predicted,actual);
normal=(1/length(c1))*confusionMatrix;
normMat=transpose(normal)
%%
%
% 1 and 2
%
% normal =
%
% 0.9996 0.0013
% 0.0004 0.9987
%
% 1 and 3
%
% confusionMatrix =
%
% 2295 17
% 3 2281
%
%
% normal =
%
% 0.9987 0.0074
% 0.0013 0.9926
%
% 2 and 3
%
% confusionMatrix =
%
% 2219 174
% 79 2124
%
%
% normal =
%
% 0.9656 0.0757
% 0.0344 0.9243
% 0.9996 0.0013 0.0074
% 0.0004 0.9987,0.9656 0.0757
% 0.0013 0.0344 0.9243