-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNeuralNetworkDriver.py
69 lines (55 loc) · 2.62 KB
/
NeuralNetworkDriver.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
# Dependencies
import numpy as np
from NeuralNetworkClassifier import NeuralNetworkClassifier
from CSVFileDelegate import CSVFileDelegate
class NeuralNetworkDriver(object):
def __init__(self, filepath):
"""
Class responsible for training and testing the Neural Network Classifier
:param filepath: path to the file containing the dataset
"""
self.filepath = filepath
def build_network_and_classify_data(self):
"""
Builds, trains and tests the neural network ten times, tells the CSVFileDelegate to output the results to csv
and returns the results to the GUI
:return: Dictionary of results
"""
final_results = []
csv_results = []
# Build, train and test the network 10 times
for index in range(0, 10, 1):
results_dictionary = {}
# Initialise new CSVFileDelegate
csv_delegate = CSVFileDelegate(self.filepath)
# Initialise Neural Network Classifier with 30 hidden units and train it for 5000 iterations
neural_network = NeuralNetworkClassifier(csv_delegate.training_data, csv_delegate.training_target)
neural_network.build_network(30, 5000, 0.05)
# Store results and calculate accuracy
results = neural_network.classify_set(csv_delegate.testing_data)
accuracy = self.compare_result_to_target(results, csv_delegate.testing_target)
results_dictionary['input'] = csv_delegate.testing_target
results_dictionary['result'] = results
results_dictionary['accuracy'] = accuracy
# Store results in format for CSV output
for i in range(0, len(csv_delegate.testing_target), 1):
csv_dictionary = {'input': csv_delegate.testing_target[i], 'result': results[i],
'fold_number': index + 1}
csv_results.append(csv_dictionary)
final_results.append(results_dictionary)
# Write results to CSV file
csv_delegate = CSVFileDelegate(self.filepath)
csv_delegate.write_results_to_csv(csv_results)
# Return Dictionary of results
return final_results
@staticmethod
def compare_result_to_target(result, target):
"""
Returns the proportion similarity between two lists
:param result: First list
:param target: Second list
:return: The proportion accuracy between the two lists
"""
match_count = np.sum(np.array(result) == np.array(target))
match_percentage = float(match_count) / len(result)
return match_percentage