vastorbit.machine_learning.memmodel.ensemble.RandomForestRegressor¶
- class vastorbit.machine_learning.memmodel.ensemble.RandomForestRegressor(trees: list[BinaryTreeRegressor])¶
InMemoryModelimplementation of the random forest regressor algorithm.- Parameters:
trees (list[BinaryTreeRegressor]) – list of
BinaryTreesfor regression.- Variables:
input (Attributes are identical to the)
underscore (parameters, followed by an)
('_').
Examples
Initalization
A Random Forest Regressor model is an ensemble of multiple binary tree regressor models. In this example, we will create three
BinaryTreeRegressormodels:from vastorbit.machine_learning.memmodel.tree import BinaryTreeRegressor model1 = BinaryTreeRegressor( 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.0, 11.0, 23.5], ) model2 = BinaryTreeRegressor( 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, 12, 56], ) model3 = BinaryTreeRegressor( 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, 1, 3, 6], )
Now we will use above models to create
RandomForestRegressormodel.from vastorbit.machine_learning.memmodel.ensemble import RandomForestRegressor model_rfr = RandomForestRegressor(trees = [model1, model2, model3])
Create a dataset.
data = [["male", 100], ["female", 20], ["female", 50]]
Making In-Memory Predictions
Use
predict()method to do predictions.model_rfr.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_rfr.predict_sql(cnames)
Hint
This object can be pickled and used in any in-memory environment, just like scikit-learn models.
Drawing Trees
Use
plot_tree()method to draw the input tree.model_rfr.plot_tree(tree_id = 0)
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__(trees: list[BinaryTreeRegressor]) None¶
Methods
__init__(trees)Returns the model attributes.
plot_tree([pic_path, tree_id])Draws the input tree.
predict(X)Predicts using the Random Forest regressor model.
predict_sql(X)Returns the SQL code needed to deploy the model.
set_attributes(**kwargs)Sets the model attributes.
Attributes
Must be overridden in child class