Loading...

verticapy.machine_learning.vertica.ensemble.XGBClassifier

class verticapy.machine_learning.vertica.ensemble.XGBClassifier(name: str = None, overwrite_model: bool = False, max_ntree: int = 10, max_depth: int = 5, nbins: int = 32, split_proposal_method: Literal['local', 'global'] = 'global', tol: float = 0.001, learning_rate: float = 0.1, min_split_loss: float = 0.0, weight_reg: float = 0.0, sample: float = 1.0, col_sample_by_tree: float = 1.0, col_sample_by_node: float = 1.0)

Creates an XGBClassifier object using the Vertica XGB_CLASSIFIER algorithm.

Parameters

name: str, optional

Name of the model. The model is stored in the DB.

overwrite_model: bool, optional

If set to True, training a model with the same name as an existing model overwrites the existing model.

max_ntree: int, optional

Maximum number of trees that can be created.

max_depth: int, optional

aximum depth of each tree, an integer between 1 and 20, inclusive.

nbins: int, optional

Number of bins used to find splits in each column, where more splits leads to a longer runtime but more fine-grained, possibly better splits. Must be an integer between 2 and 1000, inclusive.

split_proposal_method: str, optional

Approximate splitting strategy, either global or local (not yet supported).

tol: float, optional

Approximation error of quantile summary structures used in the approximate split finding method.

learning_rate: float, optional

Weight applied to each tree’s prediction. This reduces each tree’s impact, allowing for later trees to contribute and keeping earlier trees from dominating.

min_split_loss: float, optional

Each split must improve the model’s objective function value by at least this much in order to avoid pruning. A value of 0 is the same as turning off this parameter (trees are still pruned based on positive / negative objective function values).

weight_reg: float, optional

Regularization term that is applied to the weights of the leaves in the regression tree. A higher value leads to more sparse/smooth weights, which often helps to prevent overfitting.

sample: float, optional

Fraction of rows used per iteration in training.

col_sample_by_tree: float, optional

float in the range (0,1] that specifies the fraction of columns (features), chosen at random, to use when building each tree.

col_sample_by_node: float, optional

float in the range (0,1] that specifies the fraction of columns (features), chosen at random, to use when evaluating each split.

Attributes

Many attributes are created during the fitting phase.

trees_: list of BinaryTreeClassifier

Tree models are instances of ` BinaryTreeClassifier, each possessing various attributes. For more detailed information, refer to the documentation for BinaryTreeClassifier.

features_importance_: numpy.array

The importance of features. It is calculated using the average gain of each tree. To determine the final score, VerticaPy sums the scores of each tree, normalizes them and applies an activation function to scale them. It is necessary to use the features_importance() method to compute it initially, and the computed values will be subsequently utilized for subsequent calls.

features_importance_trees_: dict of numpy.array

Each element of the array represents the feature importance of tree i. The importance of features is calculated using the average gain of each tree. It is necessary to use the features_importance() method to compute it initially, and the computed values will be subsequently utilized for subsequent calls.

logodds_: numpy.array

The log-odds. It quantifies the logarithm of the odds ratio, providing a measure of the likelihood of an event occurring.

eta_: float

The learning rate, is a crucial hyperparameter in machine learning algorithms. It determines the step size at each iteration during the model training process. A well-chosen learning rate is essential for achieving optimal convergence and preventing overshooting or slow convergence in the training phase. Adjusting the learning rate is often necessary to strike a balance between model accuracy and computational efficiency.

n_estimators_: int

The number of model estimators.

classes_: numpy.array

The classes labels.

Note

All attributes can be accessed using the get_attributes() method.

Note

Several other attributes can be accessed by using the get_vertica_attributes() method.

Examples

The following examples provide a basic understanding of usage. For more detailed examples, please refer to the Machine Learning or the Examples section on the website.

Important

Many tree-based models inherit from the XGB base class, and it’s recommended to use it directly for access to a wider range of options.

Load data for machine learning

We import verticapy:

import verticapy as vp

Hint

By assigning an alias to verticapy, we mitigate the risk of code collisions with other libraries. This precaution is necessary because verticapy uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from verticapy are used as intended without interfering with functions from other libraries.

For this example, we will use the winequality dataset.

import verticapy.datasets as vpd

data = vpd.load_winequality()
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
13.90.2250.44.20.0329.0118.00.9893.570.3612.881white
24.70.3350.141.30.03669.0168.00.992123.470.4610.550white
34.70.4550.181.90.03633.0106.00.987463.210.8314.071white
44.70.7850.03.40.03623.0134.00.989813.530.9213.860white
54.90.3450.341.00.06832.0143.00.991383.240.410.150white
64.90.3450.341.00.06832.0143.00.991383.240.410.150white
74.90.420.02.10.04816.042.00.991543.710.7414.071red
85.00.270.41.20.07642.0124.00.992043.320.4710.160white
95.00.310.06.40.04643.0166.00.9943.30.639.960white
105.00.40.54.30.04629.080.00.99023.490.6613.660red
115.00.440.0418.60.03938.0128.00.99853.370.5710.260white
125.10.110.321.60.02812.090.00.990083.570.5212.260white
135.10.140.250.70.03915.089.00.99193.220.439.260white
145.10.1650.225.70.04742.0146.00.99343.180.559.960white
155.10.330.221.60.02718.089.00.98933.510.3812.571white
165.10.330.221.60.02718.089.00.98933.510.3812.571white
175.10.330.221.60.02718.089.00.98933.510.3812.571white
185.10.390.211.70.02715.072.00.98943.50.4512.560white
195.20.20.273.20.04716.093.00.992353.440.5310.171white
205.20.210.311.70.04817.061.00.989533.240.3712.071white
215.20.220.466.20.06641.0187.00.993623.190.429.7333333333333350white
225.20.310.22.40.02727.0117.00.988863.560.4513.071white
235.20.320.251.80.10313.050.00.99573.380.559.250red
245.20.340.376.20.03142.0133.00.990763.250.4112.560white
255.20.360.021.60.03124.0104.00.98963.440.3512.260white
265.20.3650.0813.50.04137.0142.00.9973.460.399.960white
275.20.480.041.60.05419.0106.00.99273.540.6212.271red
285.20.50.182.00.03623.0129.00.989493.360.7713.471white
295.30.160.391.00.02840.0101.00.991563.570.5910.660white
305.30.160.391.00.02840.0101.00.991563.570.5910.660white
315.30.1650.241.10.05125.0105.00.99253.320.479.150white
325.30.230.560.90.04146.0141.00.991193.160.629.750white
335.30.30.31.20.02925.093.00.987423.310.413.671white
345.30.330.31.20.04825.0119.00.990453.320.6211.360white
355.30.360.276.30.02840.0132.00.991863.370.411.660white
365.30.360.276.30.02840.0132.00.991863.370.411.660white
375.30.40.253.90.03145.0130.00.990723.310.5811.7571white
385.30.470.112.20.04816.089.00.991823.540.8813.671red
395.30.470.112.20.04816.089.00.991823.540.8813.566666666666771red
405.30.7150.191.50.1617.062.00.993953.620.6111.050red
415.40.220.291.20.04569.0152.00.991783.760.6311.071white
425.40.5950.12.80.04226.080.00.99323.360.389.350white
435.40.740.091.70.08916.026.00.994023.670.5611.660red
445.50.120.331.00.03823.0131.00.991643.250.459.850white
455.50.120.331.00.03823.0131.00.991643.250.459.850white
465.50.140.274.60.02922.0104.00.99493.340.449.050white
475.50.140.274.60.02922.0104.00.99493.340.449.050white
485.50.160.311.20.02631.068.00.98983.330.4411.6560white
495.50.160.311.20.02631.068.00.98983.330.4411.633333333333360white
505.50.180.225.50.03710.086.00.991563.460.4412.250white
515.50.240.451.70.04622.0113.00.992243.220.4810.050white
525.50.290.31.10.02220.0110.00.988693.340.3812.871white
535.50.310.293.00.02716.0102.00.990673.230.5611.260white
545.50.320.454.90.02825.0191.00.99223.510.4911.571white
555.50.350.351.10.04514.0167.00.9923.340.689.960white
565.50.3750.381.70.03617.098.00.991423.290.3910.560white
575.60.150.265.550.05151.0139.00.993363.470.511.060white
585.60.150.315.30.0388.079.00.99233.30.3910.560white
595.60.160.271.40.04453.0168.00.99183.280.3710.160white
605.60.1750.290.80.04320.067.00.991123.280.489.960white
615.60.1850.197.10.04836.0110.00.994383.260.419.560white
625.60.1850.197.10.04836.0110.00.994383.260.419.560white
635.60.220.321.20.02429.097.00.988233.20.4613.0571white
645.60.260.181.40.03418.0135.00.991743.320.3510.260white
655.60.260.265.70.03112.080.00.99233.250.3810.850white
665.60.260.511.40.02925.093.00.994283.230.4910.560white
675.60.280.284.20.04452.0158.00.9923.350.4410.771white
685.60.30.16.40.04334.0142.00.993823.140.489.850white
695.60.350.145.00.04648.0198.00.99373.30.7110.350white
705.60.490.134.50.03917.0116.00.99073.420.913.771white
715.60.490.134.50.03917.0116.00.99073.420.913.771white
725.60.660.02.20.0873.011.00.993783.710.6312.871red
735.60.660.02.20.0873.011.00.993783.710.6312.871red
745.70.150.4711.40.03549.0128.00.994563.030.3410.581white
755.70.180.262.20.02321.095.00.98933.070.5412.360white
765.70.180.361.20.0469.071.00.991993.70.6810.971white
775.70.20.32.50.04638.0125.00.992763.340.59.960white
785.70.210.320.90.03838.0121.00.990743.240.4610.660white
795.70.210.374.50.0458.0140.00.993323.290.6210.660white
805.70.220.216.00.04441.0113.00.998623.220.468.960white
815.70.220.216.00.04441.0113.00.998623.220.468.960white
825.70.220.216.00.04441.0113.00.998623.220.468.960white
835.70.220.216.00.04441.0113.00.998623.220.468.960white
845.70.220.216.00.04441.0113.00.998623.220.468.960white
855.70.220.293.50.0427.0146.00.989993.170.3612.160white
865.70.230.289.650.02526.0121.00.99253.280.3811.360white
875.70.250.2612.50.04952.5106.00.996913.080.459.460white
885.70.250.2612.50.04952.5120.00.996913.080.459.460white
895.70.250.2711.50.0424.0120.00.994113.330.3110.860white
905.70.260.2417.80.05923.0124.00.997733.30.510.150white
915.70.260.2417.80.05923.0124.00.997733.30.510.150white
925.70.260.2417.80.05923.0124.00.997733.30.510.150white
935.70.270.321.20.04620.0155.00.99343.80.4110.260white
945.70.280.2417.50.04460.0167.00.99893.310.449.450white
955.70.320.181.40.02926.0104.00.99063.440.3711.060white
965.70.320.384.750.03323.094.00.9913.420.4211.871white
975.70.360.344.20.02621.077.00.99073.410.4511.960white
985.80.140.156.10.04227.0123.00.993623.060.69.960white
995.80.150.321.20.03714.0119.00.991373.190.510.260white
1005.80.170.341.80.04596.0170.00.990353.380.911.881white
Rows: 1-100 | Columns: 14

Note

VerticaPy offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the VerticaPy environment.

You can easily divide your dataset into training and testing subsets using the vDataFrame.train_test_split() method. This is a crucial step when preparing your data for machine learning, as it allows you to evaluate the performance of your models accurately.

data = vpd.load_winequality()
train, test = data.train_test_split(test_size = 0.2)

Warning

In this case, VerticaPy utilizes seeded randomization to guarantee the reproducibility of your data split. However, please be aware that this approach may lead to reduced performance. For a more efficient data split, you can use the vDataFrame.to_db() method to save your results into tables or temporary tables. This will help enhance the overall performance of the process.

Balancing the Dataset

In VerticaPy, balancing a dataset to address class imbalances is made straightforward through the balance() function within the preprocessing module. This function enables users to rectify skewed class distributions efficiently. By specifying the target variable and setting parameters like the method for balancing, users can effortlessly achieve a more equitable representation of classes in their dataset. Whether opting for over-sampling, under-sampling, or a combination of both, VerticaPy’s balance() function streamlines the process, empowering users to enhance the performance and fairness of their machine learning models trained on imbalanced data.

To balance the dataset, use the following syntax.

from verticapy.machine_learning.vertica.preprocessing import balance

balanced_train = balance(
    name = "my_schema.train_balanced",
    input_relation = train,
    y = "good",
    method = "hybrid",
)

Note

With this code, a table named train_balanced is created in the my_schema schema. It can then be used to train the model. In the rest of the example, we will work with the full dataset.

Hint

Balancing the dataset is a crucial step in improving the accuracy of machine learning models, particularly when faced with imbalanced class distributions. By addressing disparities in the number of instances across different classes, the model becomes more adept at learning patterns from all classes rather than being biased towards the majority class. This, in turn, enhances the model’s ability to make accurate predictions for under-represented classes. The balanced dataset ensures that the model is not dominated by the majority class and, as a result, leads to more robust and unbiased model performance. Therefore, by employing techniques such as over-sampling, under-sampling, or a combination of both during dataset preparation, practitioners can significantly contribute to achieving higher accuracy and better generalization of their machine learning models.

Model Initialization

First we import the XGBClassifier model:

from verticapy.machine_learning.vertica import XGBClassifier

Then we can create the model:

model = XGBClassifier(
    max_ntree = 3,
    max_depth = 3,
    nbins = 6,
    split_proposal_method = 'global',
    tol = 0.001,
    learning_rate = 0.1,
    min_split_loss = 0,
    weight_reg = 0,
    sample = 0.7,
    col_sample_by_tree = 1,
    col_sample_by_node = 1,
)

Hint

In verticapy 1.0.x and higher, you do not need to specify the model name, as the name is automatically assigned. If you need to re-use the model, you can fetch the model name from the model’s attributes.

Important

The model name is crucial for the model management system and versioning. It’s highly recommended to provide a name if you plan to reuse the model later.

Model Training

We can now fit the model:

model.fit(
    train,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density",
    ],
    "good",
    test,
)



===========
call_string
===========
xgb_classifier('"public"."_verticapy_tmp_xgbclassifier_v_mldb_7d8b2786979811efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_7dba7450979811efa8720242ac120002_"', '"good"', '"fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "density"' USING PARAMETERS exclude_columns='', max_ntree=3, max_depth=3, learning_rate=0.1, min_split_loss=0, weight_reg=0, nbins=6, objective=crossentropy, sampling_size=0.7, col_sample_by_tree=1, col_sample_by_node=1)

=======
details
=======
   predictor    |      type      
----------------+----------------
 fixed_acidity  |float or numeric
volatile_acidity|float or numeric
  citric_acid   |float or numeric
 residual_sugar |float or numeric
   chlorides    |float or numeric
    density     |float or numeric


==================
initial_prediction
==================
response_label| value  
--------------+--------
      0       | 0.00000
      1       | 0.00000


===============
Additional Info
===============
       Name       |Value
------------------+-----
    tree_count    |  3  
rejected_row_count|  0  
accepted_row_count|5200 

Important

To train a model, you can directly use the vDataFrame or the name of the relation stored in the database. The test set is optional and is only used to compute the test metrics. In verticapy, we don’t work using X matrices and y vectors. Instead, we work directly with lists of predictors and the response name.

Features Importance

We can conveniently get the features importance:

result = model.features_importance()

Note

In models such as XGBoost, feature importance is calculated using the average gain of each tree. To determine the final score, VerticaPy sums the scores of each tree, normalizes them and applies an activation function to scale them.

Metrics

We can get the entire report using:

model.report()
value
auc0.6544676806083651
prc_auc0.48533555468295325
accuracy0.8111025443330764
log_loss0.259887085975558
precision0.0
recall0.0
f1_score0.0
mcc0.0
informedness0.0
markedness-0.18889745566692362
csi0.0
Rows: 1-11 | Columns: 2

Important

Most metrics are computed using a single SQL query, but some of them might require multiple SQL queries. Selecting only the necessary metrics in the report can help optimize performance. E.g. model.report(metrics = ["auc", "accuracy"]).

For classification models, we can easily modify the cutoff to observe the effect on different metrics:

model.report(cutoff = 0.2)
value
auc0.6544676806083651
prc_auc0.48533555468295325
accuracy0.18889745566692367
log_loss0.259887085975558
precision0.18889745566692367
recall1.0
f1_score0.317769130998703
mcc0.0
informedness0.0
markedness-0.8111025443330764
csi0.18889745566692367
Rows: 1-11 | Columns: 2

You can also use the score() function to compute any classification metric. The default metric is the accuracy:

model.score()
Out[3]: 0.8111025443330764

Prediction

Prediction is straight-forward:

model.predict(
    test,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density",
    ],
    "prediction",
)
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
Abc
prediction
Varchar(1)
14.20.170.361.80.02993.0161.00.989993.650.8912.071white0
24.80.2250.381.20.07447.0130.00.991323.310.410.360white0
35.00.330.2311.80.0323.0158.00.993223.410.6411.860white0
45.00.4550.181.90.03633.0106.00.987463.210.8314.071white0
55.20.370.331.20.02813.081.00.99023.370.3811.760white0
65.30.210.290.70.02811.066.00.992153.30.49.850white0
75.40.220.356.50.02926.087.00.990923.290.4412.571white0
85.40.330.314.00.0327.0108.00.990313.30.4312.271white0
95.50.340.262.20.02131.0119.00.989193.550.4913.081white0
105.50.490.031.80.04428.087.00.99083.50.8214.081red0
115.60.120.332.90.04421.073.00.988963.170.3212.981white0
125.60.190.391.10.04317.067.00.99183.230.5310.360white0
135.60.240.342.00.04114.073.00.989813.040.4511.671white0
145.60.260.010.20.03813.0111.00.993153.440.4612.460white0
155.60.6150.01.60.08916.059.00.99433.580.529.950red0
165.70.150.283.70.04557.0151.00.99133.220.2711.260white0
175.70.160.321.20.0367.089.00.991113.260.4811.050white0
185.70.240.31.30.0325.098.00.989683.370.4312.471white0
195.70.270.169.00.05332.0111.00.994743.360.3710.460white0
205.70.440.137.00.02528.0173.00.99133.330.4812.560white0
215.80.180.281.30.0349.094.00.990923.210.5211.260white0
225.80.230.22.00.04339.0154.00.992263.210.3910.260white0
235.80.2750.35.40.04341.0149.00.99263.330.4210.871white0
245.80.340.217.20.04148.0146.00.994413.160.499.850white0
255.80.610.111.80.06618.028.00.994833.550.6610.960red0
265.90.120.274.80.0340.0110.00.992263.550.6812.160white0
275.90.240.121.40.03560.0247.00.993583.340.449.660white0
285.90.260.2112.50.03436.0152.00.99723.280.439.560white0
295.90.480.31.50.03719.078.00.990573.470.4211.971white0
305.90.610.082.10.07116.024.00.993763.560.7711.160red0
316.00.130.285.70.03856.0189.50.99483.590.4310.671white0
326.00.190.261.40.03930.0104.00.989983.320.4112.460white0
336.00.220.281.10.03447.090.00.988623.220.3812.660white0
346.00.220.281.10.03447.090.00.988623.220.3812.660white0
356.00.260.291.00.03227.096.00.98963.380.4412.360white0
366.00.270.151.50.05635.0128.00.99363.120.458.850white0
376.00.280.251.80.0428.0108.00.99293.080.559.050white0
386.00.280.496.80.04861.0222.00.99533.190.479.350white0
396.00.290.251.40.03330.0114.00.987943.080.4313.260white0
406.00.290.4110.80.04855.0149.00.99373.090.5910.966666666666771white0
416.00.310.473.60.06718.042.00.995493.390.6611.060red0
426.00.4950.275.00.15717.0129.00.993963.030.369.350white0
436.00.50.01.40.05715.026.00.994483.360.459.550red0
446.00.640.051.90.0669.017.00.994963.520.7810.650red0
456.10.20.171.60.04846.0129.00.9913.30.4311.460white0
466.10.210.381.50.03937.0122.00.989723.20.4312.060white0
476.10.220.2816.550.05954.0135.00.996653.20.3810.550white0
486.10.270.446.70.04161.0230.00.995053.120.48.950white0
496.10.280.274.70.0356.0140.00.990423.160.4212.581white0
506.10.280.274.70.0356.0140.00.990423.160.4212.581white0
516.10.310.342.80.04259.5162.00.991793.270.4710.860white0
526.10.320.252.30.07123.058.00.996333.420.9710.650red0
536.10.320.3310.70.03627.098.00.995213.340.5210.260white0
546.10.340.3112.00.05346.0238.00.99773.160.488.650white0
556.10.40.310.90.04823.0170.00.9933.220.779.560white0
566.20.220.271.50.06420.0132.00.99383.220.469.260white0
576.20.260.28.00.04735.0111.00.994453.110.4210.460white0
586.20.280.4313.00.03964.0233.00.997453.080.388.950white0
596.20.310.233.30.05234.0113.00.994293.160.488.450white0
606.20.320.244.10.05134.0149.00.993063.360.5211.050white0
616.20.3450.2710.10.05638.0187.00.994863.310.5610.650white0
626.20.350.2518.40.05128.0182.00.999463.130.629.360white0
636.30.270.380.90.0517.0140.00.99263.450.510.571white0
646.30.270.491.20.06335.092.00.99113.380.4212.260white0
656.30.280.248.450.03132.0172.00.99583.390.579.771white0
666.30.30.487.40.05334.0149.00.994723.180.539.850white0
676.30.310.342.20.04520.077.00.99273.30.4310.250white0
686.30.320.1717.750.0651.0190.00.999163.130.488.860white0
696.30.340.334.60.03419.080.00.99173.380.5812.071white0
706.30.360.34.80.04914.085.00.99323.280.3910.650white0
716.40.1050.291.10.03544.0140.00.991423.170.5510.771white0
726.40.210.215.10.09721.0105.00.99393.070.469.650white0
736.40.210.285.90.04729.0101.00.992783.150.411.060white0
746.40.220.341.80.05729.0104.00.99593.810.5710.360white0
756.40.220.497.50.05442.0151.00.99483.270.5210.160white0
766.40.230.331.150.04415.5217.50.9923.330.4411.060white0
776.40.240.268.20.05447.0182.00.995383.120.59.550white0
786.40.240.271.50.0435.0105.00.989143.130.312.460white0
796.40.250.571.00.06221.0122.00.992383.00.49.550white0
806.40.250.571.00.06221.0122.00.992383.00.49.550white0
816.40.250.747.80.04552.0209.00.99563.210.429.260white0
826.40.280.283.00.0419.098.00.992163.250.4711.160white0
836.40.310.2613.20.04657.0205.00.99753.170.419.650white0
846.40.310.3112.90.04555.0161.00.995463.020.5910.250white0
856.40.310.3112.90.04555.0161.00.995463.020.5910.250white0
866.40.360.532.20.2319.035.00.99343.370.9312.460red0
876.40.380.247.20.04741.0151.00.996043.110.69.250white0
886.40.790.042.20.06111.017.00.995883.530.6510.460red0
896.50.140.322.70.03718.089.00.99243.40.7411.571white0
906.50.150.4412.60.05265.0158.00.996883.260.710.371white0
916.50.290.527.90.04935.0192.00.995513.160.519.560white0
926.50.330.2414.50.04820.096.00.994563.060.311.581white0
936.50.350.3110.20.06958.0170.00.996923.180.499.450white0
946.50.40.12.00.07630.047.00.995543.360.489.460red0
956.50.610.02.20.09548.059.00.995413.610.711.560red0
966.60.210.295.350.02943.0106.00.991122.930.4311.571white0
976.60.230.261.30.04516.0128.00.99343.360.610.060white0
986.60.230.2914.450.05729.0144.00.997563.330.5410.260white0
996.60.250.3514.00.06942.0163.00.9993.560.479.850white0
1006.60.250.417.40.04329.0151.00.99463.150.610.271white0
Rows: 1-100 | Columns: 15

Note

Predictions can be made automatically using the test set, in which case you don’t need to specify the predictors. Alternatively, you can pass only the vDataFrame to the predict() function, but in this case, it’s essential that the column names of the vDataFrame match the predictors and response name in the model.

Probabilities

It is also easy to get the model’s probabilities:

model.predict_proba(
    test,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density",
    ],
    "prediction",
)
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
Abc
prediction
Varchar(1)
Abc
prediction_0
Varchar(128)
Abc
prediction_1
Varchar(128)
14.20.170.361.80.02993.0161.00.989993.650.8912.071white00.5510950.448905
24.80.2250.381.20.07447.0130.00.991323.310.410.360white00.5510950.448905
35.00.330.2311.80.0323.0158.00.993223.410.6411.860white00.5869440.413056
45.00.4550.181.90.03633.0106.00.987463.210.8314.071white00.5869440.413056
55.20.370.331.20.02813.081.00.99023.370.3811.760white00.5510950.448905
65.30.210.290.70.02811.066.00.992153.30.49.850white00.5510950.448905
75.40.220.356.50.02926.087.00.990923.290.4412.571white00.5510950.448905
85.40.330.314.00.0327.0108.00.990313.30.4312.271white00.5510950.448905
95.50.340.262.20.02131.0119.00.989193.550.4913.081white00.5869440.413056
105.50.490.031.80.04428.087.00.99083.50.8214.081red00.5869440.413056
115.60.120.332.90.04421.073.00.988963.170.3212.981white00.5510950.448905
125.60.190.391.10.04317.067.00.99183.230.5310.360white00.5510950.448905
135.60.240.342.00.04114.073.00.989813.040.4511.671white00.5510950.448905
145.60.260.010.20.03813.0111.00.993153.440.4612.460white00.5869440.413056
155.60.6150.01.60.08916.059.00.99433.580.529.950red00.5869440.413056
165.70.150.283.70.04557.0151.00.99133.220.2711.260white00.5510950.448905
175.70.160.321.20.0367.089.00.991113.260.4811.050white00.5510950.448905
185.70.240.31.30.0325.098.00.989683.370.4312.471white00.5510950.448905
195.70.270.169.00.05332.0111.00.994743.360.3710.460white00.5869440.413056
205.70.440.137.00.02528.0173.00.99133.330.4812.560white00.5869440.413056
215.80.180.281.30.0349.094.00.990923.210.5211.260white00.5510950.448905
225.80.230.22.00.04339.0154.00.992263.210.3910.260white00.5869440.413056
235.80.2750.35.40.04341.0149.00.99263.330.4210.871white00.5510950.448905
245.80.340.217.20.04148.0146.00.994413.160.499.850white00.5869440.413056
255.80.610.111.80.06618.028.00.994833.550.6610.960red00.5869440.413056
265.90.120.274.80.0340.0110.00.992263.550.6812.160white00.5869440.413056
275.90.240.121.40.03560.0247.00.993583.340.449.660white00.5869440.413056
285.90.260.2112.50.03436.0152.00.99723.280.439.560white00.6091130.390887
295.90.480.31.50.03719.078.00.990573.470.4211.971white00.5510950.448905
305.90.610.082.10.07116.024.00.993763.560.7711.160red00.5869440.413056
316.00.130.285.70.03856.0189.50.99483.590.4310.671white00.5510950.448905
326.00.190.261.40.03930.0104.00.989983.320.4112.460white00.5869440.413056
336.00.220.281.10.03447.090.00.988623.220.3812.660white00.5510950.448905
346.00.220.281.10.03447.090.00.988623.220.3812.660white00.5510950.448905
356.00.260.291.00.03227.096.00.98963.380.4412.360white00.5510950.448905
366.00.270.151.50.05635.0128.00.99363.120.458.850white00.5869440.413056
376.00.280.251.80.0428.0108.00.99293.080.559.050white00.5869440.413056
386.00.280.496.80.04861.0222.00.99533.190.479.350white00.5510950.448905
396.00.290.251.40.03330.0114.00.987943.080.4313.260white00.5869440.413056
406.00.290.4110.80.04855.0149.00.99373.090.5910.966666666666771white00.5510950.448905
416.00.310.473.60.06718.042.00.995493.390.6611.060red00.5510950.448905
426.00.4950.275.00.15717.0129.00.993963.030.369.350white00.5869440.413056
436.00.50.01.40.05715.026.00.994483.360.459.550red00.5869440.413056
446.00.640.051.90.0669.017.00.994963.520.7810.650red00.5869440.413056
456.10.20.171.60.04846.0129.00.9913.30.4311.460white00.5869440.413056
466.10.210.381.50.03937.0122.00.989723.20.4312.060white00.5510950.448905
476.10.220.2816.550.05954.0135.00.996653.20.3810.550white00.598930.40107
486.10.270.446.70.04161.0230.00.995053.120.48.950white00.5510950.448905
496.10.280.274.70.0356.0140.00.990423.160.4212.581white00.5869440.413056
506.10.280.274.70.0356.0140.00.990423.160.4212.581white00.5869440.413056
516.10.310.342.80.04259.5162.00.991793.270.4710.860white00.5510950.448905
526.10.320.252.30.07123.058.00.996333.420.9710.650red00.6195750.380425
536.10.320.3310.70.03627.098.00.995213.340.5210.260white00.5510950.448905
546.10.340.3112.00.05346.0238.00.99773.160.488.650white00.609490.39051
556.10.40.310.90.04823.0170.00.9933.220.779.560white00.5510950.448905
566.20.220.271.50.06420.0132.00.99383.220.469.260white00.5869440.413056
576.20.260.28.00.04735.0111.00.994453.110.4210.460white00.5869440.413056
586.20.280.4313.00.03964.0233.00.997453.080.388.950white00.598930.40107
596.20.310.233.30.05234.0113.00.994293.160.488.450white00.5869440.413056
606.20.320.244.10.05134.0149.00.993063.360.5211.050white00.5869440.413056
616.20.3450.2710.10.05638.0187.00.994863.310.5610.650white00.5869440.413056
626.20.350.2518.40.05128.0182.00.999463.130.629.360white00.6195750.380425
636.30.270.380.90.0517.0140.00.99263.450.510.571white00.5510950.448905
646.30.270.491.20.06335.092.00.99113.380.4212.260white00.5510950.448905
656.30.280.248.450.03132.0172.00.99583.390.579.771white00.6091130.390887
666.30.30.487.40.05334.0149.00.994723.180.539.850white00.5510950.448905
676.30.310.342.20.04520.077.00.99273.30.4310.250white00.5510950.448905
686.30.320.1717.750.0651.0190.00.999163.130.488.860white00.6195750.380425
696.30.340.334.60.03419.080.00.99173.380.5812.071white00.5510950.448905
706.30.360.34.80.04914.085.00.99323.280.3910.650white00.5510950.448905
716.40.1050.291.10.03544.0140.00.991423.170.5510.771white00.5510950.448905
726.40.210.215.10.09721.0105.00.99393.070.469.650white00.5869440.413056
736.40.210.285.90.04729.0101.00.992783.150.411.060white00.5510950.448905
746.40.220.341.80.05729.0104.00.99593.810.5710.360white00.598930.40107
756.40.220.497.50.05442.0151.00.99483.270.5210.160white00.5510950.448905
766.40.230.331.150.04415.5217.50.9923.330.4411.060white00.5510950.448905
776.40.240.268.20.05447.0182.00.995383.120.59.550white00.5869440.413056
786.40.240.271.50.0435.0105.00.989143.130.312.460white00.5869440.413056
796.40.250.571.00.06221.0122.00.992383.00.49.550white00.5510950.448905
806.40.250.571.00.06221.0122.00.992383.00.49.550white00.5510950.448905
816.40.250.747.80.04552.0209.00.99563.210.429.260white00.5510950.448905
826.40.280.283.00.0419.098.00.992163.250.4711.160white00.5510950.448905
836.40.310.2613.20.04657.0205.00.99753.170.419.650white00.6195750.380425
846.40.310.3112.90.04555.0161.00.995463.020.5910.250white00.5510950.448905
856.40.310.3112.90.04555.0161.00.995463.020.5910.250white00.5510950.448905
866.40.360.532.20.2319.035.00.99343.370.9312.460red00.5510950.448905
876.40.380.247.20.04741.0151.00.996043.110.69.250white00.6195750.380425
886.40.790.042.20.06111.017.00.995883.530.6510.460red00.6195750.380425
896.50.140.322.70.03718.089.00.99243.40.7411.571white00.5510950.448905
906.50.150.4412.60.05265.0158.00.996883.260.710.371white00.598930.40107
916.50.290.527.90.04935.0192.00.995513.160.519.560white00.5510950.448905
926.50.330.2414.50.04820.096.00.994563.060.311.581white00.5869440.413056
936.50.350.3110.20.06958.0170.00.996923.180.499.450white00.609490.39051
946.50.40.12.00.07630.047.00.995543.360.489.460red00.5869440.413056
956.50.610.02.20.09548.059.00.995413.610.711.560red00.5869440.413056
966.60.210.295.350.02943.0106.00.991122.930.4311.571white00.5510950.448905
976.60.230.261.30.04516.0128.00.99343.360.610.060white00.5869440.413056
986.60.230.2914.450.05729.0144.00.997563.330.5410.260white00.598930.40107
996.60.250.3514.00.06942.0163.00.9993.560.479.850white00.598930.40107
1006.60.250.417.40.04329.0151.00.99463.150.610.271white00.5510950.448905
Rows: 1-100 | Columns: 17

Note

Probabilities are added to the vDataFrame, and VerticaPy uses the corresponding probability function in SQL behind the scenes. You can use the pos_label parameter to add only the probability of the selected category.

Confusion Matrix

You can obtain the confusion matrix of your choice by specifying the desired cutoff.

model.confusion_matrix(cutoff = 0.5)
Out[4]: 
array([[1052,    0],
       [ 245,    0]])

Note

In classification, the cutoff is a threshold value used to determine class assignment based on predicted probabilities or scores from a classification model. In binary classification, if the predicted probability for a specific class is greater than or equal to the cutoff, the instance is assigned to the positive class; otherwise, it is assigned to the negative class. Adjusting the cutoff allows for trade-offs between true positives and false positives, enabling the model to be optimized for specific objectives or to consider the relative costs of different classification errors. The choice of cutoff is critical for tailoring the model’s performance to meet specific needs.

Main Plots (Classification Curves)

Classification models allow for the creation of various plots that are very helpful in understanding the model, such as the ROC Curve, PRC Curve, Cutoff Curve, Gain Curve, and more.

Most of the classification curves can be found in the Machine Learning - Classification Curve.

For example, let’s draw the model’s ROC curve.

model.roc_curve()

Important

Most of the curves have a parameter called nbins, which is essential for estimating metrics. The larger the nbins, the more precise the estimation, but it can significantly impact performance. Exercise caution when increasing this parameter excessively.

Hint

In binary classification, various curves can be easily plotted. However, in multi-class classification, it’s important to select the pos_label, representing the class to be treated as positive when drawing the curve.

Other Plots

Tree models can be visualized by drawing their tree plots. For more examples, check out Machine Learning - Tree Plots.

model.plot_tree()
../_images/machine_learning_vertica_tree_xgb_classifier_.png

Note

The above example may not render properly in the doc because of the huge size of the tree. But it should render nicely in jupyter environment.

In order to plot graph using graphviz separately, you can extract the graphviz DOT file code as follows:

model.to_graphviz()
Out[5]: 'digraph Tree {\ngraph [bgcolor="#FFFFFFDD"];\n0 [label="\\"density\\"", shape="box", style="filled", fillcolor="#FFFFFFDD", fontcolor="#000000", color="#000000"]\n0 -> 1 [label="<= 0.995772", color="#000000", fontcolor="#000000"]\n0 -> 2 [label="> 0.995772", color="#000000", fontcolor="#000000"]\n1 [label="\\"citric_acid\\"", shape="box", style="filled", fillcolor="#FFFFFFDD", fontcolor="#000000", color="#000000"]\n1 -> 3 [label="<= 0.276667", color="#000000", fontcolor="#000000"]\n1 -> 4 [label="> 0.276667", color="#000000", fontcolor="#000000"]\n2 [label="\\"volatile_acidity\\"", shape="box", style="filled", fillcolor="#FFFFFFDD", fontcolor="#000000", color="#000000"]\n2 -> 5 [label="<= 0.288333", color="#000000", fontcolor="#000000"]\n2 -> 6 [label="> 0.288333", color="#000000", fontcolor="#000000"]\n3 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#87cefa" color="#000000"><FONT color="#000000"><b>prediction: 0 </b></FONT></td></tr><tr><td port="port0" border="1" align="left" color="#000000"><FONT color="#000000">logodds(0): 1.28</FONT></td></tr><tr><td port="port1" border="1" align="left" color="#000000"><FONT color="#000000">logodds(1): -1.28</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="#87cefa" color="#000000"><FONT color="#000000"><b>prediction: 0 </b></FONT></td></tr><tr><td port="port0" border="1" align="left" color="#000000"><FONT color="#000000">logodds(0): 0.78</FONT></td></tr><tr><td port="port1" border="1" align="left" color="#000000"><FONT color="#000000">logodds(1): -0.78</FONT></td></tr></table>>, fillcolor="#FFFFFFDD", fontcolor="#000000", shape="none", color="#000000"]\n5 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#87cefa" color="#000000"><FONT color="#000000"><b>prediction: 0 </b></FONT></td></tr><tr><td port="port0" border="1" align="left" color="#000000"><FONT color="#000000">logodds(0): 1.32</FONT></td></tr><tr><td port="port1" border="1" align="left" color="#000000"><FONT color="#000000">logodds(1): -1.32</FONT></td></tr></table>>, fillcolor="#FFFFFFDD", fontcolor="#000000", shape="none", color="#000000"]\n6 [label=<<table border="0" cellspacing="0"> <tr><td port="port1" border="1" bgcolor="#87cefa" color="#000000"><FONT color="#000000"><b>prediction: 0 </b></FONT></td></tr><tr><td port="port0" border="1" align="left" color="#000000"><FONT color="#000000">logodds(0): 1.76</FONT></td></tr><tr><td port="port1" border="1" align="left" color="#000000"><FONT color="#000000">logodds(1): -1.76</FONT></td></tr></table>>, fillcolor="#FFFFFFDD", fontcolor="#000000", shape="none", color="#000000"]\n}'

This string can then be copied into a DOT file which can beparsed by graphviz.

Contour plot is another useful plot that can be produced for models with two predictors.

model.contour()

Important

Machine learning models with two predictors can usually benefit from their own contour plot. This visual representation aids in exploring predictions and gaining a deeper understanding of how these models perform in different scenarios. Please refer to Contour Plot for more examples.

Parameter Modification

In order to see the parameters:

model.get_params()
Out[6]: 
{'max_ntree': 3,
 'max_depth': 3,
 'nbins': 6,
 'split_proposal_method': 'global',
 'tol': 0.001,
 'learning_rate': 0.1,
 'min_split_loss': 0,
 'weight_reg': 0,
 'sample': 0.7,
 'col_sample_by_tree': 1,
 'col_sample_by_node': 1}

And to manually change some of the parameters:

model.set_params({'max_depth': 5})

Model Register

In order to register the model for tracking and versioning:

model.register("model_v1")

Please refer to /notebooks/ml/model_tracking_versioning/index.ipynb for more details on model tracking and versioning.

Model Exporting

To Memmodel

model.to_memmodel()

Note

MemModel objects serve as in-memory representations of machine learning models. They can be used for both in-database and in-memory prediction tasks. These objects can be pickled in the same way that you would pickle a scikit-learn model.

The preceding methods for exporting the model use MemModel, and it is recommended to use MemModel directly.

To SQL

You can get the SQL query equivalent of the XGB model by:

model.to_sql()
Out[8]: '(CASE WHEN (1 / (1 + EXP(- (-1.3959376373916976 + 0.1 * ((CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN -1.28077 ELSE -0.776 END) ELSE (CASE WHEN "volatile_acidity" < 0.288333 THEN -1.3165 ELSE -1.7581 END) END) + (CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN -1.14298 ELSE -0.639964 END) ELSE (CASE WHEN "citric_acid" < 0.276667 THEN -1.69394 ELSE -1.2682 END) END) + (CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN -1.08972 ELSE -0.635001 END) ELSE (CASE WHEN "fixed_acidity" < 9.85 THEN -1.4254 ELSE -0.825923 END) END)))))) / ((1 / (1 + EXP(- (1.3959376373916976 + 0.1 * ((CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN 1.28077 ELSE 0.776 END) ELSE (CASE WHEN "volatile_acidity" < 0.288333 THEN 1.3165 ELSE 1.7581 END) END) + (CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN 1.14298 ELSE 0.639964 END) ELSE (CASE WHEN "citric_acid" < 0.276667 THEN 1.69394 ELSE 1.2682 END) END) + (CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN 1.08972 ELSE 0.635001 END) ELSE (CASE WHEN "fixed_acidity" < 9.85 THEN 1.4254 ELSE 0.825923 END) END)))))) + (1 / (1 + EXP(- (-1.3959376373916976 + 0.1 * ((CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN -1.28077 ELSE -0.776 END) ELSE (CASE WHEN "volatile_acidity" < 0.288333 THEN -1.3165 ELSE -1.7581 END) END) + (CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN -1.14298 ELSE -0.639964 END) ELSE (CASE WHEN "citric_acid" < 0.276667 THEN -1.69394 ELSE -1.2682 END) END) + (CASE WHEN "density" < 0.995772 THEN (CASE WHEN "citric_acid" < 0.276667 THEN -1.08972 ELSE -0.635001 END) ELSE (CASE WHEN "fixed_acidity" < 9.85 THEN -1.4254 ELSE -0.825923 END) END))))))) > 0.5 THEN 1 ELSE 0 END)'

Note

This SQL query can be directly used in any database.

Deploy SQL

To get the SQL query which uses Vertica functions use below:

model.deploySQL()
Out[9]: 'PREDICT_XGB_CLASSIFIER("fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "density" USING PARAMETERS model_name = \'"public"."_verticapy_tmp_xgbclassifier_v_mldb_7d8b2786979811efa8720242ac120002_"\', match_by_pos = \'true\')'

To Python

To obtain the prediction function in Python syntax, use the following code:

X = [[4.2, 0.17, 0.36, 1.8, 0.029, 0.9899]]

model.to_python()(X)
Out[11]: array([0])

Hint

The to_python() method is used to retrieve predictions, probabilities, or cluster distances. For specific details on how to use this method for different model types, refer to the relevant documentation for each model.

__init__(name: str = None, overwrite_model: bool = False, max_ntree: int = 10, max_depth: int = 5, nbins: int = 32, split_proposal_method: Literal['local', 'global'] = 'global', tol: float = 0.001, learning_rate: float = 0.1, min_split_loss: float = 0.0, weight_reg: float = 0.0, sample: float = 1.0, col_sample_by_tree: float = 1.0, col_sample_by_node: float = 1.0) None

Must be overridden in the child class

Methods

__init__([name, overwrite_model, max_ntree, ...])

Must be overridden in the child class

classification_report([metrics, cutoff, ...])

Computes a classification report using multiple model evaluation metrics (auc, accuracy, f1...).

confusion_matrix([pos_label, cutoff])

Computes the model confusion matrix.

contour([pos_label, nbins, chart])

Draws the model's contour plot.

cutoff_curve([pos_label, nbins, show, chart])

Draws the model Cutoff curve.

deploySQL([X, pos_label, cutoff, allSQL])

Returns the SQL code needed to deploy the model.

does_model_exists(name[, raise_error, ...])

Checks whether the model is stored in the Vertica database.

drop()

Drops the model from the Vertica database.

export_models(name, path[, kind])

Exports machine learning models.

features_importance([tree_id, show, chart])

Computes the model's features importance.

fit(input_relation, X, y[, test_relation, ...])

Trains the model.

get_attributes([attr_name])

Returns the model attributes.

get_match_index(x, col_list[, str_check])

Returns the matching index.

get_params()

Returns the parameters of the model.

get_plotting_lib([class_name, chart, ...])

Returns the first available library (Plotly, Matplotlib, or Highcharts) to draw a specific graphic.

get_score([tree_id])

Returns the feature importance metrics for the input tree.

get_tree([tree_id])

Returns a table with all the input tree information.

get_vertica_attributes([attr_name])

Returns the model Vertica attributes.

import_models(path[, schema, kind])

Imports machine learning models.

lift_chart([pos_label, nbins, show, chart])

Draws the model Lift Chart.

plot([max_nb_points, chart])

Draws the model.

plot_tree([tree_id, pic_path])

Draws the input tree.

prc_curve([pos_label, nbins, show, chart])

Draws the model PRC curve.

predict(vdf[, X, name, cutoff, inplace])

Predicts using the input relation.

predict_proba(vdf[, X, name, pos_label, inplace])

Returns the model's probabilities using the input relation.

register(registered_name[, raise_error])

Registers the model and adds it to in-DB Model versioning environment with a status of 'under_review'.

report([metrics, cutoff, labels, nbins])

Computes a classification report using multiple model evaluation metrics (auc, accuracy, f1...).

roc_curve([pos_label, nbins, show, chart])

Draws the model ROC curve.

score([metric, average, pos_label, cutoff, ...])

Computes the model score.

set_params([parameters])

Sets the parameters of the model.

summarize()

Summarizes the model.

to_binary(path)

Exports the model to the Vertica Binary format.

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

Returns the code for a Graphviz tree.

to_json([path])

Creates a Python XGBoost JSON file

to_memmodel()

Converts the model to an InMemory object that can be used for different types of predictions.

to_pmml(path)

Exports the model to PMML.

to_python([return_proba, ...])

Returns the Python function needed for in-memory scoring without using built-in Vertica functions.

to_sql([X, return_proba, ...])

Returns the SQL code needed to deploy the model without using built-in Vertica functions.

to_tf(path)

Exports the model to the Frozen Graph format (TensorFlow).

Attributes