Loading...

verticapy.machine_learning.vertica.linear_model.LogisticRegression

class verticapy.machine_learning.vertica.linear_model.LogisticRegression(name: str = None, overwrite_model: bool = False, penalty: Literal['none', 'l1', 'l2', 'enet', None] = 'none', tol: float = 1e-06, C: Annotated[int | float | Decimal, 'Python Numbers'] = 1.0, max_iter: int = 100, solver: Literal['newton', 'bfgs', 'cgd'] = 'newton', l1_ratio: float = 0.5, fit_intercept: bool = True)

Creates a LogisticRegression object using the Vertica Logistic Regression algorithm.

Parameters

name: str, optional

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

overwrite_model: bool, optional

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

penalty: str, optional

Determines the method of regularization.

  • None:

    No Regularization.

  • l1:

    L1 Regularization.

  • l2:

    L2 Regularization.

  • enet:

    Combination between L1 and L2.

tol: float, optional

Determines whether the algorithm has reached the specified accuracy result.

C: PythonNumber, optional

The regularization parameter value. The value must be zero or non-negative.

max_iter: int, optional

Determines the maximum number of iterations the algorithm performs before achieving the specified accuracy result.

solver: str, optional

The optimizer method used to train the model.

  • newton:

    Newton Method.

  • bfgs:

    Broyden Fletcher Goldfarb Shanno.

  • cgd:

    Coordinate Gradient Descent.

l1_ratio: float, optional

ENet mixture parameter that defines the provided ratio of L1 versus L2 regularization.

fit_intercept: bool, optional

boolean, specifies whether the model includes an intercept. If set to False, no intercept is used in training the model. Note that setting fit_intercept to False does not work well with the BFGS optimizer.

Attributes

Many attributes are created during the fitting phase.

coef_: numpy.array

The regression coefficients. The order of coefficients is the same as the order of columns used during the fitting phase.

intercept_: float

The expected value of the dependent variable when all independent variables are zero, serving as the baseline or constant term in the model.

features_importance_: numpy.array

The importance of features is computed through the model coefficients, which are normalized based on their range. Subsequently, an activation function calculates the final score. It is necessary to use the features_importance() method to compute it initially, and the computed values will be subsequently utilized for subsequent calls.

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.

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 LogisticRegression model:

from verticapy.machine_learning.vertica import LogisticRegression

Then we can create the model:

model = LogisticRegression(
    tol = 1e-6,
    max_iter = 100,
    solver = 'newton',
    fit_intercept = True,
)

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,
)



=======
details
=======
   predictor    |coefficient|std_err | z_value |p_value 
----------------+-----------+--------+---------+--------
   Intercept    | 424.62692 |24.61628|17.24984 | 0.00000
 fixed_acidity  |  0.40318  | 0.04264| 9.45456 | 0.00000
volatile_acidity| -1.28567  | 0.34387|-3.73879 | 0.00018
  citric_acid   |  0.15307  | 0.33713| 0.45402 | 0.64981
 residual_sugar |  0.12853  | 0.01377| 9.33334 | 0.00000
   chlorides    | -1.43865  | 1.85640|-0.77497 | 0.43836
    density     |-431.74147 |25.03718|-17.24401| 0.00000


==============
regularization
==============
type| lambda 
----+--------
none| 1.00000


===========
call_string
===========
logistic_reg('"public"."_verticapy_tmp_logisticregression_v_mldb_8697615e979911efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_86d6540e979911efa8720242ac120002_"', '"good"', '"fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "density"'
USING PARAMETERS optimizer='newton', epsilon=1e-06, max_iterations=100, regularization='none', lambda=1, alpha=0.5, fit_intercept=true)

===============
Additional Info
===============
       Name       |Value
------------------+-----
 iteration_count  |  5  
rejected_row_count|  0  
accepted_row_count|5197 

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

For LinearModel, feature importance is computed using the coefficients. These coefficients are then normalized using the feature distribution. An activation function is applied to get the final score.

Metrics

We can get the entire report using:

model.report()
value
auc0.7390107248520705
prc_auc0.40423073914659574
accuracy0.81
log_loss0.191289487675866
precision0.6382978723404256
recall0.11538461538461539
f1_score0.19543973941368079
mcc0.21221826742247502
informedness0.09903846153846141
markedness0.4547384150379514
csi0.10830324909747292
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.7390107248520705
prc_auc0.40423073914659574
accuracy0.6753846153846154
log_loss0.191289487675866
precision0.3392857142857143
recall0.6576923076923077
f1_score0.4476439790575916
mcc0.27708008915764676
informedness0.3374999999999999
markedness0.22747666905958353
csi0.28836424957841483
Rows: 1-11 | Columns: 2

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

model.score()
Out[2]: 0.81

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)
123
prediction
Integer
14.40.320.394.30.0331.0127.00.989043.460.3612.881white0
25.00.270.324.50.03258.0178.00.989563.450.3112.671white0
35.00.330.184.60.03240.0124.00.991143.180.411.060white0
45.10.350.266.80.03436.0120.00.991883.380.411.560white0
55.30.310.3810.50.03153.0140.00.993213.340.4611.760white0
65.30.430.111.10.0296.051.00.990763.510.4811.240white0
75.30.580.076.90.04334.0149.00.99443.340.579.750white0
85.40.180.244.80.04130.0113.00.994453.420.49.460white0
95.50.240.328.70.0619.0102.00.9943.270.3110.450white0
105.60.180.292.30.045.047.00.991263.070.4510.140white0
115.60.180.310.20.02828.0131.00.99543.490.4210.871white0
125.60.2050.1612.550.05131.0115.00.995643.40.3810.860white0
135.60.2250.249.80.05459.0140.00.995453.170.3910.260white0
145.60.230.293.10.02319.089.00.990683.250.5111.260white0
155.60.310.371.40.07412.096.00.99543.320.589.250red0
165.60.50.092.30.04917.099.00.99373.630.6313.050red0
175.60.6950.066.80.0429.084.00.994323.440.4410.250white0
185.70.2550.651.20.07917.0137.00.993073.20.429.450white0
195.70.280.33.90.02636.0105.00.989633.260.5812.7560white0
205.70.310.297.30.0533.0143.00.993323.310.511.066666666666760white0
215.70.320.52.60.04917.0155.00.99273.220.6410.060white0
225.70.450.421.10.05161.0197.00.99323.020.49.050white0
235.80.20.271.40.03112.077.00.99053.250.3610.971white0
245.80.250.2613.10.05144.0148.00.99723.290.389.350white0
255.80.270.2712.30.04555.0170.00.99723.280.429.360white0
265.80.330.216.050.04726.0166.00.99763.090.468.950white0
275.90.130.281.90.0520.078.00.99183.430.6410.860white0
285.90.170.280.70.0275.028.00.989853.130.3210.650white0
295.90.20.231.50.03738.093.00.990213.360.4912.060white0
305.90.220.31.30.05242.086.00.990693.310.4711.5560white0
315.90.230.288.60.04637.0142.00.994323.230.5310.660white0
325.90.240.32.00.03328.092.00.992253.390.6910.971white0
335.90.250.1912.40.04750.0162.00.99733.350.389.550white0
345.90.250.271.50.02937.081.00.98923.20.4612.260white0
355.90.260.2718.20.04852.0168.00.99933.350.449.450white0
365.90.320.284.70.03934.094.00.989643.220.5713.171white0
375.90.360.045.70.04621.087.00.99343.220.5110.250white0
385.90.4150.131.40.0411.064.00.99223.290.5210.550white0
396.00.160.221.60.04236.0106.00.99053.240.3211.460white0
406.00.180.311.40.03614.075.00.990853.340.5811.181white0
416.00.190.2612.40.04850.0147.00.99723.30.368.960white0
426.00.190.291.20.04629.092.00.990333.220.5311.360white0
436.00.20.323.00.03126.0118.00.991343.380.6811.271white0
446.00.280.2417.80.04742.0111.00.998963.10.458.960white0
456.00.330.265.10.05116.0119.00.994163.150.419.250white0
466.00.360.321.10.05326.0173.00.994143.380.548.850white0
476.00.520.337.70.04624.0119.00.992243.040.3811.060white0
486.10.170.4215.10.03328.0124.00.996842.870.479.550white0
496.10.170.4215.10.03328.0124.00.996842.870.479.550white0
506.10.250.2810.00.05556.0131.00.9943.220.3510.960white0
516.10.320.371.80.05113.0200.00.99453.490.4410.540white0
526.10.340.215.00.04217.0133.00.993733.020.539.450white0
536.10.340.292.20.03625.0100.00.989383.060.4411.860white0
546.10.440.284.250.03243.0132.00.99163.260.4711.266666666666771white0
556.10.460.326.20.05310.094.00.995373.350.4710.150white0
566.20.20.2911.80.03521.093.00.993643.180.3411.960white0
576.20.210.285.70.02845.0121.00.991683.211.0812.1571white0
586.20.220.282.20.0424.0125.00.99173.190.4810.560white0
596.20.250.4415.80.05739.0167.00.998043.140.519.250white0
606.20.250.4415.80.05739.0167.00.998043.140.519.250white0
616.20.270.326.30.04847.0159.00.992823.210.611.060white0
626.20.270.491.40.0520.074.00.99313.320.449.860white0
636.20.280.415.00.04350.0188.00.993183.230.6410.860white0
646.20.30.311.20.04819.0125.00.989993.320.5412.660white0
656.20.30.321.70.03230.0130.00.99113.280.4111.271white0
666.20.310.264.80.03736.0148.00.99283.210.4110.460white0
676.20.340.2512.10.05933.0171.00.997693.140.568.760white0
686.20.510.141.90.05615.034.00.993963.480.5711.560red0
696.30.170.422.80.02845.0107.00.99083.270.4311.860white0
706.30.230.31.80.03316.091.00.99063.280.411.860white0
716.30.230.336.90.05223.0118.00.99383.230.4610.460white0
726.30.230.497.10.0567.0210.00.99513.230.349.550white0
736.30.2550.371.10.0437.0114.00.99053.00.3910.960white0
746.30.270.255.80.03852.0155.00.9953.280.389.460white0
756.30.280.2211.50.03627.0150.00.994453.00.3310.660white0
766.30.390.246.90.0699.0117.00.99423.150.3510.240white0
776.30.480.041.10.04630.099.00.99283.240.369.660white0
786.40.120.31.10.03137.094.00.989863.010.5611.760white0
796.40.140.311.20.03453.0138.00.990843.380.3511.571white0
806.40.150.291.80.04421.0115.00.991663.10.3810.250white0
816.40.150.311.10.04425.096.00.99323.540.5110.360white0
826.40.170.271.50.03720.098.00.99163.460.4211.071white0
836.40.20.282.50.03224.084.00.991683.310.5511.550white0
846.40.20.323.10.04118.0126.00.99143.430.4212.060white0
856.40.220.327.90.02934.0124.00.99483.40.3910.250white0
866.40.250.320.90.03440.0114.00.991143.310.5810.871white0
876.40.280.178.30.04261.0195.00.995773.220.469.450white0
886.40.290.2811.10.06366.0169.00.99732.890.579.050white0
896.40.310.091.40.06615.028.00.994593.420.710.071red0
906.40.310.538.80.05736.0221.00.996423.170.449.150white0
916.40.380.268.20.04328.098.00.992342.990.3111.460white0
926.40.40.254.20.03215.091.00.989883.260.5213.160white0
936.50.190.11.30.04623.0107.00.99373.290.4510.050white0
946.50.210.355.70.04347.0197.00.993923.240.510.160white0
956.50.220.191.10.06436.0191.00.992973.050.59.560white0
966.50.220.297.40.02816.087.00.993113.150.5610.971white0
976.50.230.3616.30.03843.0133.00.999243.260.418.850white0
986.50.230.3616.30.03843.0133.00.999243.260.418.850white0
996.50.240.362.20.02736.0134.00.98983.280.3612.571white0
1006.50.270.196.60.04598.0175.00.993643.160.3410.160white0
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)
123
prediction
Integer
123
prediction_0
Float(22)
123
prediction_1
Float(22)
14.40.320.394.30.0331.0127.00.989043.460.3612.881white00.6108430295621430.389156970437857
25.00.270.324.50.03258.0178.00.989563.450.3112.671white00.5883279215191320.411672078480868
35.00.330.184.60.03240.0124.00.991143.180.411.060white00.7549010706528940.245098929347106
45.10.350.266.80.03436.0120.00.991883.380.411.560white00.7572515135933130.242748486406687
55.30.310.3810.50.03153.0140.00.993213.340.4611.760white00.7467908533718060.253209146628194
65.30.430.111.10.0296.051.00.990763.510.4811.240white00.8060801555874170.193919844412583
75.30.580.076.90.04334.0149.00.99443.340.579.750white00.9220047664024330.0779952335975673
85.40.180.244.80.04130.0113.00.994453.420.49.460white00.8982495606112690.101750439388731
95.50.240.328.70.0619.0102.00.9943.270.3110.450white00.822631047707910.17736895229209
105.60.180.292.30.045.047.00.991263.070.4510.140white00.7373563223420550.262643677657945
115.60.180.310.20.02828.0131.00.99543.490.4210.871white00.8563724279575480.143627572042452
125.60.2050.1612.550.05131.0115.00.995643.40.3810.860white00.8420695780376890.157930421962311
135.60.2250.249.80.05459.0140.00.995453.170.3910.260white00.8768492178216670.123150782178333
145.60.230.293.10.02319.089.00.990683.250.5111.260white00.6723566548279020.327643345172098
155.60.310.371.40.07412.096.00.99543.320.589.250red00.9584797614873330.041520238512667
165.60.50.092.30.04917.099.00.99373.630.6313.050red00.9269460062234620.0730539937765379
175.60.6950.066.80.0429.084.00.994323.440.4410.250white00.9223853312465380.0776146687534618
185.70.2550.651.20.07917.0137.00.993073.20.429.450white00.8820743252331060.117925674766894
195.70.280.33.90.02636.0105.00.989633.260.5812.7560white00.5472237065851350.452776293414865
205.70.310.297.30.0533.0143.00.993323.310.511.066666666666760white00.8053702977963370.194629702203663
215.70.320.52.60.04917.0155.00.99273.220.6410.060white00.8501656208770320.149834379122968
225.70.450.421.10.05161.0197.00.99323.020.49.050white00.9110747896920080.088925210307992
235.80.20.271.40.03112.077.00.99053.250.3610.971white00.6802714777083830.319728522291617
245.80.250.2613.10.05144.0148.00.99723.290.389.350white00.9036491205689490.0963508794310512
255.80.270.2712.30.04555.0170.00.99723.280.429.360white00.9134749034803570.086525096519643
265.80.330.216.050.04726.0166.00.99763.090.468.950white00.8945659614550570.105434038544943
275.90.130.281.90.0520.078.00.99183.430.6410.860white00.7590551606334150.240944839366585
285.90.170.280.70.0275.028.00.989853.130.3210.650white00.6173197388805080.382680261119492
295.90.20.231.50.03738.093.00.990213.360.4912.060white00.6436858478192240.356314152180776
305.90.220.31.30.05242.086.00.990693.310.4711.5560white00.7028526076919120.297147392308088
315.90.230.288.60.04637.0142.00.994323.230.5310.660white00.81714745676170.1828525432383
325.90.240.32.00.03328.092.00.992253.390.6910.971white00.8088943503644210.191105649635579
335.90.250.1912.40.04750.0162.00.99733.350.389.550white00.9118337585591360.0881662414408642
345.90.250.271.50.02937.081.00.98923.20.4612.260white00.5503266763849950.449673323615005
355.90.260.2718.20.04852.0168.00.99933.350.449.450white00.9210193592776520.0789806407223483
365.90.320.284.70.03934.094.00.989643.220.5713.171white00.5208642711335830.479135728866417
375.90.360.045.70.04621.087.00.99343.220.5110.250white00.8424476667286180.157552333271382
385.90.4150.131.40.0411.064.00.99223.290.5210.550white00.8531417239294720.146858276070529
396.00.160.221.60.04236.0106.00.99053.240.3211.460white00.6503843490560330.349615650943967
406.00.180.311.40.03614.075.00.990853.340.5811.181white00.6901564221614410.309843577838559
416.00.190.2612.40.04850.0147.00.99723.30.368.960white00.8971845146321060.102815485367894
426.00.190.291.20.04629.092.00.990333.220.5311.360white00.653023503880380.34697649611962
436.00.20.323.00.03126.0118.00.991343.380.6811.271white00.6950299806601350.304970019339865
446.00.280.2417.80.04742.0111.00.998963.10.458.960white00.9128903352626230.0871096647373769
456.00.330.265.10.05116.0119.00.994163.150.419.250white00.8782998563500130.121700143649987
466.00.360.321.10.05326.0173.00.994143.380.548.850white00.9251303698638280.0748696301361725
476.00.520.337.70.04624.0119.00.992243.040.3811.060white00.7387860982371910.261213901762809
486.10.170.4215.10.03328.0124.00.996842.870.479.550white00.8251677255538410.174832274446159
496.10.170.4215.10.03328.0124.00.996842.870.479.550white00.8251677255538410.174832274446159
506.10.250.2810.00.05556.0131.00.9943.220.3510.960white00.757139840234270.24286015976573
516.10.320.371.80.05113.0200.00.99453.490.4410.540white00.9225397377543220.077460262245678
526.10.340.215.00.04217.0133.00.993733.020.539.450white00.8545666354513690.145433364548631
536.10.340.292.20.03625.0100.00.989383.060.4411.860white00.5576955864917680.442304413508232
546.10.440.284.250.03243.0132.00.99163.260.4711.266666666666771white00.7409922495080820.259007750491918
556.10.460.326.20.05310.094.00.995373.350.4710.150white00.9225823297292590.077417670270741
566.20.20.2911.80.03521.093.00.993643.180.3411.960white00.649165229605660.35083477039434
576.20.210.285.70.02845.0121.00.991683.211.0812.1571white00.6358793241775380.364120675822462
586.20.220.282.20.0424.0125.00.99173.190.4810.560white00.7400334443158670.259966555684133
596.20.250.4415.80.05739.0167.00.998043.140.519.250white00.8883382492293390.111661750770661
606.20.250.4415.80.05739.0167.00.998043.140.519.250white00.8883382492293390.111661750770661
616.20.270.326.30.04847.0159.00.992823.210.611.060white00.7450491981463120.254950801853688
626.20.270.491.40.0520.074.00.99313.320.449.860white00.8581407311847440.141859268815256
636.20.280.415.00.04350.0188.00.993183.230.6410.860white00.8000787102248770.199921289775123
646.20.30.311.20.04819.0125.00.989993.320.5412.660white00.633245980097920.36675401990208
656.20.30.321.70.03230.0130.00.99113.280.4111.271white00.7184093936802780.281590606319722
666.20.310.264.80.03736.0148.00.99283.210.4110.460white00.7860585647335890.213941435266411
676.20.340.2512.10.05933.0171.00.997693.140.568.760white00.927304916581480.07269508341852
686.20.510.141.90.05615.034.00.993963.480.5711.560red00.9225657045461070.0774342954538932
696.30.170.422.80.02845.0107.00.99083.270.4311.860white00.6075824496070780.392417550392922
706.30.230.31.80.03316.091.00.99063.280.411.860white00.6415382133940060.358461786605994
716.30.230.336.90.05223.0118.00.99383.230.4610.460white00.7909805671353580.209019432864642
726.30.230.497.10.0567.0210.00.99513.230.349.550white00.8628346429374480.137165357062552
736.30.2550.371.10.0437.0114.00.99053.00.3910.960white00.6593374206059050.340662579394095
746.30.270.255.80.03852.0155.00.9953.280.389.460white00.8843058031072160.115694196892785
756.30.280.2211.50.03627.0150.00.994453.00.3310.660white00.7461779929888720.253822007011128
766.30.390.246.90.0699.0117.00.99423.150.3510.240white00.8516349768745810.148365023125419
776.30.480.041.10.04630.099.00.99283.240.369.660white00.8809824550130830.119017544986917
786.40.120.31.10.03137.094.00.989863.010.5611.760white00.5418796827769250.458120317223075
796.40.140.311.20.03453.0138.00.990843.380.3511.571white00.6471794790598870.352820520940113
806.40.150.291.80.04421.0115.00.991663.10.3810.250white00.7137928924352050.286207107564794
816.40.150.311.10.04425.096.00.99323.540.5110.360white00.8409977243398710.159002275660129
826.40.170.271.50.03720.098.00.99163.460.4211.071white00.7201561931621950.279843806837805
836.40.20.282.50.03224.084.00.991683.310.5511.550white00.7070479371296790.292952062870321
846.40.20.323.10.04118.0126.00.99143.430.4212.060white00.6659463770663090.334053622933691
856.40.220.327.90.02934.0124.00.99483.40.3910.250white00.824815833018560.17518416698144
866.40.250.320.90.03440.0114.00.991143.310.5810.871white00.7139485256786910.286051474321309
876.40.280.178.30.04261.0195.00.995773.220.469.450white00.8844743350715180.115525664928482
886.40.290.2811.10.06366.0169.00.99732.890.579.050white00.9139152415638290.0860847584361713
896.40.310.091.40.06615.028.00.994593.420.710.071red00.9240199371889850.0759800628110152
906.40.310.538.80.05736.0221.00.996423.170.449.150white00.9052451563732130.0947548436267866
916.40.380.268.20.04328.098.00.992342.990.3111.460white00.6645662968962650.335433703103735
926.40.40.254.20.03215.091.00.989883.260.5213.160white00.5367274876163940.463272512383606
936.50.190.11.30.04623.0107.00.99373.290.4510.050white00.8701136979065810.129886302093419
946.50.210.355.70.04347.0197.00.993923.240.510.160white00.8044803760088280.195519623991172
956.50.220.191.10.06436.0191.00.992973.050.59.560white00.8406656888267380.159334311173262
966.50.220.297.40.02816.087.00.993113.150.5610.971white00.6998914226641260.300108577335874
976.50.230.3616.30.03843.0133.00.999243.260.418.850white00.9141933602951780.085806639704822
986.50.230.3616.30.03843.0133.00.999243.260.418.850white00.9141933602951780.085806639704822
996.50.240.362.20.02736.0134.00.98983.280.3612.571white00.5248908074566690.475109192543331
1006.50.270.196.60.04598.0175.00.993643.160.3410.160white00.7828710748643520.217128925135648
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[3]: 
array([[1023,   17],
       [ 230,   30]])

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

If the model allows, you can also generate relevant plots. For example, classification plots can be found in the Machine Learning - Classification Plots.

model.plot()

Important

The plotting feature is typically suitable for models with fewer than three predictors.

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

model.contour()

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[4]: 
{'penalty': 'none',
 'tol': 1e-06,
 'max_iter': 100,
 'solver': 'newton',
 'fit_intercept': True}

And to manually change some of the parameters:

model.set_params({'tol': 0.001})

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 following methods for exporting the model use MemModel, and it is recommended to use MemModel directly.

To SQL

You can get the SQL code by:

model.to_sql()
Out[6]: '((1 / (1 + EXP(- (424.62692415138 + 0.403179029947125 * "fixed_acidity" + -1.28566720131892 * "volatile_acidity" + 0.153065045355106 * "citric_acid" + 0.128532468331258 * "residual_sugar" + -1.4386487597294 * "chlorides" + -431.741467616055 * "density")))) > 0.5)::int'

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[8]: 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, penalty: Literal['none', 'l1', 'l2', 'enet', None] = 'none', tol: float = 1e-06, C: Annotated[int | float | Decimal, 'Python Numbers'] = 1.0, max_iter: int = 100, solver: Literal['newton', 'bfgs', 'cgd'] = 'newton', l1_ratio: float = 0.5, fit_intercept: bool = True) → None

Methods

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

classification_report([metrics, cutoff, nbins])

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

confusion_matrix([cutoff])

Computes the model confusion matrix.

contour([nbins, chart])

Draws the model's contour plot.

cutoff_curve([nbins, show, chart])

Draws the model Cutoff curve.

deploySQL([X, cutoff])

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([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_vertica_attributes([attr_name])

Returns the model Vertica attributes.

import_models(path[, schema, kind])

Imports machine learning models.

lift_chart([nbins, show, chart])

Draws the model Lift Chart.

plot([max_nb_points, chart])

Draws the model.

prc_curve([nbins, show, chart])

Draws the model PRC curve.

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

Makes predictions on 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, nbins])

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

roc_curve([nbins, show, chart])

Draws the model ROC curve.

score([metric, cutoff, nbins])

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_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