8000 ENH add fontname argument in export_graphviz for non-English characters by Zeroto521 · Pull Request #18959 · scikit-learn/scikit-learn · GitHub
[go: up one dir, main page]

Skip to content

ENH add fontname argument in export_graphviz for non-English characters #18959

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ Changelog
:pr:`123456` by :user:`Joe Bloggs <joeongithub>`.
where 123456 is the *pull request* number, not the issue number.

:mod:`sklearn.tree`
...................

- |Enhancement| Add `fontname` argument in :func:`tree.export_graphviz`
for non-English characters. :pr:`18959` by :user:`Zero <Zeroto521>`
and :user:`wstates <wstates>`.

:mod:`sklearn.cluster`
......................

Expand Down
29 changes: 16 additions & 13 deletions sklearn/tree/_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,18 +371,17 @@ def __init__(self, out_file=SENTINEL, max_depth=None,
feature_names=None, class_names=None, label='all',
filled=False, leaves_parallel=False, impurity=True,
node_ids=False, proportion=False, rotate=False, rounded=False,
special_characters=False, precision=3):
special_characters=False, precision=3, fontname='helvetica'):

super().__init__(
max_depth=max_depth, feature_names=feature_names,
class_names=class_names, label=label, filled=filled,
impurity=impurity,
node_ids=node_ids, proportion=proportion, rotate=rotate,
rounded=rounded,
precision=precision)
impurity=impurity, node_ids=node_ids, proportion=proportion,
rotate=rotate, rounded=rounded, precision=precision)
self.leaves_parallel = leaves_parallel
self.out_file = out_file
self.special_characters = special_characters
self.fontname = fontname

# PostScript compatibility for special characters
if special_characters:
Expand Down Expand Up @@ -449,16 +448,17 @@ def head(self):
self.out_file.write(
', style="%s", color="black"'
% ", ".join(rounded_filled))
if self.rounded:
self.out_file.write(', fontname=helvetica')

self.out_file.write(', fontname="%s"' % self.fontname)
self.out_file.write('] ;\n')

# Specify graph & edge aesthetics
if self.leaves_parallel:
self.out_file.write(
'graph [ranksep=equally, splines=polyline] ;\n')
if self.rounded:
self.out_file.write('edge [fontname=helvetica] ;\n')

self.out_file.write('edge [fontname="%s"] ;\n' % self.fontname)

if self.rotate:
self.out_file.write('rankdir=LR ;\n')

Expand Down Expand Up @@ -667,7 +667,8 @@ def export_graphviz(decision_tree, out_file=None, *, max_depth=None,
feature_names=None, class_names=None, label='all',
filled=False, leaves_parallel=False, impurity=True,
node_ids=False, proportion=False, rotate=False,
rounded=False, special_characters=False, precision=3):
rounded=False, special_characters=False, precision=3,
fontname='helvetica'):
"""Export a decision tree in DOT format.

This function generates a GraphViz representation of the decision tree,
Expand Down Expand Up @@ -734,8 +735,7 @@ def export_graphviz(decision_tree, out_file=None, *, max_depth=None,
When set to ``True``, orient tree left to right rather than top-down.

rounded : bool, default=False
When set to ``True``, draw node boxes with rounded corners and use
Helvetica fonts instead of Times-Roman.
When set to ``True``, draw node boxes with rounded corners.

special_characters : bool, default=False
When set to ``False``, ignore special characters for PostScript
Expand All @@ -745,6 +745,9 @@ def export_graphviz(decision_tree, out_file=None, *, max_depth=None,
Number of digits of precision for floating point in the values of
impurity, threshold and value attributes of each node.

fontname : str, default='helvetica'
Name of font used to render text.

Returns
-------
dot_data : string
Expand Down Expand Up @@ -784,7 +787,7 @@ def export_graphviz(decision_tree, out_file=None, *, max_depth=None,
filled=filled, leaves_parallel=leaves_parallel, impurity=impurity,
node_ids=node_ids, proportion=proportion, rotate=rotate,
rounded=rounded, special_characters=special_characters,
precision=precision)
precision=precision, fontname=fontname)
exporter.export(decision_tree)

if return_string:
Expand Down
37 changes: 24 additions & 13 deletions sklearn/tree/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ def test_graphviz_toy():
# Test export code
contents1 = export_graphviz(clf, out_file=None)
contents2 = 'digraph Tree {\n' \
'node [shape=box] ;\n' \
'node [shape=box, fontname="helvetica"] ;\n' \
'edge [fontname="helvetica"] ;\n' \
'0 [label="X[0] <= 0.0\\ngini = 0.5\\nsamples = 6\\n' \
'value = [3, 3]"] ;\n' \
'1 [label="gini = 0.0\\nsamples = 3\\nvalue = [3, 0]"] ;\n' \
Expand All @@ -50,7 +51,8 @@ def test_graphviz_toy():
contents1 = export_graphviz(clf, feature_names=["feature0", "feature1"],
out_file=None)
contents2 = 'digraph Tree {\n' \
'node [shape=box] ;\n' \
'node [shape=box, fontname="helvetica"] ;\n' \
'edge [fontname="helvetica"] ;\n' \
'0 [label="feature0 <= 0.0\\ngini = 0.5\\nsamples = 6\\n' \
'value = [3, 3]"] ;\n' \
'1 [label="gini = 0.0\\nsamples = 3\\nvalue = [3, 0]"] ;\n' \
Expand All @@ -66,7 +68,8 @@ def test_graphviz_toy():
# Test with class_names
contents1 = export_graphviz(clf, class_names=["yes", "no"], out_file=None)
contents2 = 'digraph Tree {\n' \
'node [shape=box] ;\n' \
'node [shape=box, fontname="helvetica"] ;\n' \
'edge [fontname="helvetica"] ;\n' \
'0 [label="X[0] <= 0.0\\ngini = 0.5\\nsamples = 6\\n' \
'value = [3, 3]\\nclass = yes"] ;\n' \
'1 [label="gini = 0.0\\nsamples = 3\\nvalue = [3, 0]\\n' \
Expand All @@ -84,11 +87,11 @@ def test_graphviz_toy():
# Test plot_options
contents1 = export_graphviz(clf, filled=True, impurity=False,
proportion=True, special_characters=True,
rounded=True, out_file=None)
rounded=True, out_file=None, fontname="sans")
contents2 = 'digraph Tree {\n' \
'node [shape=box, style="filled, rounded", color="black", ' \
'fontname=helvetica] ;\n' \
'edge [fontname=helvetica] ;\n' \
'fontname="sans"] ;\n' \
'edge [fontname="sans"] ;\n' \
'0 [label=<X<SUB>0</SUB> &le; 0.0<br/>samples = 100.0%<br/>' \
'value = [0.5, 0.5]>, fillcolor="#ffffff"] ;\n' \
'1 [label=<samples = 50.0%<br/>value = [1.0, 0.0]>, ' \
Expand All @@ -107,7 +110,8 @@ def test_graphviz_toy():
contents1 = export_graphviz(clf, max_depth=0,
class_names=True, out_file=None)
contents2 = 'digraph Tree {\n' \
'node [shape=box] ;\n' \
'node [shape=box, fontname="helvetica"] ;\n' \
'edge [fontname="helvetica"] ;\n' \
'0 [label="X[0] <= 0.0\\ngini = 0.5\\nsamples = 6\\n' \
'value = [3, 3]\\nclass = y[0]"] ;\n' \
'1 [label="(...)"] ;\n' \
Expand All @@ -122,7 +126,9 @@ def test_graphviz_toy():
contents1 = export_graphviz(clf, max_depth=0, filled=True,
out_file=None, node_ids=True)
contents2 = 'digraph Tree {\n' \
'node [shape=box, style="filled", color="black"] ;\n' \
'node [shape=box, style="filled", color="black", '\
'fontname="helvetica"] ;\n' \
'edge [fontname="helvetica"] ;\n' \
'0 [label="node #0\\nX[0] <= 0.0\\ngini = 0.5\\n' \
'samples = 6\\nvalue = [3, 3]", fillcolor="#ffffff"] ;\n' \
'1 [label="(...)", fillcolor="#C0C0C0"] ;\n' \
Expand All @@ -143,7 +149,9 @@ def test_graphviz_toy():
contents1 = export_graphviz(clf, filled=True,
impurity=False, out_file=None)
contents2 = 'digraph Tree {\n' \
'node [shape=box, style="filled", color="black"] ;\n' \
'node [shape=box, style="filled", color="black", ' \
'fontname="helvetica"] ;\n' \
'edge [fontname="helvetica"] ;\n' \
'0 [label="X[0] <= 0.0\\nsamples = 6\\n' \
'value = [[3.0, 1.5, 0.0]\\n' \
'[3.0, 1.0, 0.5]]", fillcolor="#ffffff"] ;\n' \
Expand Down Expand Up @@ -174,12 +182,13 @@ def test_graphviz_toy():
clf.fit(X, y)

contents1 = export_graphviz(clf, filled=True, leaves_parallel=True,
out_file=None, rotate=True, rounded=True)
out_file=None, rotate=True, rounded=True,
fontname="sans")
contents2 = 'digraph Tree {\n' \
'node [shape=box, style="filled, rounded", color="black", ' \
'fontname=helvetica] ;\n' \
'fontname="sans"] ;\n' \
'graph [ranksep=equally, splines=polyline] ;\n' \
'edge [fontname=helvetica] ;\n' \
'edge [fontname="sans"] ;\n' \
'rankdir=LR ;\n' \
'0 [label="X[0] <= 0.0\\nmse = 1.0\\nsamples = 6\\n' \
'value = 0.0", fillcolor="#f2c09c"] ;\n' \
Expand All @@ -203,7 +212,9 @@ def test_graphviz_toy():

contents1 = export_graphviz(clf, filled=True, out_file=None)
contents2 = 'digraph Tree {\n' \
'node [shape=box, style="filled", color="black"] ;\n' \
'node [shape=box, style="filled", color="black", '\
'fontname="helvetica"] ;\n' \
'edge [fontname="helvetica"] ;\n' \
'0 [label="gini = 0.0\\nsamples = 6\\nvalue = 6.0", ' \
'fillcolor="#ffffff"] ;\n' \
'}'
Expand Down
0