Model.to_graphviz

In [ ]:
Model.to_graphviz(tree_id: int = 0,
                  classes_color: list = [],
                  round_pred: int = 2,
                  percent: bool = False,
                  vertical: bool = True,
                  node_style: dict = {},
                  arrow_style: dict = {},
                  leaf_style: dict = {},)

Returns the code for a Graphviz tree.

Parameters

Name Type Optional Description
tree_id
int
Unique tree identifier. It is an integer between 0 and n_estimators - 1
classes_color
list
Colors that represent the different classes.
round_pred
int
The number of decimals to round the prediction to. 0 rounds to an integer.
percent
bool
If set to True, the probabilities are returned as a percent.
vertical
bool
If set to True, the function generates a vertical tree.
node_style
dict
Dictionary of options to customize each node of the tree. For a list of options, see the Graphviz API: https://graphviz.org/doc/info/attrs.html
arrow_style
dict
Dictionary of options to customize each arrow of the tree. For a list of options, see the Graphviz API: https://graphviz.org/doc/info/attrs.html
leaf_style
dict
Dictionary of options to customize each leaf of the tree. For a list of options, see the Graphviz API: https://graphviz.org/doc/info/attrs.html

Example

In [19]:
from verticapy.learn.ensemble import RandomForestClassifier
model = RandomForestClassifier(name = "public.RF_iris",
                               n_estimators = 20,
                               max_features = "auto",
                               max_leaf_nodes = 32, 
                               sample = 0.7,
                               max_depth = 3,
                               min_samples_leaf = 5,
                               min_info_gain = 0.0,
                               nbins = 32)
model.fit("public.iris", 
          ["PetalLengthCm", "PetalWidthCm"], 
          "Species")
model.to_graphviz(tree_id = 3)
Out[19]:
'digraph Tree{\n0 [label="\\"PetalLengthCm\\"", shape="box", style="filled"]\n0 -> 1 [label="<= 1.921875"]\n0 -> 2 [label="> 1.921875"]\n1 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#87cefa"><b> prediction: Iris-setosa </b></td></tr><tr><td port="port0" border="1" align="left"> prob(Iris-setosa): 1.0 </td></tr><tr><td port="port1" border="1" align="left"> prob(Iris-versicolor): 0.0 </td></tr><tr><td port="port2" border="1" align="left"> prob(Iris-virginica): 0.0 </td></tr></table>>, shape="none"]\n2 [label="\\"PetalWidthCm\\"", shape="box", style="filled"]\n2 -> 3 [label="<= 1.75"]\n2 -> 4 [label="> 1.75"]\n3 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#efc5b5"><b> prediction: Iris-versicolor </b></td></tr><tr><td port="port0" border="1" align="left"> prob(Iris-setosa): 0.06 </td></tr><tr><td port="port1" border="1" align="left"> prob(Iris-versicolor): 0.89 </td></tr><tr><td port="port2" border="1" align="left"> prob(Iris-virginica): 0.06 </td></tr></table>>, shape="none"]\n4 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#d4ede3"><b> prediction: Iris-virginica </b></td></tr><tr><td port="port0" border="1" align="left"> prob(Iris-setosa): 0.02 </td></tr><tr><td port="port1" border="1" align="left"> prob(Iris-versicolor): 0.02 </td></tr><tr><td port="port2" border="1" align="left"> prob(Iris-virginica): 0.96 </td></tr></table>>, shape="none"]\n}'