-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfisherIris_mpl_kfold.m
236 lines (182 loc) · 5.61 KB
/
fisherIris_mpl_kfold.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
222
223
224
225
226
227
228
229
230
231
232
233
234
%% MLP - dataset: Fisheriris
%author = @leilamr
close all;
clear all;
clc
%load dataset
load fisheriris
%convert targets: 1 = "setosa", 2 = "versicolor" e 3 = "virginica"
classnames = unique(species);
for i=1:3
class(strcmp(species, classnames{i})) = i;
end
class = class';
%inputs data
inputData = [class meas];
%definition of classes of iris plant types
a=[1 0 0];
b=[0 1 0];
c=[0 0 1];
avgTest = 0;
%% cross-validation: KFold (k = 10) manual
j = 10;
% size kx = 15x5
k1 = [inputData(1:5,:); inputData(56:65,:)];
k2 = [inputData(6:10,:); inputData(66:75,:)];
k3 = [inputData(11:15,:); inputData(76:85,:)];
k4 = [inputData(16:20,:); inputData(86:95,:)];
k5 = [inputData(21:25,:); inputData(96:105,:)];
k6 = [inputData(26:30,:); inputData(106:115,:)];
k7 = [inputData(31:35,:); inputData(116:125,:)];
k8 = [inputData(36:40,:); inputData(126:135,:)];
k9 = [inputData(41:45,:); inputData(136:145,:)];
k10 = [inputData(46:55,:);inputData(146:150,:)];
%above folds exchange
for i=1:j
if i == 1
%make train matrix(135x5)
trainMatrix = [k1;k2;k3;k4;k5;k6;k7;k8;k9];
testMatrix = k10;
end
if i == 2
%make train matrix(135x5)
trainMatrix = [k1;k2;k3;k4;k5;k6;k7;k8;k10];
testMatrix = k9;
end
if i == 3
%make train matrix(135x5)
trainMatrix = [k1;k2;k3;k4;k5;k6;k7;k9;k10];
testMatrix = k8;
end
if i == 4
%make train matrix(135x5)
trainMatrix = [k1;k2;k3;k4;k5;k6;k8;k9;k10];
testMatrix = k7;
end
if i == 5
%make train matrix(135x5)
trainMatrix = [k1;k2;k3;k4;k5;k7;k8;k9;k10];
testMatrix = k6;
end
if i == 6
%make train matrix(135x5)
trainMatrix = [k1;k2;k3;k4;k6;k7;k8;k9;k10];
testMatrix = k5;
end
if i == 7
%make train matrix(135x5)
trainMatrix = [k1;k2;k3;k5;k6;k7;k8;k9;k10];
testMatrix = k4;
end
if i == 8
%make train matrix(135x5)
trainMatrix = [k1;k2;k4;k5;k6;k7;k8;k9;k10];
testMatrix = k3;
end
if i == 9
%make train matrix(135x5)
trainMatrix = [k1;k3;k4;k5;k6;k7;k8;k9;k10];
testMatrix = k2;
end
if i == 10
%make train matrix(135x5)
trainMatrix = [k2;k3;k4;k5;k6;k7;k8;k9;k10];
testMatrix = k1;
end
%% start MLP
%matrix for Train (135x4)
inputTrain = trainMatrix(:,2:5);
%target (135x1)
target = trainMatrix(:,1);
%normalization of data-input
%inputTrain = mapminmax(inputTrain);
%normalization of target
outputTrain = zeros(size(target,1),3);
for j=1:size(target,1)
if target(j,1) == 1
outputTrain(j,:) = a;
end
if target(j,1) == 2
outputTrain(j,:) = b;
end
if target(j,1) == 3
outputTrain(j,:) = c;
end
end
%matrix transposed input and output network
inputTrain = inputTrain'; %(4x135)
outputTrain = outputTrain'; %(3x135)
%% make network
n_hidden_nodes = 4;
net = feedforwardnet(n_hidden_nodes);
net.trainParam.epochs = 1000;
net.trainParam.max_fail = 500;
net.trainParam.min_grad = 0.000000000000001;
net.divideParam.trainRatio = 1;
net.divideParam.valRatio = 0;
net.divideParam.testRatio = 0;
net.layers{1}.transferFcn='tansig';
net.layers{2}.transferFcn='purelin';
%% train network
[net, xTest, yTest] = train(net,inputTrain,outputTrain);
%% test network performance
inputTest = testMatrix(:,2:5); %(15x4)
targetTest = testMatrix(:,1);
% normalization of data-test
%inputTest = mapminmax(inputTest);
outputTest = zeros(size(targetTest,1),3);
for j=1:size(targetTest,1)
if targetTest(j,1) == 1
outputTest(j,:) = a;
end
if targetTest(j,1) == 2
outputTest(j,:) = b;
end
if targetTest(j,1) == 3
outputTest(j,:) = c;
end
end
%matrix transposed input and output network
inputTest = inputTest'; %(4x15)
outputTest = outputTest'; %(3x15)
%network prediction result
result = sim(net,inputTest);
[per con] = confusion(outputTest,result);
perTest = 100 * (1 - per);
fprintf('Fold = %d [Accuracy = %2.2f%%] \n',i, perTest);
avgTest = avgTest + perTest;
%plot and save confusion matrix
x(i)=plotconfusion(result,outputTest);
if i==1
saveas(x(i),'confusion_1.jpg');
end
if i==2
saveas(x(i),'confusion_2.jpg');
end
if i==3
saveas(x(i),'confusion_3.jpg');
end
if i==4
saveas(x(i),'confusion_4.jpg');
end
if i==5
saveas(x(i),'confusion_5.jpg');
end
if i==6
saveas(x(i),'confusion_6.jpg');
end
if i==7
saveas(x(i),'confusion_7.jpg');
end
if i==8
saveas(x(i),'confusion_8.jpg');
end
if i==9
saveas(x(i),'confusion_9.jpg');
end
if i==10
saveas(x(i),'confusion_10.jpg');
end
end
avgTest=avgTest/10;
fprintf('network hit average (kfold = 10): %.3f%%\n', avgTest);