Loading...

verticapy.machine_learning.vertica.naive_bayes.NaiveBayes#

class verticapy.machine_learning.vertica.naive_bayes.NaiveBayes(name: str = None, overwrite_model: bool = False, alpha: int | float | Decimal = 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
273.34.55.67.8Iris-setosa
283.34.55.67.8Iris-setosa
293.34.55.67.8Iris-setosa
303.34.55.67.8Iris-setosa
313.34.55.67.8Iris-setosa
323.34.55.67.8Iris-setosa
333.34.55.67.8Iris-setosa
343.34.55.67.8Iris-setosa
353.34.55.67.8Iris-setosa
363.34.55.67.8Iris-setosa
373.34.55.67.8Iris-setosa
383.34.55.67.8Iris-setosa
393.34.55.67.8Iris-setosa
403.34.55.67.8Iris-setosa
413.34.55.67.8Iris-setosa
423.34.55.67.8Iris-setosa
434.33.01.10.1Iris-setosa
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.34.79.61.8Iris-virginica
554.34.79.61.8Iris-virginica
564.34.79.61.8Iris-virginica
574.34.79.61.8Iris-virginica
584.34.79.61.8Iris-virginica
594.34.79.61.8Iris-virginica
604.34.79.61.8Iris-virginica
614.34.79.61.8Iris-virginica
624.34.79.61.8Iris-virginica
634.34.79.61.8Iris-virginica
644.34.79.61.8Iris-virginica
654.34.79.61.8Iris-virginica
664.34.79.61.8Iris-virginica
674.34.79.61.8Iris-virginica
684.34.79.61.8Iris-virginica
694.34.79.61.8Iris-virginica
704.34.79.61.8Iris-virginica
714.34.79.61.8Iris-virginica
724.34.79.61.8Iris-virginica
734.34.79.61.8Iris-virginica
744.34.79.61.8Iris-virginica
754.34.79.61.8Iris-virginica
764.34.79.61.8Iris-virginica
774.34.79.61.8Iris-virginica
784.34.79.61.8Iris-virginica
794.34.79.61.8Iris-virginica
804.34.79.61.8Iris-virginica
814.34.79.61.8Iris-virginica
824.34.79.61.8Iris-virginica
834.34.79.61.8Iris-virginica
844.34.79.61.8Iris-virginica
854.34.79.61.8Iris-virginica
864.42.91.40.2Iris-setosa
874.43.01.30.2Iris-setosa
884.43.21.30.2Iris-setosa
894.52.31.30.3Iris-setosa
904.63.11.50.2Iris-setosa
914.63.21.40.2Iris-setosa
924.63.41.40.3Iris-setosa
934.63.61.00.2Iris-setosa
944.73.21.30.2Iris-setosa
954.73.21.60.2Iris-setosa
964.83.01.40.1Iris-setosa
974.83.01.40.3Iris-setosa
984.83.11.60.2Iris-setosa
994.83.41.60.2Iris-setosa
1004.83.41.90.2Iris-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,
)

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.00.98062015503875970.99166666666666670.99076227390180880.9935453251067868[null]
prc_auc1.00.8990740740740740.99116511658631230.96341306355346210.9833144335207336[null]
accuracy1.00.89795918367346940.89795918367346940.93197278911564630.93752603082049150.9319727891156463
log_loss0.002580570604447790.07797843506045650.08089586382642820.0538182898304441560.05017147313635763[null]
precision1.00.55555555555555560.95238095238095230.83597883597883590.92225461613216720.8979591836734694
recall1.00.83333333333333340.83333333333333340.8888888888888890.89795918367346940.8979591836734694
f1_score1.00.66666666666666670.8888888888888890.8518518518518520.90476190476190480.8979591836734694
mcc1.00.62671813797086720.80138768534475380.80936860777187370.85701251583876110.8469387755102041
informedness1.00.74031007751937980.79333333333333340.84454780361757110.86697674418604660.8469387755102042
markedness1.00.53055555555555540.80952380952380930.7800264550264550.84922254616132150.8469387755102042
csi1.00.50.80.76666666666666660.84081632653061230.8148148148148148
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.00.98062015503875970.99166666666666670.99076227390180880.9935453251067868[null]
prc_auc1.00.8990740740740740.99116511658631230.96341306355346210.9833144335207336[null]
accuracy1.00.91836734693877550.93877551020408170.95238095238095240.96001665972511460.9523809523809523
log_loss0.002580570604447790.07797843506045650.08089586382642820.0538182898304441560.05017147313635763[null]
precision1.00.60.95652173913043480.85217391304347830.92972493345164160.9038461538461539
recall1.01.00.91666666666666660.97222222222222220.95918367346938770.9591836734693877
f1_score1.00.74999999999999990.93617021276595740.89539007092198590.93812418584455050.9306930693069307
mcc1.00.7376896681610960.87813144073184390.871940369630980.90818964462307820.8953885457014064
informedness1.00.90697674418604630.87666666666666650.92788113695090420.92820123398196470.9081632653061225
markedness1.00.60000000000000010.87959866220735790.82653288740245270.89204832434646110.8827935222672065
csi1.00.60.880.82666666666666670.89224489795918360.8703703703703703
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]: 0.851851851851852

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
73.34.55.67.8Iris-setosaIris-setosa
83.34.55.67.8Iris-setosaIris-setosa
93.34.55.67.8Iris-setosaIris-setosa
104.34.79.61.8Iris-virginicaIris-virginica
114.34.79.61.8Iris-virginicaIris-virginica
124.34.79.61.8Iris-virginicaIris-virginica
134.34.79.61.8Iris-virginicaIris-virginica
144.34.79.61.8Iris-virginicaIris-virginica
154.34.79.61.8Iris-virginicaIris-virginica
164.34.79.61.8Iris-virginicaIris-virginica
174.34.79.61.8Iris-virginicaIris-virginica
184.34.79.61.8Iris-virginicaIris-virginica
194.63.41.40.3Iris-setosaIris-setosa
204.63.61.00.2Iris-setosaIris-setosa
214.83.11.60.2Iris-setosaIris-setosa
224.92.54.51.7Iris-virginicaIris-versicolor
235.03.31.40.2Iris-setosaIris-setosa
245.13.51.40.2Iris-setosaIris-setosa
255.43.71.50.2Iris-setosaIris-setosa
265.43.91.30.4Iris-setosaIris-setosa
275.43.91.70.4Iris-setosaIris-setosa
285.53.51.30.2Iris-setosaIris-setosa
295.73.04.21.2Iris-versicolorIris-versicolor
305.93.24.81.8Iris-versicolorIris-virginica
316.02.75.11.6Iris-versicolorIris-versicolor
326.03.04.81.8Iris-virginicaIris-versicolor
336.12.84.01.3Iris-versicolorIris-versicolor
346.12.94.71.4Iris-versicolorIris-versicolor
356.22.94.31.3Iris-versicolorIris-versicolor
366.32.55.01.9Iris-virginicaIris-virginica
376.32.74.91.8Iris-virginicaIris-versicolor
386.32.85.11.5Iris-virginicaIris-versicolor
396.32.95.61.8Iris-virginicaIris-virginica
406.42.85.62.1Iris-virginicaIris-virginica
416.73.05.22.3Iris-virginicaIris-virginica
426.73.15.62.4Iris-virginicaIris-virginica
436.73.35.72.5Iris-virginicaIris-virginica
446.93.15.42.1Iris-virginicaIris-virginica
457.23.66.12.5Iris-virginicaIris-virginica
467.72.66.92.3Iris-virginicaIris-virginica
477.73.06.12.3Iris-virginicaIris-virginica
483.34.55.67.8Iris-setosaIris-setosa
494.34.79.61.8Iris-virginicaIris-virginica
Rows: 1-49 | 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-setosa15.32765e-2593.04755e-177
23.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
33.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
43.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
53.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
63.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
73.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
83.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
93.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
104.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
114.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
124.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
134.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
144.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
154.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
164.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
174.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
184.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
194.63.41.40.3Iris-setosaIris-setosa15.90302e-142.07352e-14
204.63.61.00.2Iris-setosaIris-setosa13.40448e-182.60895e-16
214.83.11.60.2Iris-setosaIris-setosa16.58419e-138.9424e-16
224.92.54.51.7Iris-virginicaIris-versicolor0.0155030.8036650.180832
235.03.31.40.2Iris-setosaIris-setosa14.30877e-147.72366e-16
245.13.51.40.2Iris-setosaIris-setosa11.44006e-147.63312e-16
255.43.71.50.2Iris-setosaIris-setosa12.42326e-141.19441e-15
265.43.91.30.4Iris-setosaIris-setosa16.09786e-141.63566e-12
275.43.91.70.4Iris-setosaIris-setosa17.05111e-122.44434e-12
285.53.51.30.2Iris-setosaIris-setosa11.68971e-141.2626e-15
295.73.04.21.2Iris-versicolorIris-versicolor0.0006409770.9993134.62317e-05
305.93.24.81.8Iris-versicolorIris-virginica0.01674340.3345840.648672
316.02.75.11.6Iris-versicolorIris-versicolor0.001203720.9402760.0585206
326.03.04.81.8Iris-virginicaIris-versicolor0.00751020.5519570.440533
336.12.84.01.3Iris-versicolorIris-versicolor0.0001140080.9998018.52623e-05
346.12.94.71.4Iris-versicolorIris-versicolor0.0002391580.9986510.0011096
356.22.94.31.3Iris-versicolorIris-versicolor0.0001187770.9997540.000126845
366.32.55.01.9Iris-virginicaIris-virginica0.002533090.1871170.81035
376.32.74.91.8Iris-virginicaIris-versicolor0.002091390.614470.383439
386.32.85.11.5Iris-virginicaIris-versicolor0.0005126240.982030.0174578
396.32.95.61.8Iris-virginicaIris-virginica0.003581590.02810040.968318
406.42.85.62.1Iris-virginicaIris-virginica0.00377080.0002596010.99597
416.73.05.22.3Iris-virginicaIris-virginica0.01528033.43483e-050.984685
426.73.15.62.4Iris-virginicaIris-virginica0.03302083.76687e-070.966979
436.73.35.72.5Iris-virginicaIris-virginica0.121421.01411e-080.87858
446.93.15.42.1Iris-virginicaIris-virginica0.002116980.0001637280.997719
457.23.66.12.5Iris-virginicaIris-virginica0.03135741.59568e-110.968643
467.72.66.92.3Iris-virginicaIris-virginica0.0001107411.41021e-120.999889
477.73.06.12.3Iris-virginicaIris-virginica0.0004413863.17972e-090.999559
483.34.55.67.8Iris-setosaIris-setosa15.32765e-2593.04755e-177
494.34.79.61.8Iris-virginicaIris-virginica0.002329981.9755e-380.99767
Rows: 1-49 | 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,  5,  1],
       [ 0,  4, 20]])

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([[30,  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.3244726886220993 * EXP(- POWER("SepalLengthCm" - 5.41486486486486, 2) / 3.02338763420952) * 0.4491388607461844 * EXP(- POWER("SepalWidthCm" - 3.87027027027027, 2) / 1.577934098482068) * 0.19437108725076185 * EXP(- POWER("PetalLengthCm" - 7.6472972972973, 2) / 8.42532765642342) * 1.9328615063345225 * EXP(- POWER("PetalWidthCm" - 1.90135135135135, 2) / 0.085201777119588) * 0.375634517766497 >= 0.4505140594417424 * EXP(- POWER("SepalLengthCm" - 4.17594936708861, 2) / 1.568315481986386) * 0.6362690709840972 * EXP(- POWER("SepalWidthCm" - 3.92405063291139, 2) / 0.786264199935092) * 0.1921613470484011 * EXP(- POWER("PetalLengthCm" - 3.46075949367089, 2) / 8.6202142161636) * 0.10494293818212533 * EXP(- POWER("PetalWidthCm" - 3.87721518987342, 2) / 28.9030509574812) * 0.401015228426396 AND 0.3244726886220993 * EXP(- POWER("SepalLengthCm" - 5.41486486486486, 2) / 3.02338763420952) * 0.4491388607461844 * EXP(- POWER("SepalWidthCm" - 3.87027027027027, 2) / 1.577934098482068) * 0.19437108725076185 * EXP(- POWER("PetalLengthCm" - 7.6472972972973, 2) / 8.42532765642342) * 1.9328615063345225 * EXP(- POWER("PetalWidthCm" - 1.90135135135135, 2) / 0.085201777119588) * 0.375634517766497 >= 0.7292913131474902 * EXP(- POWER("SepalLengthCm" - 5.92727272727273, 2) / 0.598477801268546) * 1.2291225672892683 * EXP(- POWER("SepalWidthCm" - 2.75, 2) / 0.210697674418604) * 0.8486436275756416 * EXP(- POWER("PetalLengthCm" - 4.225, 2) / 0.441976744186038) * 2.0783710790945586 * EXP(- POWER("PetalWidthCm" - 1.31136363636364, 2) / 0.073689217758985) * 0.223350253807107 THEN \'Iris-virginica\' WHEN 0.7292913131474902 * EXP(- POWER("SepalLengthCm" - 5.92727272727273, 2) / 0.598477801268546) * 1.2291225672892683 * EXP(- POWER("SepalWidthCm" - 2.75, 2) / 0.210697674418604) * 0.8486436275756416 * EXP(- POWER("PetalLengthCm" - 4.225, 2) / 0.441976744186038) * 2.0783710790945586 * EXP(- POWER("PetalWidthCm" - 1.31136363636364, 2) / 0.073689217758985) * 0.223350253807107 >= 0.4505140594417424 * EXP(- POWER("SepalLengthCm" - 4.17594936708861, 2) / 1.568315481986386) * 0.6362690709840972 * EXP(- POWER("SepalWidthCm" - 3.92405063291139, 2) / 0.786264199935092) * 0.1921613470484011 * EXP(- POWER("PetalLengthCm" - 3.46075949367089, 2) / 8.6202142161636) * 0.10494293818212533 * EXP(- POWER("PetalWidthCm" - 3.87721518987342, 2) / 28.9030509574812) * 0.401015228426396 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-versicolor'], dtype='<U15')

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: int | float | Decimal = 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