Loading...

verticapy.machine_learning.vertica.svm.LinearSVC

class verticapy.machine_learning.vertica.svm.LinearSVC(name: str = None, overwrite_model: bool = False, tol: float = 0.0001, C: float = 1.0, intercept_scaling: float = 1.0, intercept_mode: Literal['regularized', 'unregularized'] = 'regularized', class_weight: Literal['auto', 'none'] | list = [1, 1], max_iter: int = 100)

Creates a LinearSVC object using the Vertica Support Vector Machine (SVM) algorithm on the data. Given a set of training examples, where each is marked as belonging to one of two categories, an SVM training algorithm builds a model that assigns new examples to one category or the other, making it a non-probabilistic binary linear classifier.

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.

tol: float, optional

Tolerance for stopping criteria. This is used to control accuracy.

C: float, optional

Weight for misclassification cost. The algorithm minimizes the regularization cost and the misclassification cost.

intercept_scaling: float

A float value, serves as the value of a dummy feature whose coefficient Vertica uses to calculate the model intercept. Because the dummy feature is not in the training data, its values are set to a constant, by default set to 1.

intercept_mode: str, optional

Specify how to treat the intercept.

  • regularized:

    Fits the intercept and applies a regularization.

  • unregularized:

    Fits the intercept but does not include it in regularization.

class_weight: str | list, optional

Specifies how to determine weights for the two classes. It can be a list of 2 elements or one of the following methods:

  • auto:

    Weights each class according to the number of samples.

  • none:

    No weights are used.

max_iter: int, optional

The maximum number of iterations that the algorithm performs.

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.

Model Initialization

First we import the LinearSVC model:

from verticapy.machine_learning.vertica import LinearSVC

Then we can create the model:

model = LinearSVC(
    tol = 1e-4,
    C = 1.0,
    intercept_scaling = 1.0,
    intercept_mode = "regularized",
    class_weight = [1, 1],
    max_iter = 100,
)

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
----------------+-----------
   Intercept    |  1.51553  
 fixed_acidity  |  0.00351  
volatile_acidity| -0.51234  
  citric_acid   |  0.14191  
 residual_sugar | -0.01813  
   chlorides    | -4.59023  
    density     | -1.68097  


===========
call_string
===========
SELECT svm_classifier('"public"."_verticapy_tmp_linearsvc_v_mldb_e198dc34979b11efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_e1d74fd2979b11efa8720242ac120002_"', '"good"', '"fixed_acidity", "volatile_acidity", "citric_acid", "residual_sugar", "chlorides", "density"'
USING PARAMETERS class_weights='1,1', C=1, max_iterations=100, intercept_mode='regularized', intercept_scaling=1, epsilon=0.0001);

===============
Additional Info
===============
       Name       |Value
------------------+-----
accepted_row_count|5196 
rejected_row_count|  0  
 iteration_count  | 13  

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.6752826149969873
prc_auc0.2963134390332993
accuracy0.8016910069177555
log_loss0.235063722071299
precision0.0
recall0.0
f1_score0.0
mcc0.0
informedness0.0
markedness-0.19830899308224448
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.6752826149969873
prc_auc0.2963134390332993
accuracy0.20599538816295157
log_loss0.235063722071299
precision0.19984508133230056
recall1.0
f1_score0.3331181407359587
mcc0.04377282517388779
informedness0.009587727708533
markedness0.1998450813323005
csi0.19984508133230056
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[3]: 0.8016910069177555

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
15.00.610.121.30.00965.0100.00.98743.260.3713.550white0
25.10.260.346.40.03426.099.00.994493.230.419.260white0
35.20.240.453.80.02721.0128.00.9923.550.4911.281white0
45.20.340.01.80.0527.063.00.99163.680.7914.060red0
55.20.340.01.80.0527.063.00.99163.680.7914.060red0
65.20.4050.151.450.03810.044.00.991253.520.411.640white0
75.30.430.111.10.0296.051.00.990763.510.4811.240white0
85.40.530.162.70.03634.0128.00.988563.20.5313.281white0
95.40.530.162.70.03634.0128.00.988563.20.5313.281white0
105.50.160.261.50.03235.0100.00.990763.430.7712.060white0
115.60.180.271.70.0331.0103.00.988923.350.3712.960white0
125.60.180.292.30.045.047.00.991263.070.4510.140white0
135.60.180.311.50.03816.084.00.99243.340.5810.160white0
145.60.190.461.10.03233.0115.00.99093.360.510.460white0
155.60.460.244.80.04224.072.00.99083.290.3712.660white0
165.60.50.092.30.04917.099.00.99373.630.6313.050red0
175.70.220.2216.650.04439.0110.00.998553.240.489.060white0
185.70.2550.651.20.07917.0137.00.993073.20.429.450white0
195.70.310.297.30.0533.0143.00.993323.310.511.066666666666760white0
205.80.220.251.50.02421.0109.00.992343.370.5810.460white0
215.80.260.249.20.04455.0152.00.99613.310.389.450white0
225.80.280.669.10.03926.0159.00.99653.660.5510.850white0
235.80.330.216.050.04726.0166.00.99763.090.468.950white0
245.80.380.261.10.05820.0140.00.992713.270.439.760white0
255.90.240.281.30.03236.095.00.988893.080.6412.971white0
265.90.270.279.00.05143.0136.00.99413.250.5310.760white0
275.90.320.1914.50.04237.0115.00.996843.160.4310.350white0
285.90.320.284.70.03934.094.00.989643.220.5713.171white0
295.90.340.312.00.0338.0142.00.988923.40.4112.971white0
305.90.350.472.20.1114.0138.00.99323.090.59.150white0
315.90.4350.166.40.03121.0134.00.991513.240.4612.260white0
326.00.180.311.40.03614.075.00.990853.340.5811.181white0
336.00.20.711.60.1510.054.00.99273.120.479.850white0
346.00.240.271.90.04840.0170.00.99383.640.5410.071white0
356.00.240.341.00.03652.0184.00.990973.440.4411.4560white0
366.00.240.411.30.03642.0118.00.990183.040.6411.7560white0
376.00.260.151.20.05335.0124.00.993473.080.468.850white0
386.00.280.2417.80.04742.0111.00.998963.10.458.960white0
396.00.320.31.90.03341.0142.00.989123.290.4212.871white0
406.00.330.265.10.05116.0119.00.994163.150.419.250white0
416.00.340.323.80.04413.0116.00.991083.390.4411.871white0
426.00.340.6615.90.04626.0164.00.99793.140.58.860white0
436.00.380.266.00.03442.0134.00.99123.380.3812.371white0
446.00.580.22.40.07515.050.00.994673.580.6712.560red0
456.00.6150.040.80.0328.050.00.990363.140.411.040white0
466.10.280.2419.950.07432.0174.00.999223.190.449.360white0
476.10.280.2512.90.05434.0189.00.99793.250.439.040white0
486.10.320.286.60.02129.0132.00.991883.150.3611.4571white0
496.10.340.215.00.04217.0133.00.993733.020.539.450white0
506.10.380.143.90.0627.0113.00.993443.070.349.240white0
516.10.410.01.60.06336.087.00.99143.270.6710.860white0
526.10.440.284.250.03243.0132.00.99163.260.4711.371white0
536.10.680.521.40.03732.0123.00.990223.240.4512.060white0
546.20.190.294.30.04533.0126.00.996583.180.429.360white0
556.20.20.281.10.03924.078.00.98993.360.4712.160white0
566.20.220.220.80.03558.0184.01.000223.110.539.060white0
576.20.230.350.70.05124.0111.00.99163.370.4311.030white0
586.20.230.381.60.04412.0113.00.991763.30.7311.450white0
596.20.250.4415.80.05739.0167.00.998043.140.519.250white0
606.20.270.326.30.04847.0159.00.992823.210.611.060white0
616.20.30.321.70.03230.0130.00.99113.280.4111.271white0
626.20.310.264.80.03736.0148.00.99283.210.4110.460white0
636.20.330.144.80.05227.0128.00.994753.210.489.450white0
646.20.340.2512.10.05933.0171.00.997693.140.568.760white0
656.20.360.383.20.03120.089.00.989563.060.3312.071white0
666.20.440.392.50.0776.014.00.995553.510.6911.060red0
676.20.460.171.60.0737.011.00.994253.610.5411.450red0
686.20.560.091.70.05324.032.00.994023.540.611.350red0
696.20.630.311.70.08815.064.00.99693.460.799.350red0
706.30.150.3411.40.0525.096.00.997543.210.4910.060white0
716.30.170.422.80.02845.0107.00.99083.270.4311.860white0
726.30.180.221.50.04345.0155.00.992383.190.4810.250white0
736.30.210.281.50.05146.0142.00.99283.230.4210.160white0
746.30.230.2217.450.05442.0151.00.998533.120.69.360white0
756.30.230.31.80.03316.091.00.99063.280.411.860white0
766.30.230.311.50.02211.082.00.98923.30.412.971white0
776.30.230.336.90.05223.0118.00.99383.230.4610.460white0
786.30.260.292.20.04335.0175.00.99183.380.4311.660white0
796.30.270.187.70.04845.0186.00.99623.230.479.050white0
806.30.270.232.90.04713.0100.00.99363.280.439.850white0
816.30.270.255.80.03852.0155.00.9953.280.389.460white0
826.30.290.284.70.05928.081.00.990363.240.5612.781white0
836.30.290.46.50.03943.0167.00.99533.150.449.360white0
846.30.30.23.70.03934.0132.00.991583.00.3810.750white0
856.30.30.285.00.04236.0168.00.995053.220.699.560white0
866.30.360.193.20.07515.039.00.99563.560.5212.760red0
876.30.390.246.90.0699.0117.00.99423.150.3510.240white0
886.30.410.183.50.02723.0109.00.990183.340.5412.881white0
896.30.680.013.70.10332.054.00.995863.510.6611.360red0
906.40.130.471.60.09240.0158.00.99283.210.369.860white0
916.40.150.257.80.0513.068.00.993943.160.49.960white0
926.40.150.291.80.04421.0115.00.991663.10.3810.250white0
936.40.190.3510.20.04340.0106.00.996323.160.59.760white0
946.40.250.320.90.03440.0114.00.991143.310.5810.871white0
956.40.280.178.30.04261.0195.00.995773.220.469.450white0
966.40.290.571.00.0615.0120.00.99243.060.419.550white0
976.40.550.269.60.02720.0104.00.99243.220.7313.160white0
986.50.080.331.90.02823.093.00.9913.340.712.071white0
996.50.190.11.30.04623.0107.00.99373.290.4510.050white0
1006.50.190.274.90.03713.0101.00.99163.170.4111.860white0
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.70.3350.141.30.03669.0168.00.992123.470.4610.550white00.6168730900974160.383126909902585
25.10.110.321.60.02812.090.00.990083.570.5212.260white00.5742866151404550.425713384859545
35.10.390.211.70.02715.072.00.98943.50.4512.560white00.611710758389020.38828924161098
45.20.360.021.60.03124.0104.00.98963.440.3512.260white00.6183698701234710.381630129876529
55.20.50.182.00.03623.0129.00.989493.360.7713.471white00.6368405471511480.363159452848852
65.30.160.391.00.02840.0101.00.991563.570.5910.660white00.5758966904997560.424103309500244
75.30.470.112.20.04816.089.00.991823.540.8813.671red00.6498810733202140.350118926679786
85.40.220.291.20.04569.0152.00.991783.760.6311.071white00.606485521338030.39351447866197
95.40.740.091.70.08916.026.00.994023.670.5611.660red00.7195371616508350.280462838349165
105.50.120.331.00.03823.0131.00.991643.250.459.850white00.5840223113049030.415977688695097
115.60.260.181.40.03418.0135.00.991743.320.3510.260white00.6037303823801410.396269617619859
125.60.280.284.20.04452.0158.00.9923.350.4410.771white00.6257918202600440.374208179739956
135.70.180.361.20.0469.071.00.991993.70.6810.971white00.6001347513968210.399865248603179
145.70.20.32.50.04638.0125.00.992763.340.59.960white00.6105578756399670.389442124360033
155.80.180.371.10.03631.096.00.989423.160.4812.060white00.5871560491267960.412843950873204
165.80.190.241.30.04438.0128.00.993623.770.610.650white00.6042491499476450.395750850052355
175.80.220.290.90.03434.089.00.989363.140.3611.171white00.5917390887701140.408260911229886
185.80.220.291.30.03625.068.00.988653.240.3512.660white00.5954158359248870.404584164075113
195.80.260.181.20.03140.0114.00.99083.420.411.071white00.599012869252640.40098713074736
205.80.290.333.70.02930.088.00.989943.250.4212.360white00.6059090867484090.394090913251591
215.90.140.254.50.02734.0140.00.99343.490.5110.860white00.5927734399241180.407226560075882
225.90.190.211.70.04557.0135.00.993413.320.449.550white00.607925292542030.39207470745797
235.90.210.237.90.03322.0130.00.99443.380.5910.960white00.6236396577085720.376360342291428
245.90.210.284.60.05340.0199.00.99643.720.710.040white00.6302411812704790.369758818729521
255.90.220.4522.60.1255.0122.00.996363.10.3512.850white00.7591738270980680.240826172901932
265.90.230.241.60.03732.0115.00.990763.210.5111.460white00.6015304614297810.398469538570219
275.90.290.337.40.03758.0205.00.994953.260.419.650white00.6322868550108520.367713144989148
285.90.330.328.10.0389.034.00.99113.220.3612.771white00.6398621316470280.360137868352972
295.90.370.321.60.02941.0102.00.989163.410.5512.771white00.6065444539730060.393455546026994
306.00.160.36.70.04343.0153.00.99513.630.4610.650white00.6211520936014990.378847906398501
316.00.20.266.80.04922.093.00.99283.150.4211.060white00.6332302893061220.366769710693878
326.00.20.266.80.04922.093.00.99283.150.4211.060white00.6332302893061220.366769710693878
336.00.250.45.70.05256.0152.00.993983.160.8810.560white00.6335914651893310.366408534810669
346.00.260.341.30.0466.029.00.99243.290.6310.450white00.6109489813883230.389051018611677
356.00.40.31.60.04730.0117.00.99313.170.4810.160white00.6317926254472010.368207374552799
366.00.5550.264.50.05317.0126.00.99433.240.469.150white00.6697720486668990.330227951333101
376.00.670.071.20.069.0108.00.99313.110.358.740white00.6820711720766120.317928827923388
386.10.150.41.20.0319.084.00.989263.190.9613.060white00.5757973262051410.424202673794859
396.10.220.491.50.05118.087.00.99283.30.469.650white00.6074062008807420.392593799119258
406.10.220.491.50.05118.087.00.99283.30.469.650white00.6074062008807420.392593799119258
416.10.250.31.20.03642.0107.00.9913.340.5610.871white00.5990326712534980.400967328746502
426.10.280.278.00.04841.0162.00.994983.210.519.950white00.6470629314728410.352937068527159
436.10.30.32.10.03150.0163.00.98953.390.4312.771white00.6029811427310460.397018857268954
446.10.30.322.20.04241.0142.00.989523.310.4412.771white00.6147690233258260.385230976674174
456.10.450.270.80.03913.082.00.99273.230.329.550white00.6265715797014810.373428420298519
466.20.160.321.10.03674.0184.00.990963.220.4111.060white00.5866795627648480.413320437235152
476.20.160.471.40.02923.081.00.993.260.4212.260white00.574604823431750.42539517656825
486.20.240.227.90.05345.0149.00.995453.230.529.350white00.6489285053717540.351071494628246
496.20.270.2612.10.04643.0127.00.99513.160.3710.860white00.6609370709466680.339062929053332
506.20.280.2710.30.0326.0108.00.993883.20.3610.760white00.637155661528770.36284433847123
516.20.340.287.50.03440.0197.00.994853.140.69.750white00.636816993530570.36318300646943
526.20.440.187.70.09628.0210.00.997713.560.729.250white00.715048163513290.28495183648671
536.20.450.21.60.0693.015.00.99583.410.569.250red00.6647524617156960.335247538284304
546.20.650.061.60.056.018.00.993483.570.5411.9550red00.6716743680786530.328325631921347
556.20.660.481.20.02929.075.00.98923.330.3912.881white00.6342388256650090.365761174334991
566.30.170.324.20.0437.0117.00.991823.240.4311.360white00.6061250707420210.393874929257979
576.30.210.2911.70.04849.0147.00.994823.220.3810.850white00.6533007952310990.346699204768901
586.30.220.434.550.03831.0130.00.99183.350.3311.571white00.6078283621181170.392171637881883
596.30.290.293.30.03732.0140.00.98953.170.3612.871white00.6136781191957040.386321880804296
606.30.30.918.20.03450.0199.00.993943.390.4911.760white00.6136050275361230.386394972463877
616.30.380.178.80.0850.0212.00.998033.470.669.440white00.6979173681615880.302082631838412
626.30.390.222.80.04853.0173.00.993043.240.459.850white00.6390731353458840.360926864654116
636.30.980.012.00.05715.033.00.994883.60.4611.260red00.7177493658118850.282250634188115
646.40.180.7411.90.04654.0168.00.99783.580.6810.150white00.6349367161024410.365063283897559
656.40.220.341.40.02356.0115.00.989583.180.711.760white00.5795446406639180.420455359336082
666.40.220.341.40.02356.0115.00.989583.180.711.760white00.5795446406639180.420455359336082
676.40.250.35.50.03815.0129.00.99483.140.499.660white00.6210322963762310.378967703623769
686.40.290.322.40.01434.089.00.990083.240.6612.571white00.5835272078341890.416472792165811
696.40.30.275.00.05827.0151.00.991983.220.4912.260white00.6460688035459510.353931196454049
706.40.310.46.40.03939.0191.00.995133.140.529.850white00.6299373048434220.370062695156578
716.40.330.37.20.04142.0168.00.993313.220.4911.160white00.6403802424716420.359619757528358
726.40.340.11.10.04819.084.00.99273.210.389.850white00.6297173244633510.370282675536649
736.40.350.212.10.05146.0171.00.99323.160.59.550white00.6348924130339490.365107586966051
746.40.5950.145.20.05815.097.00.99513.380.369.040white00.6857372208477590.314262779152241
756.50.130.272.60.03532.076.00.99143.210.7611.333333333333360white00.5900762257838980.409923774216102
766.50.160.341.40.02929.0133.00.991083.330.6411.571white00.5792940061905450.420705993809455
776.50.180.4114.20.03947.0129.00.996783.280.7210.371white00.6474385058690460.352561494130954
786.50.190.265.20.0431.0140.00.9953.260.689.560white00.615997409434310.38400259056569
796.50.230.2517.30.04615.0110.00.998283.150.429.260white00.6785231129797920.321476887020208
806.50.230.3313.80.04225.0139.00.996953.350.5610.460white00.6573644860098860.342635513990114
816.50.250.2717.40.06429.0140.00.997763.20.4910.160white00.6980364559808680.301963544019132
826.50.280.292.70.03826.0107.00.99123.320.4111.671white00.6114809987544330.388519001245567
836.50.290.39.150.05125.0166.00.993393.240.5611.3560white00.6542039868016550.345796013198345
846.50.290.311.70.03524.079.00.990533.270.6911.471white00.6041519294250540.395848070574946
856.50.30.397.80.03861.0219.00.99593.190.59.450white00.634132286022640.36586771397736
866.50.320.32.30.05120.0127.00.989643.130.5212.860white00.6277118720202310.372288127979769
876.50.410.224.80.05249.0142.00.99463.140.629.250white00.6543333305403080.345666669459692
886.50.510.251.70.04839.0177.00.992123.280.5710.650white00.6471125949726580.352887405027342
896.60.180.281.70.04153.0161.00.992073.130.4510.260white00.598801573138580.40119842686142
906.60.210.360.80.03448.0113.00.991653.240.6810.560white00.5879101083905950.412089891609405
916.60.220.2317.30.04737.0118.00.999063.080.468.860white00.6792349982735930.320765001726407
926.60.220.2317.30.04737.0118.00.999063.080.468.860white00.6792349982735930.320765001726407
936.60.230.34.60.0629.0154.00.991423.230.4912.281white00.6369072822811350.363092717718865
946.60.230.314.90.05133.0118.00.998353.040.549.060white00.6724069954175290.327593004582471
956.60.240.313.00.05218.0143.00.998253.370.499.460white00.6668964026717480.333103597328252
966.60.250.3112.40.05952.0181.00.99843.510.479.860white00.6724723091221180.327527690877882
976.60.250.368.10.04554.0180.00.99583.080.429.250white00.6377655753729730.362234424627027
986.60.310.281.40.03528.0107.00.988363.00.413.260white00.6053626890392010.394637310960799
996.60.410.161.40.03728.0160.00.991672.950.4510.660white00.6250101328716530.374989867128347
1006.60.420.1312.80.04426.0158.00.997723.240.479.050white00.6833669243849480.316633075615052
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([[1043,    0],
       [ 258,    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

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

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[5]: 
{'tol': 0.0001,
 'C': 1.0,
 'intercept_scaling': 1.0,
 'intercept_mode': 'regularized',
 'class_weight': [1, 1],
 'max_iter': 100}

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[7]: '((1 / (1 + EXP(- (1.51553164629599 + 0.0035068366370968 * "fixed_acidity" + -0.512343741994686 * "volatile_acidity" + 0.141911729594543 * "citric_acid" + -0.0181348451555414 * "residual_sugar" + -4.59022886762405 * "chlorides" + -1.68096576167843 * "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[9]: 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, tol: float = 0.0001, C: float = 1.0, intercept_scaling: float = 1.0, intercept_mode: Literal['regularized', 'unregularized'] = 'regularized', class_weight: Literal['auto', 'none'] | list = [1, 1], max_iter: int = 100) None

Methods

__init__([name, overwrite_model, tol, C, ...])

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