From 34cd59d908ac5d5f9877e801d3ca63f608bc464c Mon Sep 17 00:00:00 2001 From: wstates Date: Mon, 8 Jul 2019 15:57:41 +0800 Subject: [PATCH 1/2] Export_graphviz() defaults "Helvetica" font Non-English character text cannot be displayed Added a parameter that controls the dot file "fontname" Signed-off-by: wstates --- sklearn/tree/export.py | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/sklearn/tree/export.py b/sklearn/tree/export.py index 2c9d21b7bb5cc..a4489b3a33845 100644 --- a/sklearn/tree/export.py +++ b/sklearn/tree/export.py @@ -183,7 +183,7 @@ def __init__(self, max_depth=None, feature_names=None, class_names=None, label='all', filled=False, impurity=True, node_ids=False, proportion=False, rotate=False, rounded=False, - precision=3, fontsize=None): + precision=3, fontsize=None, fontname='helvetica'): self.max_depth = max_depth self.feature_names = feature_names self.class_names = class_names @@ -196,6 +196,7 @@ def __init__(self, max_depth=None, feature_names=None, self.rounded = rounded self.precision = precision self.fontsize = fontsize + self.fontname = fontname def get_color(self, value): # Find the appropriate color & intensity for a node @@ -355,7 +356,7 @@ 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, @@ -363,7 +364,7 @@ def __init__(self, out_file=SENTINEL, max_depth=None, impurity=impurity, node_ids=node_ids, proportion=proportion, rotate=rotate, rounded=rounded, - precision=precision) + precision=precision, fontname=fontname) self.leaves_parallel = leaves_parallel self.out_file = out_file self.special_characters = special_characters @@ -434,7 +435,7 @@ def head(self): ', 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 @@ -442,7 +443,7 @@ def head(self): 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') @@ -656,7 +657,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, @@ -772,7 +774,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: From 316127c81cb4699da1218cb335019d46855b3463 Mon Sep 17 00:00:00 2001 From: wstates Date: Mon, 8 Jul 2019 15:57:41 +0800 Subject: [PATCH 2/2] Export_graphviz() defaults "Helvetica" font Non-English character text cannot be displayed Added a parameter that controls the dot file "fontname" Signed-off-by: wstates --- sklearn/tree/export.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/sklearn/tree/export.py b/sklearn/tree/export.py index 2c9d21b7bb5cc..77fa0f0be1015 100644 --- a/sklearn/tree/export.py +++ b/sklearn/tree/export.py @@ -149,7 +149,7 @@ def plot_tree(decision_tree, max_depth=None, feature_names=None, fontsize : int, optional (default=None) Size of text font. If None, determined automatically to fit figure. - + optional (default=None) Returns ------- annotations : list of artists @@ -183,7 +183,7 @@ def __init__(self, max_depth=None, feature_names=None, class_names=None, label='all', filled=False, impurity=True, node_ids=False, proportion=False, rotate=False, rounded=False, - precision=3, fontsize=None): + precision=3, fontsize=None, fontname='helvetica'): self.max_depth = max_depth self.feature_names = feature_names self.class_names = class_names @@ -196,6 +196,7 @@ def __init__(self, max_depth=None, feature_names=None, self.rounded = rounded self.precision = precision self.fontsize = fontsize + self.fontname = fontname def get_color(self, value): # Find the appropriate color & intensity for a node @@ -355,7 +356,7 @@ 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, @@ -363,7 +364,7 @@ def __init__(self, out_file=SENTINEL, max_depth=None, impurity=impurity, node_ids=node_ids, proportion=proportion, rotate=rotate, rounded=rounded, - precision=precision) + precision=precision, fontname=fontname) self.leaves_parallel = leaves_parallel self.out_file = out_file self.special_characters = special_characters @@ -434,7 +435,7 @@ def head(self): ', 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 @@ -442,7 +443,7 @@ def head(self): 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') @@ -656,7 +657,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, @@ -732,6 +734,10 @@ def export_graphviz(decision_tree, out_file=None, max_depth=None, precision : int, optional (default=3) Number of digits of precision for floating point in the values of impurity, threshold and value attributes of each node. + fontname : str,optional (default='helvetica') + Set dot file "font name" field + Output font cannot display optional + font name "sans" or other correct font Returns ------- @@ -772,7 +778,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: