vastorbit.machine_learning.memmodel.tree.BinaryTreeRegressor¶
- class vastorbit.machine_learning.memmodel.tree.BinaryTreeRegressor(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'])¶
InMemoryModelimplementation of binary trees for regression.- 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 thethresholdfor the internal node i.value (ArrayLike) – Contains the constant prediction value of each node. If used for classification and
return_probais set toTrue, each element of thelistmust be a sublist with the probabilities of each class.
- Variables:
input (Attributes are identical to the)
underscore (parameters, followed by an)
('_').
Examples
Initalization
Import the required module.
from vastorbit.machine_learning.memmodel.tree import BinaryTreeRegressor
A
BinaryTreeRegressormodel is defined by its left and right child node id’s,featureandthresholdvalue to split a node. Final values at leaf nodes are also required.Let’s create a
BinaryTreeRegressormodel:from vastorbit.machine_learning.memmodel.tree import BinaryTreeRegressor # 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, 3, 11, 1993] # Building the Model model_btr = BinaryTreeRegressor( children_left = children_left, children_right = children_right, feature = feature, threshold = threshold, value = value, )
Create a dataset.
data = [["male", 100], ["female", 20], ["female", 50]]
Making In-Memory Predictions
Use
predict()method to do predictions.model_btr.predict(data)
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_btr.predict_sql(cnames)
Hint
This object can be pickled and used in any in-memory environment, just like scikit-learn models.
Drawing Tree
Use
to_graphviz()method to generate code for a Graphviz tree.model_btr.to_graphviz()
Use
plot_tree()method to draw the input tree.model_btr.plot_tree()
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 Machine Learning - Tree Plots
- __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']) None¶
Methods
__init__(children_left, children_right, ...)Returns the model attributes.
plot_tree([pic_path])Draws the input tree.
predict(X)Predicts using the
BinaryTreemodel.Returns the model probabilities.
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
Must be overridden in child class