Loading...

verticapy.machine_learning.memmodel.tree.BinaryTreeClassifier

class verticapy.machine_learning.memmodel.tree.BinaryTreeClassifier(children_left: Annotated[list | ndarray, 'Array Like Structure'], children_right: Annotated[list | ndarray, 'Array Like Structure'], feature: Annotated[list | ndarray, 'Array Like Structure'], threshold: Annotated[list | ndarray, 'Array Like Structure'], value: Annotated[list | ndarray, 'Array Like Structure'], classes: Annotated[list | ndarray, 'Array Like Structure'] | None = None)

InMemoryModel implementation of binary trees for classification.

Parameters

children_left: ArrayLike

A list of node IDs, where children_left[i] is the node id of the left child of node i.

children_right: ArrayLike

A list of node IDs, where children_right[i] is the node id of the right child of node i.

feature: ArrayLike

A list of features, where feature[i] is the feature to split on for the internal node i.

threshold: ArrayLike

A list of thresholds, where threshold[i] is the threshold for the internal node i.

value: ArrayLike

Contains the constant prediction value of each node. If used for classification and return_proba is set to True, each element of the list must be a sublist with the probabilities of each class.

classes: ArrayLike, optional

The classes for the binary tree model.

Attributes

Attributes are identical to the input parameters, followed by an underscore (‘_’).

Examples

Initalization

Import the required module.

from verticapy.machine_learning.memmodel.tree import BinaryTreeClassifier

A BinaryTreeClassifier tree model is defined by its left and right child node id’s, feature and threshold value to split a node. Final values at leaf nodes and name of classes are also required. Let’s create a BinaryTreeClassifier model.

# 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"]

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

Create a dataset.

data = [["male", 100], ["female", 20], ["female", 50]]

Making In-Memory Predictions

Use predict() method to do predictions.

model_btc.predict(data)
Out[10]: array(['a', 'b', 'c'], dtype='<U1')

Use predict_proba() method to compute the predicted probabilities for each class.

model_btc.predict_proba(data)
Out[11]: 
array([[0.8, 0.1, 0.1],
       [0.1, 0.8, 0.1],
       [0.2, 0.2, 0.6]])

Deploy SQL Code

Let’s use the following column names:

cnames = ["sex", "fare"]

Use predict_sql() method to get the SQL code needed to deploy the model using its attributes.

model_btc.predict_sql(cnames)
Out[13]: "(CASE WHEN sex = 'female' THEN (CASE WHEN fare < 30 THEN 'b' ELSE 'c' END) ELSE 'a' END)"

Use predict_proba_sql() method to get the SQL code needed to deploy the model that computes predicted probabilities.

model_btc.predict_proba_sql(cnames)
Out[14]: 
["(CASE WHEN sex = 'female' THEN (CASE WHEN fare < 30 THEN 0.1 ELSE 0.2 END) ELSE 0.8 END)",
 "(CASE WHEN sex = 'female' THEN (CASE WHEN fare < 30 THEN 0.8 ELSE 0.2 END) ELSE 0.1 END)",
 "(CASE WHEN sex = 'female' THEN (CASE WHEN fare < 30 THEN 0.1 ELSE 0.6 END) ELSE 0.1 END)"]

Hint

This object can be pickled and used in any in-memory environment, just like SKLEARN models.

Drawing Tree

Use to_graphviz() method to generate code for a Graphviz tree.

model_btc.to_graphviz()
Out[15]: '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}'

Use plot_tree() method to draw the input tree.

model_btc.plot_tree()
../_images/machine_learning_memmodel_tree_binarytreeclassifier.png

Important

plot_tree() requires the Graphviz module.

Note

The above example is a very basic one. For other more detailed examples and customization options, please see :ref:`chart_gallery.tree`_

__init__(children_left: Annotated[list | ndarray, 'Array Like Structure'], children_right: Annotated[list | ndarray, 'Array Like Structure'], feature: Annotated[list | ndarray, 'Array Like Structure'], threshold: Annotated[list | ndarray, 'Array Like Structure'], value: Annotated[list | ndarray, 'Array Like Structure'], classes: Annotated[list | ndarray, 'Array Like Structure'] | None = None) → None

Methods

__init__(children_left, children_right, ...)

get_attributes()

Returns the model attributes.

plot_tree([pic_path])

Draws the input tree.

predict(X)

Predicts using the BinaryTree model.

predict_proba(X)

Returns the model probabilities.

predict_proba_sql(X)

Returns the SQL code needed to deploy the model probabilities.

predict_sql(X)

Returns the SQL code needed to deploy the model.

set_attributes(**kwargs)

Sets the model attributes.

to_graphviz([feature_names, classes_color, ...])

Returns the code for a Graphviz tree.

Attributes

object_type

Must be overridden in child class