-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresultsVisualization.py
More file actions
170 lines (139 loc) · 5.93 KB
/
resultsVisualization.py
File metadata and controls
170 lines (139 loc) · 5.93 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import collections
import pydotplus
import numpy as np
from sklearn import tree
def tree_printer(classifier, dataframe_x, tuples_selection_type='', important_nodes=[]):
"""
:param classifier: variable in which is built the decision tree
:param dataframe_x:
:param tuples_selection_type:
:return: nothing, it prints the png image with the tree
"""
switcher = {
'': "tree_PO_Default.png",
'most': "tree_PO_Most.png",
'min': "tree_PO_Min.png",
'c': "tree_PR_Cluster.png",
'r': "tree_PR_Random.png"
}
path = switcher.get(tuples_selection_type)
try:
features = list(dataframe_x.columns.values)
dot_data = tree.export_graphviz(classifier,
feature_names=features,
out_file=None,
filled=True,
rounded=True,
node_ids=True)
graph = pydotplus.graph_from_dot_data(dot_data)
colors = ('turquoise', 'orange')
edges = collections.defaultdict(list)
for edge in graph.get_edge_list():
edges[edge.get_source()].append(int(edge.get_destination()))
leaves = list()
features = classifier.tree_.feature
for i in range(len(features)):
if features[i] == -2:
leaves.append(i)
for edge in edges:
edges[edge].sort()
for i in range(2):
dest = graph.get_node(str(edges[edge][i]))[0]
dest.set_fillcolor('turquoise')
if (int(dest.get_label().split(", ")[1].split(']')[0]) != 0) and (
int(dest.get_label().split("#")[1].split("\\")[0]) in leaves):
dest.set_fillcolor('yellow')
if len(important_nodes) > 0:
if int(dest.get_label().split("#")[1].split("\\")[0]) in important_nodes:
dest.set_fillcolor('green')
print(classifier.tree_.feature)
graph.write_png(path)
print("Tree " + path + " printed!")
except ValueError as ve:
print("Tree not printed!" + ve)
def path_finder(classifier, dataframe_x, list_y):
"""
:param classifier:
:param dataframe_x:
:param list_y:
:return: explanations is a set (list of unique elements) where each element is a dictionary
"""
headers = list(dataframe_x.columns.values)
matrix = classifier.decision_path(dataframe_x)
children_left = classifier.tree_.children_left
features = classifier.tree_.feature
thresholds = classifier.tree_.threshold
explanations = set()
for elem in range(len(list_y)):
if list_y[elem] != 0:
tmp_matrix = matrix[elem, :]
tmp_matrix = tmp_matrix.todense()
tmp_matrix = np.squeeze(np.asarray(tmp_matrix))
node_path = list()
dictionaries = list()
for index in range(len(tmp_matrix)):
if tmp_matrix[index] != 0:
node_path.append(index)
if features[index] != -2:
dictionary = {'column': headers[features[index]],
'symbol': '',
'value': thresholds[index]}
dictionaries.append(dictionary)
for index in range(len(node_path) - 1):
if children_left[node_path[index]] == node_path[index + 1]:
dictionaries[index]['symbol'] = '<='
else:
dictionaries[index]['symbol'] = '>'
compressed_dictionaries = path_compressor(dictionaries)
string_compressed_dictionaries = from_dictionaries_to_string(compressed_dictionaries)
explanations.add(string_compressed_dictionaries)
return explanations
def path_compressor(dictionaries):
compressed_dictionaries = list()
columns_set = set()
for dictionary in dictionaries:
columns_set.add(dictionary['column'])
for unique_column in columns_set:
unique_dictionary = list()
for dictionary in dictionaries:
if dictionary['column'] == unique_column:
unique_dictionary.append(dictionary)
greater_dict = list()
less_dict = list()
for dictionary in unique_dictionary:
if dictionary['symbol'] == '>':
greater_dict.append(dictionary['value'])
else:
less_dict.append(dictionary['value'])
if len(greater_dict) > 0:
greater_dict.sort(reverse=True, key=float)
dictionary = {'column': unique_column,
'symbol': '>',
'value': greater_dict[0]}
compressed_dictionaries.append(dictionary)
if len(less_dict) > 0:
less_dict.sort(key=float)
dictionary = {'column': unique_column,
'symbol': '<=',
'value': less_dict[0]}
compressed_dictionaries.append(dictionary)
return compressed_dictionaries
def from_dictionaries_to_string(dictionaries):
result = ''
for index in range(len(dictionaries)):
if index != 0:
result += " and "
result += dictionaries[index]['column'] + " " + dictionaries[index]['symbol'] + " " + str(
dictionaries[index]['value'])
return result
def print_explanations_to_terminal(explanations):
print('\n' + '_' * 100 + '\n')
print("\n" + "List of explanations :" + "\n")
for expl in explanations:
print(expl)
def features_to_terminal(method_name, time, purity, height, number_imp_nodes):
print('\n' + '_' * 30 + ' Performances ' + '_' * 30)
print('\nTime needed: ' + str(time))
print('\nPurity : ' + str(purity) + ' %')
print('\nHeight: ' + str(height))
print('\nNumber of important nodes: ' + str(number_imp_nodes))