Loading...

verticapy.machine_learning.vertica.naive_bayes.NaiveBayes

class verticapy.machine_learning.vertica.naive_bayes.NaiveBayes(name: str = None, overwrite_model: bool = False, alpha: Annotated[int | float | Decimal, 'Python Numbers'] = 1.0, nbtype: Literal['auto', 'bernoulli', 'categorical', 'multinomial', 'gaussian'] = 'auto')

Creates a NaiveBayes object using the Vertica Naive Bayes algorithm. It is a “probabilistic classifier” based on applying Bayes’ theorem with strong (naïve) independence assumptions between the features.

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.

alpha: float, optional

A float that specifies use of Laplace smoothing if the event model is categorical, multinomial, or Bernoulli.

nbtype: str, optional

Naive Bayes type.

  • auto:

    Vertica NaiveBayes objects treat columns according to data type:

    • FLOAT:

      values are assumed to follow some Gaussian distribution.

    • INTEGER:

      values are assumed to belong to one multinomial distribution.

    • CHAR/VARCHAR:

      values are assumed to follow some categorical distribution. The string values stored in these columns must be no greater than 128 characters.

    • BOOLEAN:

      values are treated as categorical with two values.

  • bernoulli:

    Casts the variables to boolean.

  • categorical:

    Casts the variables to categorical.

  • multinomial:

    Casts the variables to integer.

  • gaussian:

    Casts the variables to float.

Attributes

Many attributes are created during the fitting phase.

prior_: numpy.array

The model’s classes probabilities.

attributes: list of dict

list of the model’s attributes. Each feature is represented by a dictionary, which differs based on the distribution.

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 iris dataset.

import verticapy.datasets as vpd

data = vpd.load_iris()
123
SepalLengthCm
Numeric(7)
123
SepalWidthCm
Numeric(7)
123
PetalLengthCm
Numeric(7)
123
PetalWidthCm
Numeric(7)
Abc
Species
Varchar(30)
13.34.55.67.8Iris-setosa
23.34.55.67.8Iris-setosa
33.34.55.67.8Iris-setosa
43.34.55.67.8Iris-setosa
53.34.55.67.8Iris-setosa
63.34.55.67.8Iris-setosa
73.34.55.67.8Iris-setosa
83.34.55.67.8Iris-setosa
93.34.55.67.8Iris-setosa
103.34.55.67.8Iris-setosa
113.34.55.67.8Iris-setosa
123.34.55.67.8Iris-setosa
133.34.55.67.8Iris-setosa
143.34.55.67.8Iris-setosa
153.34.55.67.8Iris-setosa
163.34.55.67.8Iris-setosa
173.34.55.67.8Iris-setosa
183.34.55.67.8Iris-setosa
193.34.55.67.8Iris-setosa
203.34.55.67.8Iris-setosa
213.34.55.67.8Iris-setosa
223.34.55.67.8Iris-setosa
233.34.55.67.8Iris-setosa
243.34.55.67.8Iris-setosa
253.34.55.67.8Iris-setosa
263.34.55.67.8Iris-setosa
274.33.01.10.1Iris-setosa
284.34.79.61.8Iris-virginica
294.34.79.61.8Iris-virginica
304.34.79.61.8Iris-virginica
314.34.79.61.8Iris-virginica
324.34.79.61.8Iris-virginica
334.34.79.61.8Iris-virginica
344.34.79.61.8Iris-virginica
354.34.79.61.8Iris-virginica
364.34.79.61.8Iris-virginica
374.34.79.61.8Iris-virginica
384.34.79.61.8Iris-virginica
394.34.79.61.8Iris-virginica
404.34.79.61.8Iris-virginica
414.34.79.61.8Iris-virginica
424.34.79.61.8Iris-virginica
434.34.79.61.8Iris-virginica
444.34.79.61.8Iris-virginica
454.34.79.61.8Iris-virginica
464.34.79.61.8Iris-virginica
474.34.79.61.8Iris-virginica
484.34.79.61.8Iris-virginica
494.34.79.61.8Iris-virginica
504.34.79.61.8Iris-virginica
514.34.79.61.8Iris-virginica
524.34.79.61.8Iris-virginica
534.34.79.61.8Iris-virginica
544.42.91.40.2Iris-setosa
554.43.01.30.2Iris-setosa
564.43.21.30.2Iris-setosa
574.52.31.30.3Iris-setosa
584.63.11.50.2Iris-setosa
594.63.21.40.2Iris-setosa
604.63.41.40.3Iris-setosa
614.63.61.00.2Iris-setosa
624.73.21.30.2Iris-setosa
634.73.21.60.2Iris-setosa
644.83.01.40.1Iris-setosa
654.83.01.40.3Iris-setosa
664.83.11.60.2Iris-setosa
674.83.41.60.2Iris-setosa
684.83.41.90.2Iris-setosa
694.92.43.31.0Iris-versicolor
704.92.54.51.7Iris-virginica
714.93.01.40.2Iris-setosa
724.93.11.50.1Iris-setosa
734.93.11.50.1Iris-setosa
744.93.11.50.1Iris-setosa
755.02.03.51.0Iris-versicolor
765.02.33.31.0Iris-versicolor
775.03.01.60.2Iris-setosa
785.03.21.20.2Iris-setosa
795.03.31.40.2Iris-setosa
805.03.41.50.2Iris-setosa
815.03.41.60.4Iris-setosa
825.03.51.30.3Iris-setosa
835.03.51.60.6Iris-setosa
845.03.61.40.2Iris-setosa
855.12.53.01.1Iris-versicolor
865.13.31.70.5Iris-setosa
875.13.41.50.2Iris-setosa
885.13.51.40.2Iris-setosa
895.13.51.40.3Iris-setosa
905.13.71.50.4Iris-setosa
915.13.81.50.3Iris-setosa
925.13.81.60.2Iris-setosa
935.13.81.90.4Iris-setosa
945.22.73.91.4Iris-versicolor
955.23.41.40.2Iris-setosa
965.23.51.50.2Iris-setosa
975.24.11.50.1Iris-setosa
985.33.71.50.2Iris-setosa
995.43.04.51.5Iris-versicolor
1005.43.41.50.4Iris-setosa
Rows: 1-100 | Columns: 5

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_iris()
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 NaiveBayes model:

from verticapy.machine_learning.vertica import NaiveBayes

Then we can create the model:

model = NaiveBayes()

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,
    [
        "SepalLengthCm",
        "SepalWidthCm",
        "PetalLengthCm",
        "PetalWidthCm",
    ],
    "Species",
    test,
)



=======
details
=======
index|  predictor  |  type   
-----+-------------+---------
  0  |   Species   |ResponseC
  1  |SepalLengthCm|Gaussian 
  2  |SepalWidthCm |Gaussian 
  3  |PetalLengthCm|Gaussian 
  4  |PetalWidthCm |Gaussian 


=====
prior
=====
     class     |probability
---------------+-----------
  Iris-setosa  |  0.37356  
Iris-versicolor|  0.21264  
Iris-virginica |  0.41379  


===========
call_string
===========
naive_bayes('"public"."_verticapy_tmp_naivebayes_v_demo_4033768055a411ef880f0242ac120002_"', '"public"."_verticapy_tmp_view_v_demo_4040d6e055a411ef880f0242ac120002_"', '"species"', '"SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm"' USING PARAMETERS exclude_columns='', alpha=1)

====================
gaussian.Iris-setosa
====================
index|   mu   |sigma_sq
-----+--------+--------
  1  | 4.32000| 0.82350
  2  | 3.88000| 0.35944
  3  | 3.17385| 4.26602
  4  | 3.38615|14.06652


========================
gaussian.Iris-versicolor
========================
index|   mu   |sigma_sq
-----+--------+--------
  1  | 6.00270| 0.27860
  2  | 2.82703| 0.08147
  3  | 4.32432| 0.22467
  4  | 1.36216| 0.03853


=======================
gaussian.Iris-virginica
=======================
index|   mu   |sigma_sq
-----+--------+--------
  1  | 5.66806| 1.53291
  2  | 3.65972| 0.80413
  3  | 7.17361| 4.21436
  4  | 1.92361| 0.05760


===============
Additional Info
===============
       Name       | Value  
------------------+--------
      alpha       | 1.00000
accepted_row_count|  174   
rejected_row_count|   0    

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.

Metrics

We can get the entire report using:

model.report()
Iris-setosa
Iris-versicolor
Iris-virginica
avg_macro
avg_weighted
avg_micro
auc1.01.01.01.01.0[null]
prc_auc1.01.01.01.01.0[null]
accuracy1.01.01.01.01.01.0
log_loss0.003315742651767050.007041231928899150.005482820459933750.0052799316801999840.005007476613146998[null]
precision1.01.01.01.01.01.0
recall1.01.01.01.01.01.0
f1_score1.01.01.01.01.01.0
mcc1.01.01.01.01.01.0
informedness1.01.01.01.01.01.0
markedness1.01.01.01.01.01.0
csi1.01.01.01.01.01.0
Rows: 1-11 | Columns: 7

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)
Iris-setosa
Iris-versicolor
Iris-virginica
avg_macro
avg_weighted
avg_micro
auc1.01.01.01.01.0[null]
prc_auc1.01.01.01.01.0[null]
accuracy0.97727272727272730.97727272727272731.00.98484848484848490.98347107438016530.9848484848484849
log_loss0.003315742651767050.007041231928899150.005482820459933750.0052799316801999840.005007476613146998[null]
precision0.950.92857142857142861.00.95952380952380960.95730519480519480.9565217391304348
recall1.01.01.01.01.01.0
f1_score0.97435897435897430.9629629629629631.00.97910731244064570.9779849779849780.9777777777777777
mcc0.95498691090506570.94795438261592381.00.96764709784032990.96518541548189230.966841563388569
informedness0.960.9677419354838711.00.97591397849462370.97319648093841640.9772727272727273
markedness0.950.92857142857142861.00.95952380952380960.95730519480519480.9565217391304348
csi0.950.92857142857142861.00.95952380952380960.95730519480519480.9565217391304348
Rows: 1-11 | Columns: 7

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

model.score(metric = "f1", average = "macro")
Out[4]: 1.0

Note

For multi-class scoring, verticapy allows the flexibility to use three averaging techniques: micro, macro and weighted. Please refer to this link for more details on how they are calculated.

Prediction

Prediction is straight-forward:

model.predict(
    test,
    [
        "SepalLengthCm",
        "SepalWidthCm",
        "PetalLengthCm",
        "PetalWidthCm",
    ],
    "prediction",
)
123
SepalLengthCm
Numeric(7)
123
SepalWidthCm
Numeric(7)
123
PetalLengthCm
Numeric(7)
123
PetalWidthCm
Numeric(7)
Abc
Species
Varchar(30)
Abc
prediction
Varchar(100)
13.34.55.67.8Iris-setosaIris-setosa
23.34.55.67.8Iris-setosaIris-setosa
33.34.55.67.8Iris-setosaIris-setosa
43.34.55.67.8Iris-setosaIris-setosa
53.34.55.67.8Iris-setosaIris-setosa
63.34.55.67.8Iris-setosaIris-setosa
74.34.79.61.8Iris-virginicaIris-virginica
84.34.79.61.8Iris-virginicaIris-virginica
94.34.79.61.8Iris-virginicaIris-virginica
104.43.01.30.2Iris-setosaIris-setosa
114.63.21.40.2Iris-setosaIris-setosa
124.63.41.40.3Iris-setosaIris-setosa
134.73.21.60.2Iris-setosaIris-setosa
144.83.01.40.3Iris-setosaIris-setosa
154.83.41.90.2Iris-setosaIris-setosa
164.93.11.50.1Iris-setosaIris-setosa
175.02.03.51.0Iris-versicolorIris-versicolor
185.03.01.60.2Iris-setosaIris-setosa
195.03.31.40.2Iris-setosaIris-setosa
205.03.41.50.2Iris-setosaIris-setosa
215.13.81.60.2Iris-setosaIris-setosa
225.52.43.81.1Iris-versicolorIris-versicolor
235.52.54.01.3Iris-versicolorIris-versicolor
245.52.64.41.2Iris-versicolorIris-versicolor
255.62.93.61.3Iris-versicolorIris-versicolor
265.63.04.11.3Iris-versicolorIris-versicolor
275.72.63.51.0Iris-versicolorIris-versicolor
285.72.84.51.3Iris-versicolorIris-versicolor
295.73.04.21.2Iris-versicolorIris-versicolor
305.74.41.50.4Iris-setosaIris-setosa
315.82.64.01.2Iris-versicolorIris-versicolor
325.82.75.11.9Iris-virginicaIris-virginica
336.02.24.01.0Iris-versicolorIris-versicolor
346.22.24.51.5Iris-versicolorIris-versicolor
356.33.45.62.4Iris-virginicaIris-virginica
366.42.75.31.9Iris-virginicaIris-virginica
376.43.25.32.3Iris-virginicaIris-virginica
386.73.35.72.1Iris-virginicaIris-virginica
396.83.25.92.3Iris-virginicaIris-virginica
406.93.14.91.5Iris-versicolorIris-versicolor
417.63.06.62.1Iris-virginicaIris-virginica
424.34.79.61.8Iris-virginicaIris-virginica
433.34.55.67.8Iris-setosaIris-setosa
444.34.79.61.8Iris-virginicaIris-virginica
Rows: 1-44 | Columns: 6

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,
    [
        "SepalLengthCm",
        "SepalWidthCm",
        "PetalLengthCm",
        "PetalWidthCm",
    ],
    "prediction",
)
123
SepalLengthCm
Numeric(7)
123
SepalWidthCm
Numeric(7)
123
PetalLengthCm
Numeric(7)
123
PetalWidthCm
Numeric(7)
Abc
Species
Varchar(30)
Abc
prediction
Varchar(100)
Abc
prediction_irissetosa
Varchar(100)
Abc
prediction_irisversicolor
Varchar(100)
Abc
prediction_irisvirginica
Varchar(100)
13.34.55.67.8Iris-setosaIris-setosa11.06069e-2455.63887e-130
23.34.55.67.8Iris-setosaIris-setosa11.06069e-2455.63887e-130
33.34.55.67.8Iris-setosaIris-setosa11.06069e-2455.63887e-130
43.34.55.67.8Iris-setosaIris-setosa11.06069e-2455.63887e-130
53.34.55.67.8Iris-setosaIris-setosa11.06069e-2455.63887e-130
63.34.55.67.8Iris-setosaIris-setosa11.06069e-2455.63887e-130
74.34.79.61.8Iris-virginicaIris-virginica0.002746564.24979e-380.997253
84.34.79.61.8Iris-virginicaIris-virginica0.002746564.24979e-380.997253
94.34.79.61.8Iris-virginicaIris-virginica0.002746564.24979e-380.997253
104.43.01.30.2Iris-setosaIris-setosa13.19264e-162.59496e-12
114.63.21.40.2Iris-setosaIris-setosa11.17083e-152.58653e-12
124.63.41.40.3Iris-setosaIris-setosa14.65624e-153.66039e-11
134.73.21.60.2Iris-setosaIris-setosa12.26247e-143.48191e-12
144.83.01.40.3Iris-setosaIris-setosa11.73051e-137.72312e-11
154.83.41.90.2Iris-setosaIris-setosa12.38517e-134.10572e-12
164.93.11.50.1Iris-setosaIris-setosa11.46024e-152.1562e-13
175.02.03.51.0Iris-versicolorIris-versicolor0.2062160.7862380.00754662
185.03.01.60.2Iris-setosaIris-setosa12.86423e-136.65627e-12
195.03.31.40.2Iris-setosaIris-setosa14.1503e-153.61705e-12
205.03.41.50.2Iris-setosaIris-setosa16.50627e-153.56843e-12
215.13.81.60.2Iris-setosaIris-setosa15.45251e-163.36887e-12
225.52.43.81.1Iris-versicolorIris-versicolor0.002044480.9976690.000286265
235.52.54.01.3Iris-versicolorIris-versicolor0.0005647460.9985430.000892194
245.52.64.41.2Iris-versicolorIris-versicolor0.0005585320.9991190.000322096
255.62.93.61.3Iris-versicolorIris-versicolor0.002217760.9965650.00121676
265.63.04.11.3Iris-versicolorIris-versicolor0.001075610.9981250.000799052
275.72.63.51.0Iris-versicolorIris-versicolor0.006073170.9938050.000121715
285.72.84.51.3Iris-versicolorIris-versicolor0.0003370170.9990490.000614173
295.73.04.21.2Iris-versicolorIris-versicolor0.0009616190.9987470.000291345
305.74.41.50.4Iris-setosaIris-setosa13.85312e-172.08606e-09
315.82.64.01.2Iris-versicolorIris-versicolor0.0003234090.9994680.000208107
325.82.75.11.9Iris-virginicaIris-virginica0.006058160.234720.759222
336.02.24.01.0Iris-versicolorIris-versicolor0.001210180.9986080.000182079
346.22.24.51.5Iris-versicolorIris-versicolor0.0001557710.9822690.0175755
356.33.45.62.4Iris-virginicaIris-virginica0.04174755.67852e-070.958252
366.42.75.31.9Iris-virginicaIris-virginica0.001899510.1101030.887998
376.43.25.32.3Iris-virginicaIris-virginica0.01740185.90872e-050.982539
386.73.35.72.1Iris-virginicaIris-virginica0.002725046.97232e-050.997205
396.83.25.92.3Iris-virginicaIris-virginica0.004222418.58287e-070.995777
406.93.14.91.5Iris-versicolorIris-versicolor0.0004665820.9508880.0486451
417.63.06.62.1Iris-virginicaIris-virginica8.34743e-058.72557e-090.999917
424.34.79.61.8Iris-virginicaIris-virginica0.002746564.24979e-380.997253
433.34.55.67.8Iris-setosaIris-setosa11.06069e-2455.63887e-130
444.34.79.61.8Iris-virginicaIris-virginica0.002746564.24979e-380.997253
Rows: 1-44 | Columns: 9

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.

model.confusion_matrix()
Out[5]: 
array([[19,  0,  0],
       [ 0, 13,  0],
       [ 0,  0, 12]])

Hint

In the context of multi-class classification, you typically work with an overall confusion matrix that summarizes the classification efficiency across all classes. However, you have the flexibility to specify a pos_label and adjust the cutoff threshold. In this case, a binary confusion matrix is computed, where the chosen class is treated as the positive class, allowing you to evaluate its efficiency as if it were a binary classification problem.

model.confusion_matrix(pos_label = "Iris-setosa", cutoff = 0.6)
Out[6]: 
array([[25,  0],
       [ 0, 19]])

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(pos_label = "Iris-setosa")

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

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

model.contour(pos_label = "Iris-setosa")

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[7]: {'alpha': 1.0, 'nbtype': 'auto'}

And to manually change some of the parameters:

model.set_params({'alpha': 0.9})

Model Register

In order to register the model for tracking and versioning:

model.register("model_v1")

Please refer to Model Tracking and Versioning 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[9]: 'CASE WHEN "SepalLengthCm" IS NULL OR "SepalWidthCm" IS NULL OR "PetalLengthCm" IS NULL OR "PetalWidthCm" IS NULL THEN NULL WHEN 0.3222195606763497 * EXP(- POWER("SepalLengthCm" - 5.66805555555555, 2) / 3.06581768388106) * 0.4448842886396511 * EXP(- POWER("SepalWidthCm" - 3.65972222222222, 2) / 1.60825899843508) * 0.19433187084595704 * EXP(- POWER("PetalLengthCm" - 7.17361111111111, 2) / 8.42872848200312) * 1.6622064385805801 * EXP(- POWER("PetalWidthCm" - 1.92361111111111, 2) / 0.1152073552425672) * 0.413793103448276 >= 0.4396208322277158 * EXP(- POWER("SepalLengthCm" - 4.32, 2) / 1.64700000000002) * 0.6654238662946499 * EXP(- POWER("SepalWidthCm" - 3.88, 2) / 0.718875) * 0.1931516474222166 * EXP(- POWER("PetalLengthCm" - 3.17384615384615, 2) / 8.53204807692312) * 0.1063693901905793 * EXP(- POWER("PetalWidthCm" - 3.38615384615384, 2) / 28.133048076923) * 0.373563218390805 AND 0.3222195606763497 * EXP(- POWER("SepalLengthCm" - 5.66805555555555, 2) / 3.06581768388106) * 0.4448842886396511 * EXP(- POWER("SepalWidthCm" - 3.65972222222222, 2) / 1.60825899843508) * 0.19433187084595704 * EXP(- POWER("PetalLengthCm" - 7.17361111111111, 2) / 8.42872848200312) * 1.6622064385805801 * EXP(- POWER("PetalWidthCm" - 1.92361111111111, 2) / 0.1152073552425672) * 0.413793103448276 >= 0.7558170785361528 * EXP(- POWER("SepalLengthCm" - 6.0027027027027, 2) / 0.557207207207246) * 1.3976785034573909 * EXP(- POWER("SepalWidthCm" - 2.82702702702703, 2) / 0.1629429429429478) * 0.8416622377514259 * EXP(- POWER("PetalLengthCm" - 4.32432432432432, 2) / 0.449339339339342) * 2.032445245252479 * EXP(- POWER("PetalWidthCm" - 1.36216216216216, 2) / 0.0770570570570574) * 0.21264367816092 THEN \'Iris-virginica\' WHEN 0.7558170785361528 * EXP(- POWER("SepalLengthCm" - 6.0027027027027, 2) / 0.557207207207246) * 1.3976785034573909 * EXP(- POWER("SepalWidthCm" - 2.82702702702703, 2) / 0.1629429429429478) * 0.8416622377514259 * EXP(- POWER("PetalLengthCm" - 4.32432432432432, 2) / 0.449339339339342) * 2.032445245252479 * EXP(- POWER("PetalWidthCm" - 1.36216216216216, 2) / 0.0770570570570574) * 0.21264367816092 >= 0.4396208322277158 * EXP(- POWER("SepalLengthCm" - 4.32, 2) / 1.64700000000002) * 0.6654238662946499 * EXP(- POWER("SepalWidthCm" - 3.88, 2) / 0.718875) * 0.1931516474222166 * EXP(- POWER("PetalLengthCm" - 3.17384615384615, 2) / 8.53204807692312) * 0.1063693901905793 * EXP(- POWER("PetalWidthCm" - 3.38615384615384, 2) / 28.133048076923) * 0.373563218390805 THEN \'Iris-versicolor\' ELSE \'Iris-setosa\' END'

To Python

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

X = [[5, 2, 3, 1]]

model.to_python()(X)
Out[11]: array(['Iris-setosa'], dtype='<U11')

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, alpha: Annotated[int | float | Decimal, 'Python Numbers'] = 1.0, nbtype: Literal['auto', 'bernoulli', 'categorical', 'multinomial', 'gaussian'] = 'auto') None

Must be overridden in the child class

Methods

__init__([name, overwrite_model, alpha, nbtype])

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.

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([pos_label, nbins, show, chart])

Draws the model Lift Chart.

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