-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (56 loc) · 1.95 KB
/
Copy pathutils.py
File metadata and controls
73 lines (56 loc) · 1.95 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Aug 20 10:30:23 2020
@author: Edson Cilos
"""
import os
import numpy as np
import pickle
from sklearn.metrics import multilabel_confusion_matrix
def sensitivity(matrix):
return matrix[1,1]/(matrix[1,1] + matrix[1,0])
def specificity(matrix):
return matrix[0,0]/(matrix[0,0] + matrix[0,1])
def precision(matrix):
a = matrix[1,1] + matrix[0,1]
return matrix[1,1]/a if a != 0 else 0
def f1(matrix):
_precision = precision(matrix)
recall = sensitivity(matrix)
return 2 * (_precision * recall) / (_precision + recall) \
if _precision + recall != 0 else 0
def array_result(multi_matrix, index):
matrix = multi_matrix[index]
return [sensitivity(matrix),
specificity(matrix),
precision(matrix),
f1(matrix)
]
def build_row(X_test, y_test, y_pred):
multi_matrix = multilabel_confusion_matrix(y_test, y_pred)
result = []
for i in range(np.unique(y_test).shape[0]):
result.extend(array_result(multi_matrix, i))
return result
def file_name(nn = False, baseline=False, scaler=False,
pca=False, over_sample=False):
prefix = 'nn_' if nn else ''
sc = 'std_' if scaler else ''
baseline = 'baseline_' if baseline else ''
pc = 'pca_' if pca else ''
ov = 'over_' if over_sample else ''
return prefix + baseline + sc + pc + ov + 'gs.csv'
def load_encoder():
return pickle.load(open(os.path.join('data', 'enconder.sav'), 'rb'))
def classes_names():
encoder =load_encoder()
classes = len(encoder.classes_)
return encoder.inverse_transform([i for i in range(classes)]), classes
def append_time(file_name, time):
with open(os.path.join('results', "time.csv"), "a+") as file_object:
file_object.seek(0)
data = file_object.read(100)
if len(data) > 0 :
file_object.write("\n")
file_object.write("{},{}".format(file_name, time))