Loading...

verticapy.machine_learning.memmodel.tree.BinaryTreeRegressor.to_graphviz

BinaryTreeRegressor.to_graphviz(feature_names: Annotated[list | ndarray, 'Array Like Structure'] | None = None, classes_color: Annotated[list | ndarray, 'Array Like Structure'] | None = None, round_pred: int = 2, percent: bool = False, vertical: bool = True, node_style: dict | None = None, edge_style: dict | None = None, leaf_style: dict | None = None) str

Returns the code for a Graphviz tree.

Parameters

feature_names: ArrayLike, optional

List of the names of each feature.

classes_color: ArrayLike, optional

Colors that represent the different classes.

round_pred: int, optional

The number of decimals to round the prediction to. 0 rounds to an integer.

percent: bool, optional

If set to True, the probabilities are returned as percents.

vertical: bool, optional

If set to True, the function generates a vertical tree.

node_style: dict, optional

dictionary of options to customize each node of the tree. For a list of options, see the: Graphviz API .

edge_style: dict, optional

dictionary of options to customize each edge of the tree. For a list of options, see the: Graphviz API .

leaf_style: dict, optional

dictionary of options to customize each leaf of the tree. For a list of options, see the: Graphviz API .

Returns

str

Graphviz code.

Examples

Import the required module.

from verticapy.machine_learning.memmodel.tree import BinaryTreeClassifier

We will use the following attributes:

# Different Attributes
children_left = [1, 3, None, None, None]

children_right = [2, 4, None, None, None]

feature = [0, 1, None, None, None]

threshold = ["female", 30, None, None, None]

value = [
    None,
    None,
    [0.8, 0.1, 0.1],
    [0.1, 0.8, 0.1],
    [0.2, 0.2, 0.6],
]


classes = ["a", "b", "c"]

Let’s create a model.

# Building the Model
model_btc = BinaryTreeClassifier(
    children_left = children_left,
    children_right = children_right,
    feature = feature,
    threshold = threshold,
    value = value,
    classes = classes,
)

Get the model Graphviz representation.

model_btc.to_graphviz()
Out[82]: 'digraph Tree {\ngraph [bgcolor="#FFFFFFDD"];\n0 [label="X0", shape="box", style="filled", fillcolor="#FFFFFFDD", fontcolor="#000000", color="#000000"]\n0 -> 1 [label="= female", color="#000000", fontcolor="#000000"]\n0 -> 2 [label="!= female", color="#000000", fontcolor="#000000"]\n1 [label="X1", shape="box", style="filled", fillcolor="#FFFFFFDD", fontcolor="#000000", color="#000000"]\n1 -> 3 [label="<= 30", color="#000000", fontcolor="#000000"]\n1 -> 4 [label="> 30", color="#000000", fontcolor="#000000"]\n2 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#87cefa" color="#000000"><FONT color="#000000"><b>prediction: a </b></FONT></td></tr><tr><td port="port0" border="1" align="left" color="#000000"><FONT color="#000000">prob(a): 0.8</FONT></td></tr><tr><td port="port1" border="1" align="left" color="#000000"><FONT color="#000000">prob(b): 0.1</FONT></td></tr><tr><td port="port2" border="1" align="left" color="#000000"><FONT color="#000000">prob(c): 0.1</FONT></td></tr></table>>, fillcolor="#FFFFFFDD", fontcolor="#000000", shape="none", color="#000000"]\n3 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#efc5b5" color="#000000"><FONT color="#000000"><b>prediction: b </b></FONT></td></tr><tr><td port="port0" border="1" align="left" color="#000000"><FONT color="#000000">prob(a): 0.1</FONT></td></tr><tr><td port="port1" border="1" align="left" color="#000000"><FONT color="#000000">prob(b): 0.8</FONT></td></tr><tr><td port="port2" border="1" align="left" color="#000000"><FONT color="#000000">prob(c): 0.1</FONT></td></tr></table>>, fillcolor="#FFFFFFDD", fontcolor="#000000", shape="none", color="#000000"]\n4 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#d4ede3" color="#000000"><FONT color="#000000"><b>prediction: c </b></FONT></td></tr><tr><td port="port0" border="1" align="left" color="#000000"><FONT color="#000000">prob(a): 0.2</FONT></td></tr><tr><td port="port1" border="1" align="left" color="#000000"><FONT color="#000000">prob(b): 0.2</FONT></td></tr><tr><td port="port2" border="1" align="left" color="#000000"><FONT color="#000000">prob(c): 0.6</FONT></td></tr></table>>, fillcolor="#FFFFFFDD", fontcolor="#000000", shape="none", color="#000000"]\n}'

Important

For this example, a specific model is utilized, and it may not correspond exactly to the model you are working with. To see a comprehensive example specific to your class of interest, please refer to that particular class.