Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pnria Projet Deeplever
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
PNRIA
Global Helper
Pnria Projet Deeplever
Merge requests
!4
end Decision tree
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
end Decision tree
decision-tree-type-file
into
main
Overview
0
Commits
2
Pipelines
0
Changes
11
Merged
Caroline de Pourtalès
requested to merge
decision-tree-type-file
into
main
3 years ago
Overview
0
Commits
2
Pipelines
0
Changes
11
Expand
0
0
Merge request reports
Viewing commit
0e0b3aa4
Prev
Next
Show latest version
11 files
+
1687
−
173
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
11
Search (e.g. *.vue) (Ctrl+P)
0e0b3aa4
added naive bayes
· 0e0b3aa4
Caroline DE POURTALES
authored
3 years ago
pages/application/DecisionTree/utils/dtviz.py
+
69
−
2
Options
@@ -12,7 +12,8 @@
#==============================================================================
import
getopt
import
pygraphviz
import
ast
import
re
#
#==============================================================================
def
create_legend
(
g
):
@@ -22,6 +23,8 @@ def create_legend(g):
legend
.
add_node
(
"
b
"
,
style
=
"
invis
"
)
legend
.
add_node
(
"
c
"
,
style
=
"
invis
"
)
legend
.
add_node
(
"
d
"
,
style
=
"
invis
"
)
legend
.
add_node
(
"
e
"
,
style
=
"
invis
"
)
legend
.
add_node
(
"
f
"
,
style
=
"
invis
"
)
legend
.
add_edge
(
"
a
"
,
"
b
"
)
edge
=
legend
.
get_edge
(
"
a
"
,
"
b
"
)
@@ -34,7 +37,10 @@ def create_legend(g):
edge
.
attr
[
"
color
"
]
=
"
blue
"
edge
.
attr
[
"
style
"
]
=
"
dashed
"
legend
.
add_edge
(
"
e
"
,
"
f
"
)
edge
=
legend
.
get_edge
(
"
e
"
,
"
f
"
)
edge
.
attr
[
"
label
"
]
=
"
contrastive explanation
"
edge
.
attr
[
"
color
"
]
=
"
red
"
#
#==============================================================================
def
visualize
(
dt
):
@@ -194,3 +200,64 @@ def visualize_expl(dt, instance, expl):
# saving file
g
.
layout
(
prog
=
'
dot
'
)
return
(
g
.
to_string
())
#==============================================================================
def
visualize_contrastive_expl
(
dt
,
instance
,
cont_expl
):
"""
Visualize a DT with graphviz and plot the running instance.
"""
#path that follows the instance - colored in blue
path
,
term
,
depth
=
dt
.
execute
(
instance
)
edges_instance
=
[]
for
i
in
range
(
len
(
path
)
-
1
)
:
edges_instance
.
append
((
path
[
i
],
path
[
i
+
1
]))
edges_instance
.
append
((
path
[
-
1
],
"
term:
"
+
term
))
g
=
pygraphviz
.
AGraph
(
directed
=
True
,
strict
=
True
)
g
.
edge_attr
[
'
dir
'
]
=
'
forward
'
g
.
graph_attr
[
'
rankdir
'
]
=
'
TB
'
# non-terminal nodes
for
n
in
dt
.
nodes
:
g
.
add_node
(
n
,
label
=
dt
.
feature_names
[
dt
.
nodes
[
n
].
feat
])
node
=
g
.
get_node
(
n
)
node
.
attr
[
'
shape
'
]
=
'
circle
'
node
.
attr
[
'
fontsize
'
]
=
13
# terminal nodes
for
n
in
dt
.
terms
:
g
.
add_node
(
n
,
label
=
dt
.
terms
[
n
])
node
=
g
.
get_node
(
n
)
node
.
attr
[
'
shape
'
]
=
'
square
'
node
.
attr
[
'
fontsize
'
]
=
13
# transitions
for
n1
in
dt
.
nodes
:
for
v
in
dt
.
nodes
[
n1
].
vals
:
n2
=
dt
.
nodes
[
n1
].
vals
[
v
]
n2_type
=
g
.
get_node
(
n2
).
attr
[
'
shape
'
]
g
.
add_edge
(
n1
,
n2
)
edge
=
g
.
get_edge
(
n1
,
n2
)
if
len
(
v
)
==
1
:
edge
.
attr
[
'
label
'
]
=
dt
.
fvmap
[
tuple
([
dt
.
nodes
[
n1
].
feat
,
tuple
(
v
)[
0
]])]
else
:
edge
.
attr
[
'
label
'
]
=
'
{0}
'
.
format
(
'
\n
'
.
join
([
dt
.
fvmap
[
tuple
([
dt
.
nodes
[
n1
].
feat
,
val
])]
for
val
in
tuple
(
v
)]))
#instance path in dashed
if
((
n1
,
n2
)
in
edges_instance
)
or
(
n2_type
==
'
square
'
and
(
n1
,
"
term:
"
+
dt
.
terms
[
n2
])
in
edges_instance
):
edge
.
attr
[
'
style
'
]
=
'
dashed
'
for
label
in
edge
.
attr
[
'
label
'
].
split
(
'
\n
'
):
if
label
in
cont_expl
:
edge
.
attr
[
'
color
'
]
=
'
red
'
edge
.
attr
[
'
fontsize
'
]
=
10
edge
.
attr
[
'
arrowsize
'
]
=
0.8
g
.
add_subgraph
(
name
=
'
legend
'
)
create_legend
(
g
)
# saving file
g
.
layout
(
prog
=
'
dot
'
)
return
(
g
.
to_string
())
Loading