forked from rust-corpus/qrates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.py
52 lines (45 loc) · 1.73 KB
/
drawing.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
import plotly.graph_objects as go
from sklearn.tree import _tree
def draw_sankey_decision_tree(tree, feature_names):
tree_ = tree.tree_
feature_name = [
feature_names[i] if i != _tree.TREE_UNDEFINED else "undefined!"
for i in tree_.feature
]
edges = []
labels = {}
def recurse(node, parent=None):
if tree_.feature[node] != _tree.TREE_UNDEFINED:
name = feature_name[node]
threshold = tree_.threshold[node]
right_child = tree_.children_right[node]
right_size = recurse(right_child)
edges.append( (node, right_child, right_size) )
labels[right_child] = "{}".format(name)
left_child = tree_.children_left[node]
left_size = recurse(left_child)
edges.append( (node, left_child, left_size) )
labels[left_child] = "!{}".format(name)
return tree_.n_node_samples[node]
tot_size = recurse(0)
labels[0] = "all"
fig = go.Figure(data=[go.Sankey(
arrangement = "freeform",
orientation = "v",
node = dict(
#pad = 15,
#thickness = 20,
#line = dict(color = "black", width = 0.5),
label = [labels[i] for i in range(max(labels.keys()) + 1)],
color = ["red" if "!" in labels[i] else "green" for i in range(max(labels.keys()) + 1)],
),
link = dict(
# values in `source` and `target` correspond to label indices
source = [x[0] for x in edges],
target = [x[1] for x in edges],
# amount of flow
value = [x[2] for x in edges],
)
)])
fig.update_layout(title_text="Reasons of unsafety", font_size=10)
fig.show()