-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation_utils.py
95 lines (78 loc) · 3.78 KB
/
evaluation_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
83
84
85
86
87
88
89
90
91
92
93
94
95
import numpy as np
import pandas as pd
import pickle
def compute_avg_f_score(output_hot, target):
"""
Compute the Precision, Recall and F-Score for the predictions 'output_hot'.
"""
true_positives = np.sum((output_hot & target), axis = 1)
false_positives = np.sum((output_hot & ~target), axis = 1)
false_negatives = np.sum((~output_hot & target), axis = 1)
denominator = true_positives + false_positives
precision = np.where(denominator > 0,
true_positives / denominator,
np.zeros_like(true_positives)
)
denominator = true_positives + false_negatives
recall = np.where(denominator > 0,
true_positives / denominator,
np.zeros_like(true_positives)
)
denominator = precision + recall
f_score = np.where(denominator > 0,
2*(precision * recall) / denominator,
np.zeros_like(true_positives)
)
return np.mean(f_score), np.mean(precision), np.mean(recall)
def compute_avg_f_score_only(output_hot, target_hot):
"""
Compute the Precision, Recall and F-Score for the predictions 'output_hot'.
"""
output_hot = output_hot.values if isinstance(output_hot, pd.DataFrame) or isinstance(output_hot, pd.Series) else output_hot
target_hot = target_hot.values if isinstance(target_hot, pd.DataFrame) or isinstance(target_hot, pd.Series) else target_hot
f_score = 0
for out, targ in zip(output_hot, target_hot):
output_hot = np.asarray(out).astype(int)
target = np.asarray(targ).astype(int)
true_positives = np.sum((output_hot & target), axis = 1)
false_positives = np.sum((output_hot & ~target), axis = 1)
false_negatives = np.sum((~output_hot & target), axis = 1)
denominator = true_positives + false_positives
precision = np.where(denominator > 0,
true_positives / denominator,
np.zeros_like(true_positives)
)
denominator = true_positives + false_negatives
recall = np.where(denominator > 0,
true_positives / denominator,
np.zeros_like(true_positives)
)
denominator = precision + recall
f_score += np.where(denominator > 0,
2*(precision * recall) / denominator,
np.zeros_like(true_positives)
)
return f_score / len(output_hot)
def compute_avg_acuracy(y_hot, y_test):
correct = np.sum(y_hot == y_test)
return correct / y_test.size
# Save dictionaries to a file
def save_params_to_file(file_name, **kwargs):
with open(file_name, 'wb') as file:
pickle.dump(kwargs, file)
def load_params_from_file(file_name):
with open(file_name, 'rb') as file:
loaded_params = pickle.load(file)
params_trees = loaded_params['params_trees']
params_forest = loaded_params['params_forest']
params_knn = loaded_params['params_knn']
params_rr = loaded_params['params_rr']
return params_trees, params_forest, params_knn, params_rr
def load_features_from_file(file_name):
with open(file_name, 'rb') as file:
loaded_params = pickle.load(file)
params_trees = loaded_params['features_trees']
params_forest = loaded_params['features_forest']
params_knn = loaded_params['features_knn']
params_rr = loaded_params['features_rr']
return params_trees, params_forest, params_knn, params_rr