-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
55 lines (47 loc) · 1.62 KB
/
model.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
"""
trained model
__author__ = "Joe Sarsfield"
__email__ = "joe.sarsfield@gmail.com"
"""
from substrate import Substrate
import numpy as np
class NN_bag_model:
""" neural network bagging model """
def __init__(self):
self.performance_gen = [] # list of performance per generation
def predict(self, networks, samples, labels):
""" make a prediction of a set of samples and return performance metric """
self.tp = 0
self.fn = 0
self.tn = 0
self.fp = 0
for i, sample in enumerate(samples):
y_norms = []
# Get predictions from all nets
for net in networks:
y, y_norm, y_arg, y_arg_true = net.predict(sample[-1], labels[i])
y_norms.append(y_norm)
y = [0, 1] if np.array(y_norms)[:, 1].sum() >= len(y_norms)/2 else [1, 0]
y_arg = np.argmax(y)
y_arg_true = np.argmax(labels[i])
if y_arg == y_arg_true: # TP or TN
if y_arg == 1:
self.tp += 1
else:
self.tn += 1
else: # FP or FN
if y_arg == 0: # FN
self.fn += 1
else: # FP
self.fp += 1
self.visualise_performance()
self.performance_gen.append(networks[0].auc(self.tp, self.tn, self.fp, self.fn))
return self.performance_gen[-1]
def visualise_performance(self):
pass
def save_model(self):
""" save model to disk """
pass
def load_model(self):
""" load model from disk """
pass