-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
742 lines (601 loc) · 22.1 KB
/
main.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
import csv
import math
import sys
from copy import deepcopy
from random import random, randrange
import numpy
import matplotlib.pyplot as plt
from numpy import ones, vstack
from numpy.linalg import lstsq
def accuracy_metrics(actual, predicted):
""" Calculate accuracy metric.
Parameters
----------
actual : list
list of actual values
predicted : list
list of predicted values
Returns
-------
float
Accuracy
"""
count = 0
for i in range(len(actual)):
if actual[i] == predicted[i]:
count += 1
return (count / float(len(actual))) * 100
def get_piecewise_equations(number_of_segments):
"""Create piecewise equations
Parameters
----------
number_of_segments : int
Number of piecewise segments
Returns
-------
list
List of piecewise equations
"""
number_of_segments = number_of_segments - 2
min_x = -5
max_x = 5
x_range = max_x - min_x
x_values = []
y_values = []
data_points = x_range / number_of_segments
# Equation = slope, constant, range_1, range_2
# y = mx +c ; if x > range_1 and x < range_2
equations = [[0, 0, -sys.maxsize, min_x]]
for i in range(number_of_segments + 1):
x_values.append(min_x)
min_x = min_x + data_points
y_values.append(logistic_function(x_values[i]))
for i in range(number_of_segments):
points = [(x_values[i], y_values[i]), (x_values[i + 1], y_values[i + 1])]
x, y = zip(*points)
numpy_stack_values = vstack([x, ones(len(x))]).T
slope, constant = lstsq(numpy_stack_values, y)[0]
equations.append([slope, constant, x_values[i], x_values[i + 1]])
equations.append([0, 1, max_x, sys.maxsize])
return equations
def cross_validation_batch(data, number_of_splits):
""" Split dataset into batches.
Parameters
----------
data : list
Dataset.
number_of_splits : int
number of batches.
Returns
-------
list
dataset split in batches
"""
batches = []
copy_data = list(data)
batch_size = int(len(data) / number_of_splits)
for i in range(number_of_splits):
batch = []
while len(batch) < batch_size:
batch.append(copy_data.pop(randrange(len(copy_data))))
batches.append(batch)
return batches
def plot_graph(title, xlabel, ylabel, x, y):
"""Plot graph using given parameters.
Parameters
----------
title : str
Title for the graph
xlabel : str
X axis label.
ylabel : str
Y axis label.
x : list
x values
y :list
y values
"""
plt.title(title)
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.plot(x, y)
plt.show()
def load_csv(filename):
"""Load csv data.
Parameters
----------
filename : str
Filename.
Returns
-------
list
CSV data.
"""
csv_data = []
with open(filename, 'r') as fp:
rows = csv.reader(fp)
for csv_row in rows:
if not csv_row:
continue
cols = []
for col in csv_row:
if col == '?' or not col:
col = None
else:
col = float(col)
cols.append(col)
csv_data.append(cols)
return csv_data
def str_column_to_int(dataset, column):
"""Convert string column values into int"""
csv_lookup = {}
for i, value in enumerate(set([row[column] for row in dataset])):
csv_lookup[value] = i
for row in dataset:
row[column] = csv_lookup[row[column]]
return csv_lookup
def get_min_max_dataset_values(dataset):
"""
Parameters
----------
dataset : list
dataset.
Returns
-------
list
list of min and max values for each column
"""
numpy.array(dataset)
min_and_max_values = [[min(col), max(col)] for col in zip(*dataset)]
return min_and_max_values
def normalize_data(data, min_and_max_values):
""" Normalize dataset.
Parameters
----------
data : list
Dataset.
min_and_max_values : list
min and max values for each column in the dataset.
"""
for row in data:
for i in range(len(row) - 1):
row[i] = (row[i] - min_and_max_values[i][0]) / (min_and_max_values[i][1] - min_and_max_values[i][0])
def initialize_weights(neurons_in_layer1, neurons_in_layer2):
""" Initialize weights randomly for neurons connected from layer 1 to layer 2.
Parameters
----------
neurons_in_layer1 : int
Number of neurons in first layer.
neurons_in_layer2 : int
Number of neurons in second layer.
Returns
-------
list
Randomly generated weights.
Example : [{'weights':[w1,w2...]}, ...]
"""
weighted_neurons_with_bias = []
total_number_of_weights_and_bias = neurons_in_layer1 + 1
for i in range(neurons_in_layer2):
random_weights = []
for j in range(total_number_of_weights_and_bias):
random_weights.append(random())
weighted_neurons_with_bias.append({'weights': random_weights})
return weighted_neurons_with_bias
def replace_missing_data_with_mean(dataset):
""" Fill missing data with the mean of that column
Parameters
----------
dataset : list
Dataset
Returns
-------
list
Dataset
"""
ndataset = numpy.array(dataset, dtype=numpy.float)
column_mean = numpy.nanmean(ndataset, axis=0)
indices = numpy.where(numpy.isnan(ndataset))
ndataset[indices] = numpy.take(column_mean, indices[1])
return ndataset.tolist()
def create_neural_network(input_attributes, output_classes, hidden_layer_neurons):
""" Create/Initialize a single hidden layered neural network based on input parameters.
Parameters
----------
input_attributes : int
The number of input attributes or parameters for the neural network.
output_classes : int
The number of output classes for the neural network.
hidden_layer_neurons : int
The number of hidden neurons withint he hidden layer.
Returns
-------
dict
Neural network.
Example :
{
'hidden_layer': [{'weights':[w1,w2...]},...],
'output_layer': [{'weights':[w3,w4...]},...],
}
"""
neural_network = {'hidden_layer': initialize_weights(input_attributes, hidden_layer_neurons),
'output_layer': initialize_weights(hidden_layer_neurons, output_classes)}
return neural_network
def neuron_activation_function(input_vector, weight_vector):
""" Calculate weighted sum along with bias for each neuron's activation function.
Parameters
----------
input_vector : list
Input vector values.
weight_vector : list
Weight vector values.
Returns
-------
float
Net weighted sum along with bias for each neuron.
"""
net_weighted_sum = 0
number_of_activation_function = len(weight_vector) - 1
for i in range(number_of_activation_function):
net_weighted_sum += float(input_vector[i]) * weight_vector[i]
return net_weighted_sum + weight_vector[-1]
def logistic_function(net_neuron_activation_value):
""" Calculate the logistic function value i.e. sigmoid function value.
Parameters
----------
net_neuron_activation_value : float
Weighted sum along with bias for a neuron i.e. neuron activation value.
Returns
-------
float
Value of sigmoid function.
"""
sigmoid = 1 / (1 + math.exp(-net_neuron_activation_value))
return sigmoid
def derivative_of_logistic_function(logistic_function_value):
""" Calculate derivative of sigmoid function.
Parameters
----------
logistic_function_value : float
Value of sigmoid function.
Returns
-------
float
Derivative of sigmoid function.
"""
derivative_sigmoid = logistic_function_value * (1 - logistic_function_value)
return derivative_sigmoid
def piecewise_function(value, piecewise_equations):
""" Calculate the logistic function value i.e. piecewise linea equation value.
Parameters
----------
value : float
Value of linear equation's conditions
piecewise_equations : list
List of piecewise equations
Returns
-------
float
Value of sigmoid function.
"""
for equation in piecewise_equations:
if equation[2] <= value < equation[3]:
return (value * equation[0]) + equation[1]
def derivative_of_piecewise_function(value, piecewise_equations):
"""Calculate derivative of linear equations.
Parameters
----------
value : float
Value of logistic function.
piecewise_equations : list
List of piecewise equations
Returns
-------
float
Derivative of sigmoid function.
"""
for equation in piecewise_equations:
if equation[2] <= value < equation[3]:
return equation[0]
def forward_propagation_with_piecewise(input_vector, neural_network_layer, piecewise_equations):
""" Calculate the output vector for next layer in neural network based
on net neuron activation values of previous layer in neural network.
Parameters
----------
input_vector : list
List of input vector values.
neural_network_layer : dict
Layer attributes within the neural network.
piecewise_equations : list
List of piecewise equations
Returns
-------
list
List of input vector for the next layer within neural network.
"""
next_input_vector = []
for vector in neural_network_layer:
net_neuron_activation_value = neuron_activation_function(input_vector, vector['weights'])
vector['activation_output'] = piecewise_function(net_neuron_activation_value, piecewise_equations)
next_input_vector.append(vector['activation_output'])
return next_input_vector
def feed_forward_propagation_with_piecewise(neural_network, input_vector, piecewise_equations):
""" Feed forward propagation for single hidden layered neural network.
Parameters
----------
neural_network : dict
Neural Network.
input_vector : list
Input vector values
piecewise_equations : list
List of piecewise equations.
Returns
-------
list
Output vector values.
"""
next_input_vector = forward_propagation_with_piecewise(input_vector, neural_network['hidden_layer'],
piecewise_equations)
output_vector = forward_propagation(next_input_vector, neural_network['output_layer'])
return output_vector
def back_propagation_with_piecewise(neural_network, expected_output, piecewise_equations):
""" Back propagation algorithm for a single hidden layered neural network.
Update neural network with calculated delta values.
Parameters
----------
neural_network : dict
Neural Network.
expected_output : list
Expected output for a particular input.
piecewise_equations : list
List of piecewise equations.
"""
errors = []
for i in range(len(neural_network['output_layer'])):
neuron = neural_network['output_layer'][i]
errors.append(expected_output[i] - neuron['activation_output'])
for i in range(len(neural_network['output_layer'])):
neuron = neural_network['output_layer'][i]
neuron['delta'] = errors[i] * derivative_of_logistic_function(neuron['activation_output'])
errors = []
for i in range(len(neural_network['hidden_layer'])):
error = 0.0
for neuron in neural_network['output_layer']:
error += (neuron['weights'][i] * neuron['delta'])
errors.append(error)
for i in range(len(neural_network['hidden_layer'])):
neuron = neural_network['hidden_layer'][i]
neuron['delta'] = errors[i] * derivative_of_piecewise_function(neuron['activation_output'],
piecewise_equations)
def forward_propagation(input_vector, neural_network_layer):
""" Calculate the output vector for next layer in neural network based
on net neuron activation values of previous layer in neural network.
Paramters
---------
input_vector : list
List of input vector values.
neural_network_layer : dict
Layer attributes within the neural network.
Returns
-------
list
List of input vector for the next layer within neural network.
"""
next_input_vector = []
for vector in neural_network_layer:
net_neuron_activation_value = neuron_activation_function(input_vector, vector['weights'])
vector['activation_output'] = logistic_function(net_neuron_activation_value)
next_input_vector.append(vector['activation_output'])
return next_input_vector
def feed_forward_propagation(neural_network, input_vector):
"""Feed forward propagation for single hidden layered neural network.
Parameters
----------
neural_network : dict
Neural Network.
input_vector : list
Input vector values
Returns
-------
list
Output vector values.
"""
next_input_vector = forward_propagation(input_vector, neural_network['hidden_layer'])
output_vector = forward_propagation(next_input_vector, neural_network['output_layer'])
return output_vector
def back_propagation(neural_network, expected_output):
""" Back propagation algorithm for a single hidden layered neural network.
Update neural network with calculated delta values.
Parameters
----------
neural_network : dict
Neural Network.
expected_output : list
Expected output for a particular input.
"""
errors = []
for i in range(len(neural_network['output_layer'])):
neuron = neural_network['output_layer'][i]
errors.append(expected_output[i] - neuron['activation_output'])
for i in range(len(neural_network['output_layer'])):
neuron = neural_network['output_layer'][i]
neuron['delta'] = errors[i] * derivative_of_logistic_function(neuron['activation_output'])
errors = []
for i in range(len(neural_network['hidden_layer'])):
error = 0.0
for neuron in neural_network['output_layer']:
error += (neuron['weights'][i] * neuron['delta'])
errors.append(error)
for i in range(len(neural_network['hidden_layer'])):
neuron = neural_network['hidden_layer'][i]
neuron['delta'] = errors[i] * derivative_of_logistic_function(neuron['activation_output'])
def redefine_weights(neural_network, input_vector, learning_rate):
""" Update weights after back_propagation.
Parameters
----------
neural_network : dict
Neural Network.
input_vector : list
Input vector values
learning_rate : float
learning rate.
"""
input_values = input_vector[:-1]
for neuron in neural_network['hidden_layer']:
for i in range(len(input_values)):
neuron['weights'][i] += learning_rate * neuron['delta'] * float(input_values[i])
neuron['weights'][-1] += learning_rate * neuron['delta']
input_values = [neuron['activation_output'] for neuron in neural_network['hidden_layer']]
for neuron in neural_network['output_layer']:
for i in range(len(input_values)):
neuron['weights'][i] += learning_rate * neuron['delta'] * input_values[i]
neuron['weights'][-1] += learning_rate * neuron['delta']
def train_neural_network(neural_network, train_dataset, test_dataset, learning_rate, iterations, feed_forward_function,
back_propagation_function, piecewise=None):
""" Train neural network.
Parameters
----------
neural_network : dict
Neural Network.
train_dataset : list
train dataset
test_dataset : list
test dataset
learning_rate : float
learning rate
iterations : int
number of iterations
feed_forward_function : function
feed forward function name
back_propagation_function : function
back propagation function name
piecewise : list
piecewise equations
Returns
-------
x_value, mse, accuracies : (list, list, list)
"""
mse = []
x_value = []
accuracies = []
for iteration in range(iterations):
x_value.append(iteration)
error = 0
for dataset_entry in train_dataset:
if piecewise:
feed_forward_output = feed_forward_function(neural_network, dataset_entry, piecewise)
else:
feed_forward_output = feed_forward_function(neural_network, dataset_entry)
expected = [dataset_entry[-1]]
error += (expected[0] - feed_forward_output[0]) ** 2
if piecewise:
back_propagation_function(neural_network, expected, piecewise)
else:
back_propagation_function(neural_network, expected)
redefine_weights(neural_network, dataset_entry, learning_rate)
mse.append(error/len(train_dataset))
predictions = []
for dataset_entry in test_dataset:
prediction = test_neural_network(neural_network, dataset_entry, feed_forward_function, piecewise)
predictions.append(prediction)
actual = [row[-1] for row in test_dataset]
accuracy = accuracy_metrics(actual, predictions)
accuracies.append(accuracy)
return x_value, mse, accuracies
def test_neural_network(neural_network, input_vector, feed_forward_function, piecewise=None):
""" Test neural network.
Parameters
----------
neural_network : dict
Neural Network.
feed_forward_function : function
feed forward function name
piecewise : list
piecewise equations
"""
if piecewise:
result = feed_forward_function(neural_network, input_vector, piecewise)
else:
result = feed_forward_function(neural_network, input_vector)
if result[0] >= 0.50:
return 1
else:
return 0
def run_code(filename):
""" Train, Test and evaluate neural network on a given dataset. """
data = load_csv(filename)
dataset = replace_missing_data_with_mean(data)
str_column_to_int(dataset, len(dataset[0]) - 1)
min_and_max_values = get_min_max_dataset_values(dataset)
normalize_data(dataset, min_and_max_values)
learning_rate = 0.08
iterations = 25
splits = 2
batches = cross_validation_batch(dataset, splits)
number_of_inputs = len(dataset[0]) - 1
number_of_outputs = 1
number_of_hiddens = 2 * number_of_inputs
neural_network = create_neural_network(number_of_inputs, number_of_outputs, number_of_hiddens)
# Sigmoid
print("Using Sigmoid :- ")
total = []
for batch in batches:
train_set = list(batches)
train_set.remove(batch)
train_set = sum(train_set, [])
test_set = []
for row in batch:
row_copy = list(row)
test_set.append(row_copy)
network = deepcopy(neural_network)
x_values, mse, accuracies = train_neural_network(network, train_set, test_set, learning_rate, iterations,
feed_forward_propagation, back_propagation)
plot_graph('Accuracy graph for Sigmoid \nBatch - ' + str(batches.index(batch) + 1), 'Iterations', 'Accuracies',
x_values, accuracies)
plot_graph('MSE graph for Sigmoid \nBatch - ' + str(batches.index(batch) + 1), 'Iterations', 'MSE', x_values, mse)
predictions = []
for row in test_set:
prediction = test_neural_network(network, row, feed_forward_propagation)
predictions.append(prediction)
actuals = [row[-1] for row in test_set]
accuracy = accuracy_metrics(actuals, predictions)
total.append(accuracy)
print('Accuracies : %s' % total)
print('Mean Accuracy sigmoid: %.3f%%' % (sum(total) / float(len(total))))
# Piecewise
print("Using Piecewise Function Approximation:- ")
segments = [4, 6, 8, 10, 12, 14]
for segment in segments:
total = []
for batch in batches:
train_set = list(batches)
train_set.remove(batch)
train_set = sum(train_set, [])
test_set = []
for row in batch:
row_copy = list(row)
test_set.append(row_copy)
network_piecewise = deepcopy(neural_network)
x_values, mse, accuracies = train_neural_network(network_piecewise, train_set, test_set, learning_rate,
iterations, feed_forward_propagation_with_piecewise,
back_propagation_with_piecewise,
get_piecewise_equations(segment))
plot_graph('Accuracy graph for ' + str(segment) + ' Linear Piecewise Segments' + '\nBatch - ' +
str(batches.index(batch) + 1), 'Iterations', 'Accuracies', x_values, accuracies)
plot_graph('MSE graph for ' + str(segment) + ' Linear Piecewise Segments' + ' \nBatch - ' +
str(batches.index(batch) + 1), 'Iterations', 'MSE', x_values, mse)
predictions = []
for row in test_set:
prediction = test_neural_network(network_piecewise, row, feed_forward_propagation_with_piecewise,
get_piecewise_equations(segment))
predictions.append(prediction)
actuals = [row[-1] for row in test_set]
accuracy = accuracy_metrics(actuals, predictions)
total.append(accuracy)
print("Accuracy with %d piecewise segments for batch %d = %.3f%%" %
(segment, (batches.index(batch) + 1), (accuracy)))
print("Number of piecewise linear segment - ", segment)
print('Accuracies : %s' % total)
print('Mean Accuracy with %d linear segments : %.3f%%' % (segment, (sum(total) / float(len(total)))))
# Execute code
run_code("bank_dataset.csv")