-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
82 lines (63 loc) · 2.15 KB
/
utils.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
import numpy as np
def one_hot_encode(data):
"""
Converts a dataset of discrete features into a dataset of binary features
using One-Hot Encoding. Returns the boolean dataset.
"""
feature_domains = [] # to store features domains
bin_col_number = 0 # num. of columns of the new dataset
new_col = 0 # col index of current feature value in 'bin_data'
for col, feature in data.iteritems():
current_feature_domain = {}
# store each feature's value in a dictionary of
# pairs (value, new_col)
for value in feature.values:
if value not in current_feature_domain:
current_feature_domain[value] = new_col
new_col += 1
feature_domains.append(current_feature_domain)
bin_col_number += len(current_feature_domain)
# this array will contain the binary dataset
bin_data = np.zeros(shape=(data.shape[0], bin_col_number), dtype=np.int8)
# populate the binary dataset
for col, feature in data.iteritems():
for row, value in enumerate(feature):
bin_data[row, feature_domains[col][value]] = 1
return bin_data
class ResultSet:
""" Class to gather test results"""
def __init__(self):
self.nodes = 0
self.time = 0
self.precision = 0
self.recall = 0
self.avg_precision = 0
self.f1 = 0
self.accuracy = 0
self.matthews = 0
def get_mean_scores(res_list):
""" Returns a ResultSet object with the mean values of the metrics.
Takes a list of ResultSet objects in input.
"""
num = len(res_list)
if num == 0:
raise ValueError("Empty res_list!")
r = ResultSet()
for res in res_list:
r.nodes += res.nodes
r.time += res.time
r.precision += res.precision
r.recall += res.recall
r.avg_precision += res.avg_precision
r.f1 += res.f1
r.accuracy += res.accuracy
r.matthews += res.matthews
r.nodes /= num
r.time /= num
r.precision /= num
r.recall /= num
r.avg_precision /= num
r.f1 /= num
r.accuracy /= num
r.matthews /= num
return r