Loading...

verticapy.machine_learning.memmodel.ensemble.XGBRegressor#

class verticapy.machine_learning.memmodel.ensemble.XGBRegressor(trees: list[BinaryTreeRegressor], mean: float = 0.0, eta: float = 1.0)#

InMemoryModel implementation of the XGBoost regressor algorithm.

Parameters#

trees: list[BinaryTreeRegressor]

list of BinaryTrees for regression.

mean: float, optional

Average of the response column.

eta: float, optional

Learning rate.

Attributes#

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

Examples#

Initalization

A model is an ensemble of multiple binary tree regressors. In this example, we will create three BinaryTreeRegressor models.

from verticapy.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, 11, 23],
)


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 XGBRegressor model.

from verticapy.machine_learning.memmodel.ensemble import XGBRegressor

model_xgbr = XGBRegressor(
    trees = [model1, model2, model3],
    mean = 2.5,
    eta = 0.9,
)

Note

We have used mean that represents average of the response column and eta that represents learning rate of XGBoost regressor model. Both are optional parameters.

Create a dataset.

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

Making In-Memory Predictions

Use predict() method to do predictions.

model_xgbr.predict(data)
Out[8]: array([ 3.4, 25.9, 79. ])

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_xgbr.predict_sql(cnames)
Out[10]: "((CASE WHEN sex = 'female' THEN (CASE WHEN fare < 30 THEN 11 ELSE 23 END) ELSE 3 END) + (CASE WHEN sex = 'female' THEN (CASE WHEN fare < 30 THEN 12 ELSE 56 END) ELSE -3 END) + (CASE WHEN sex = 'female' THEN (CASE WHEN fare < 30 THEN 3 ELSE 6 END) ELSE 1 END)) * 0.9 + 2.5"

Hint

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

Drawing Trees

Use plot_tree() method to draw the input tree.

model_xgbr.plot_tree(tree_id = 0)
../_images/machine_learning_memmodel_tree_xgbreg.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__(trees: list[BinaryTreeRegressor], mean: float = 0.0, eta: float = 1.0) None#

Methods

__init__(trees[, mean, eta])

get_attributes()

Returns the model attributes.

plot_tree([pic_path, tree_id])

Draws the input tree.

predict(X)

Predicts using the XGBRegressor model.

predict_sql(X)

Returns the SQL code needed to deploy the model.

set_attributes(**kwargs)

Sets the model attributes.

Attributes

object_type

Must be overridden in child class