-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkfold.py
232 lines (175 loc) · 8.06 KB
/
kfold.py
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
from csv import reader
from math import exp
from random import shuffle
import matplotlib.pyplot as plt
##Import data from file---------------------------------------------------------------------------------------------------------------------
def import_data(file_name):
dataset = list()
with open(file_name,'r') as file:
csv_reader = reader(file)
for row in csv_reader:
if not row:
continue
dataset.append(row)
return dataset
##convert input string to float---------------------------------------------------------------------------------------------------------------------
def str_to_float(dataset,column):
for row in dataset:
row[column] = float(row[column].strip())
##convert string type to int type ---------------------------------------------------------------------------------------------------------------------
def str_to_int(dataset,column):
#if Iris-setosa replace with 0
#replace with 1
for row in dataset:
if row[column] == "Iris-setosa":
row[column] = 0
else:
row[column] = 1
##function fold ---------------------------------------------------------------------------------------------------------------------
def fold(dataset,
k_fold, l_rate,
n_epoch, error_train, error_test,
accuracy_train,accuracy_test):
weights = [0.5 for i in range(len(dataset[0]))]
#merandom dataset
shuffle(dataset)
dataset_test = list()
#split dataset to data train and data test
for k in range(k_fold):
dataset_train = list(dataset)
dataset_test = list()
#split data set from index a to b for testing data, dan sisanya untuk training data
a = int(k*len(dataset)/k_fold)
b = int(a+(len(dataset)/k_fold))
for m in range(a,b):
dataset_test.append(dataset_train[m])
del dataset_train[a:b]
#melakukan training, dan me-retrun weigh terakhir untuk mengetes data test
weights = train(dataset_train, l_rate,
n_epoch, error_train,accuracy_train)
#melakukan testing data menggunakan weight hasil training, tanpa melakukan pembaruan weight
test(dataset_test,n_epoch,
weights,error_test,accuracy_test)
averrage(error_train, error_test,
accuracy_train,accuracy_test,
n_epoch,k_fold)
## train the data---------------------------------------------------------------------------------------------------------------------
def train(dataset_train, l_rate,
n_epoch, error_train,accuracy_train):
weight = [0.0 for i in range(len(dataset_train))]
sum_error = list()
accuracy = list()
#training sebanyak n epoch
for epoch in range(n_epoch):
sumAccuracy = 0
sumError = 0
#menghitung sum weigh dan mengupdate weight
for row in dataset_train:
activ = activation(row, weight)
prediction = 1.0 if activ >= 0.5 else 0.0
if prediction == row[-1]:
sumAccuracy+=1
error = (row[-1] - activ)**2
sumError+=error
dweight = d_weight(row,activ)
weight[-1] = weight[0] + l_rate * dweight[-1]
for i in range(len(row)-1):
weight[i] = weight[i] + l_rate * dweight[i]*row[i]
#memasukkan setiap acuracy dan error setiap epoch untuk divisualisasikan dalam grafik
accuracy_train.append(sumAccuracy/len(dataset_train))
error_train.append(sumError)
return weight
##find dwaight / update weight ---------------------------------------------------------------------------------------------------------------------
def d_weight(row,activation):
weight = [0.0 for i in range(len(row))]
for i in range(len(row)):
weight[i] = 2.0*(row[-1]-activation)*(1.0-activation)*activation
return weight
##find activation---------------------------------------------------------------------------------------------------------------------
def activation(row, weights):
activation = weights[-1]
for i in range(len(row)-1):
activation += weights[i] * row[i]
return 1/(1+exp(-activation))
##test the data after training---------------------------------------------------------------------------------------------------------------------
def test(dataset_test, n_epoch, weights, error_test,accuracy_test):
#testing sebanyak n epoch
for epoch in range(n_epoch):
sumAccuracy = 0
sumError = 0
#menghitung sum weigh
for row in dataset_test:
activ = activation(row, weights)
prediction = 1.0 if activ >= 0.5 else 0.0
if prediction == row[-1]:
sumAccuracy+=1
error = (row[-1] - activ)**2
sumError += error
#memasukkan setiap acuracy dan error setiap epoch untuk divisualisasikan dalam grafik
error_test.append(sumError)
accuracy_test.append(sumAccuracy/len(dataset_test))
##calculate the avverage error and acuracy from k fold ---------------------------------------------------------------------------------------------------------------------
def averrage(error_train,error_test,
accuracy_train,accuracy_test,
n_epoch,k_fold):
errorTrain = [0.0 for i in range(n_epoch)]
errorTest = [0.0 for i in range(n_epoch)]
accTrain = [0.0 for i in range(n_epoch)]
accTest = [0.0 for i in range(n_epoch)]
#mencari rata rata error dan accurasi dari setiap fold
for k in range(k_fold):
err_train = list()
err_test = list()
acc_train = list()
acc_test = list()
#split data set from index a to b, kemudian mencari sum nya
#(karena tadi error dan accuracy dari setiap fold di jadikan satu)
a = int(k*len(error_train)/k_fold)
b = int(a+(len(error_train)/k_fold))
for m in range(a,b):
err_test.append(error_test[m])
err_train.append(error_train[m])
acc_test.append(accuracy_test[m])
acc_train.append(accuracy_train[m])
errorTrain = [x + y for x, y in zip(err_train, errorTrain)]
errorTest= [x + y for x, y in zip(err_test, errorTest)]
accTrain = [x + y for x, y in zip(acc_train, accTrain)]
accTest = [x + y for x, y in zip(acc_test, accTest)]
#membagi sum error dan accuracy dengan k_fold
errorTrain = [x / k_fold for x in errorTrain]
accTrain = [x / k_fold for x in accTrain]
accTest = [x / k_fold for x in accTest]
errorTest = [x / k_fold for x in errorTest]
#menggambar grafik
draw_grafik(errorTrain,errorTest,
'Averrage Error',
'Error',
'Grafik Error k-Fold')
draw_grafik(accTrain,accTest,
'Averrage Accuracy',
'Accuracy',
'Grafik Accurasy k-Fold')
##draw grafik ---------------------------------------------------------------------------------------------------------------------
def draw_grafik(data_train,data_test,label,ylabel,title):
plt.plot(data_train,label = label+' Train')
plt.plot(data_test, label = label+' Test')
plt.xlabel('Epoch')
plt.ylabel(ylabel)
plt.title(title)
plt.legend()
plt.show()
##Main program ---------------------------------------------------------------------------------------------------------------------
dataset = import_data("iris.csv")
for i in range(len(dataset[0])-1):
str_to_float(dataset,i)
str_to_int(dataset,len(dataset[0])-1)
k_fold = 5
l_rate = 0.2
#l_rate = 0.8
epoch = 300
error_train = list()
error_test = list()
accuracy_train = list()
accuracy_test =list()
fold(dataset, k_fold, l_rate, epoch,
error_train, error_test,accuracy_train,accuracy_test)