Loading...

verticapy.machine_learning.vertica.cluster.NearestCentroid

class verticapy.machine_learning.vertica.cluster.NearestCentroid(name: str = None, overwrite_model: bool = False, p: int = 2)

Creates a NearestCentroid object using the k-nearest centroid algorithm. This object uses pure SQL to compute the distances and final score.

Important

This algorithm is not Vertica Native and relies solely on SQL for attribute computation. While this model does not take advantage of the benefits provided by a model management system, including versioning and tracking, the SQL code it generates can still be used to create a pipeline.

Parameters

p: int, optional

The p corresponding to the one of the p-distances (distance metric used to compute the model).

Attributes

Many attributes are created during the fitting phase.

clusters_: numpy.array

Cluster centers.

p_: int

The p of the p-distances.

classes_: numpy.array

The classes labels.

Note

All attributes can be accessed using the get_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)
14.63.61.00.2Iris-setosa
24.73.21.30.2Iris-setosa
34.73.21.60.2Iris-setosa
44.83.01.40.1Iris-setosa
54.83.11.60.2Iris-setosa
64.83.41.90.2Iris-setosa
74.93.01.40.2Iris-setosa
84.93.11.50.1Iris-setosa
94.93.11.50.1Iris-setosa
104.93.11.50.1Iris-setosa
115.02.33.31.0Iris-versicolor
125.03.41.50.2Iris-setosa
135.13.51.40.2Iris-setosa
145.43.04.51.5Iris-versicolor
155.43.41.50.4Iris-setosa
165.43.91.30.4Iris-setosa
175.52.43.71.0Iris-versicolor
185.52.43.81.1Iris-versicolor
195.62.74.21.3Iris-versicolor
205.73.04.21.2Iris-versicolor
215.74.41.50.4Iris-setosa
225.82.85.12.4Iris-virginica
235.93.24.81.8Iris-versicolor
246.13.04.61.4Iris-versicolor
256.13.04.91.8Iris-virginica
266.32.54.91.5Iris-versicolor
276.33.34.71.6Iris-versicolor
286.33.36.02.5Iris-virginica
296.42.94.31.3Iris-versicolor
306.53.05.51.8Iris-virginica
316.53.05.82.2Iris-virginica
326.73.05.01.7Iris-versicolor
336.82.84.81.4Iris-versicolor
346.83.25.92.3Iris-virginica
357.03.24.71.4Iris-versicolor
367.13.05.92.1Iris-virginica
377.73.86.72.2Iris-virginica
384.42.91.40.2Iris-setosa
394.52.31.30.3Iris-setosa
404.83.41.60.2Iris-setosa
415.02.03.51.0Iris-versicolor
425.13.31.70.5Iris-setosa
435.13.41.50.2Iris-setosa
445.22.73.91.4Iris-versicolor
455.23.51.50.2Iris-setosa
465.24.11.50.1Iris-setosa
475.43.91.70.4Iris-setosa
485.53.51.30.2Iris-setosa
495.63.04.11.3Iris-versicolor
505.82.73.91.2Iris-versicolor
515.82.75.11.9Iris-virginica
525.82.75.11.9Iris-virginica
535.93.04.21.5Iris-versicolor
545.93.05.11.8Iris-virginica
556.02.75.11.6Iris-versicolor
566.02.94.51.5Iris-versicolor
576.12.84.71.2Iris-versicolor
586.22.84.81.8Iris-virginica
596.22.94.31.3Iris-versicolor
606.32.34.41.3Iris-versicolor
616.32.74.91.8Iris-virginica
626.43.25.32.3Iris-virginica
636.52.84.61.5Iris-versicolor
646.53.05.22.0Iris-virginica
656.53.25.12.0Iris-virginica
666.62.94.61.3Iris-versicolor
676.63.04.41.4Iris-versicolor
686.73.14.41.4Iris-versicolor
696.73.14.71.5Iris-versicolor
706.93.14.91.5Iris-versicolor
716.93.15.42.1Iris-virginica
726.93.25.72.3Iris-virginica
737.23.05.81.6Iris-virginica
747.23.26.01.8Iris-virginica
757.32.96.31.8Iris-virginica
767.72.66.92.3Iris-virginica
773.34.55.67.8Iris-setosa
783.34.55.67.8Iris-setosa
793.34.55.67.8Iris-setosa
803.34.55.67.8Iris-setosa
813.34.55.67.8Iris-setosa
823.34.55.67.8Iris-setosa
833.34.55.67.8Iris-setosa
843.34.55.67.8Iris-setosa
853.34.55.67.8Iris-setosa
863.34.55.67.8Iris-setosa
873.34.55.67.8Iris-setosa
883.34.55.67.8Iris-setosa
893.34.55.67.8Iris-setosa
903.34.55.67.8Iris-setosa
913.34.55.67.8Iris-setosa
923.34.55.67.8Iris-setosa
933.34.55.67.8Iris-setosa
943.34.55.67.8Iris-setosa
953.34.55.67.8Iris-setosa
963.34.55.67.8Iris-setosa
973.34.55.67.8Iris-setosa
983.34.55.67.8Iris-setosa
993.34.55.67.8Iris-setosa
1003.34.55.67.8Iris-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 NearestCentroid model:

from verticapy.machine_learning.vertica import NearestCentroid

Then we can create the model:

model = NearestCentroid(p = 2)

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.

Important

As this model is not native, it solely relies on SQL statements to compute various attributes, storing them within the object. No data is saved in the database.

Metrics

We can get the entire report using:

model.report()
Iris-setosa
Iris-versicolor
Iris-virginica
avg_macro
avg_weighted
avg_micro
auc1.00.99584199584199540.91258741258741260.96947646947646940.9796881496881495[null]
prc_auc1.00.98813045928430540.88929425837320550.95914157255250350.9725586562560247[null]
accuracy0.660.540.880.69333333333333340.67720.6933333333333334
log_loss0.2707659047538550.1980627836893290.1722469719810910.213691886808091670.23018892806707011[null]
precision1.00.36111111111111111.00.78703703703703710.83388888888888890.54
recall0.346153846153846151.00.454545454545454530.60023310023310030.540.54
f1_score0.51428571428571420.53061224489795910.6250.55663265306122440.54288775510204080.54
mcc0.45014069095231980.36964393223294850.62764591446084780.482476845882038760.46826268285715940.31
informedness0.346153846153846260.37837837837837850.45454545454545460.393025893025893140.37837837837837850.31000000000000005
markedness0.58536585365853670.361111111111111160.86666666666666670.60438121047877150.58894579945799460.31000000000000005
csi0.346153846153846150.36111111111111110.454545454545454530.387270137270137260.37388888888888890.3698630136986301
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.99584199584199540.91258741258741260.96947646947646940.9796881496881495[null]
prc_auc1.00.98813045928430540.88929425837320550.95914157255250350.9725586562560247[null]
accuracy0.80.260.460.50666666666666670.58480.5066666666666667
log_loss0.2707659047538550.1980627836893290.1722469719810910.213691886808091670.23018892806707011[null]
precision0.72222222222222220.260.27777777777777780.420.50426666666666660.4016393442622951
recall1.01.00.90909090909090910.96969696969696970.980.98
f1_score0.83870967741935480.412698412698412730.42553191489361710.55898000167046160.63704764083624750.5697674418604652
mcc0.64907341364155120.00.223660568773420660.29091132747165730.38672350022375920.3024586990598921
informedness0.58333333333333350.00.242424242424242430.27525252525252530.35666666666666680.25
markedness0.7222222222222223-0.740.20634920634920650.062857142857142940.2285523809523810.3659250585480094
csi0.72222222222222220.260.27027027027027030.41749749749749750.5026150150150150.3983739837398374
Rows: 1-11 | Columns: 7

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

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

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(15)
14.73.21.30.2Iris-setosaIris-versicolor
25.13.51.40.2Iris-setosaIris-versicolor
35.43.41.50.4Iris-setosaIris-versicolor
45.43.91.30.4Iris-setosaIris-versicolor
55.52.43.71.0Iris-versicolorIris-versicolor
65.74.41.50.4Iris-setosaIris-versicolor
76.13.04.61.4Iris-versicolorIris-versicolor
86.13.04.91.8Iris-virginicaIris-versicolor
93.34.55.67.8Iris-setosaIris-setosa
103.34.55.67.8Iris-setosaIris-setosa
113.34.55.67.8Iris-setosaIris-setosa
123.34.55.67.8Iris-setosaIris-setosa
133.34.55.67.8Iris-setosaIris-setosa
143.34.55.67.8Iris-setosaIris-setosa
154.33.01.10.1Iris-setosaIris-versicolor
164.63.11.50.2Iris-setosaIris-versicolor
174.92.43.31.0Iris-versicolorIris-versicolor
185.03.51.30.3Iris-setosaIris-versicolor
195.13.51.40.3Iris-setosaIris-versicolor
205.23.41.40.2Iris-setosaIris-versicolor
215.52.34.01.3Iris-versicolorIris-versicolor
225.72.94.21.3Iris-versicolorIris-versicolor
235.73.81.70.3Iris-setosaIris-versicolor
245.84.01.20.2Iris-setosaIris-versicolor
256.03.44.51.6Iris-versicolorIris-versicolor
266.12.65.61.4Iris-virginicaIris-versicolor
276.73.05.22.3Iris-virginicaIris-versicolor
286.73.35.72.5Iris-virginicaIris-versicolor
297.42.86.11.9Iris-virginicaIris-versicolor
303.34.55.67.8Iris-setosaIris-setosa
313.34.55.67.8Iris-setosaIris-setosa
323.34.55.67.8Iris-setosaIris-setosa
335.02.03.51.0Iris-versicolorIris-versicolor
345.13.41.50.2Iris-setosaIris-versicolor
355.23.51.50.2Iris-setosaIris-versicolor
365.24.11.50.1Iris-setosaIris-versicolor
375.82.73.91.2Iris-versicolorIris-versicolor
386.62.94.61.3Iris-versicolorIris-versicolor
396.73.14.71.5Iris-versicolorIris-versicolor
407.32.96.31.8Iris-virginicaIris-versicolor
414.34.79.61.8Iris-virginicaIris-virginica
424.34.79.61.8Iris-virginicaIris-virginica
434.34.79.61.8Iris-virginicaIris-virginica
444.43.01.30.2Iris-setosaIris-versicolor
454.83.01.40.3Iris-setosaIris-versicolor
465.72.84.51.3Iris-versicolorIris-versicolor
475.82.64.01.2Iris-versicolorIris-versicolor
486.12.94.71.4Iris-versicolorIris-versicolor
497.23.66.12.5Iris-virginicaIris-virginica
504.34.79.61.8Iris-virginicaIris-virginica
Rows: 1-50 | 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(15)
123
prediction_irissetosa
Float(22)
123
prediction_irisversicolor
Float(22)
123
prediction_irisvirginica
Float(22)
14.73.21.30.2Iris-setosaIris-versicolor0.3129443161847860.4506963039688410.236359379846373
25.13.51.40.2Iris-setosaIris-versicolor0.3052429010982550.4597685569888840.234988541912861
35.43.41.50.4Iris-setosaIris-versicolor0.29841008566850.4726608324939790.228929081837521
45.43.91.30.4Iris-setosaIris-versicolor0.3114279223134390.4532291084692780.235342969217283
55.52.43.71.0Iris-versicolorIris-versicolor0.1578614950163230.6930445402053060.149093964778371
65.74.41.50.4Iris-setosaIris-versicolor0.3087228179530920.4513929039092530.239884278137655
76.13.04.61.4Iris-versicolorIris-versicolor0.08334088213601250.8176451540194020.0990139638445857
86.13.04.91.8Iris-virginicaIris-versicolor0.1462113857492990.6723936187771590.181394995473542
93.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
103.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
113.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
123.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
133.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
143.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
154.33.01.10.1Iris-setosaIris-versicolor0.3218130751454890.4368017730392410.241385151815271
164.63.11.50.2Iris-setosaIris-versicolor0.3091302573130960.455654578457270.235215164229634
174.92.43.31.0Iris-versicolorIris-versicolor0.2303123519511580.5776836932156110.192003954833231
185.03.51.30.3Iris-setosaIris-versicolor0.3119759393426010.4533003804688460.234723680188553
195.13.51.40.3Iris-setosaIris-versicolor0.307167976400930.459443878247590.233388145351481
205.23.41.40.2Iris-setosaIris-versicolor0.3019319750936980.4641814373174430.233886587588858
215.52.34.01.3Iris-versicolorIris-versicolor0.1403866821448630.7244040788168970.13520923903824
225.72.94.21.3Iris-versicolorIris-versicolor0.07349016280458960.8509173989923280.0755924382030824
235.73.81.70.3Iris-setosaIris-versicolor0.2904253561754940.4777554488334980.231819194991008
245.84.01.20.2Iris-setosaIris-versicolor0.3048117004312950.4551096892869640.24007861028174
256.03.44.51.6Iris-versicolorIris-versicolor0.1383205614782350.7092753769404860.152404061581278
266.12.65.61.4Iris-virginicaIris-versicolor0.1678328579242050.5470358795118150.28513126256398
276.73.05.22.3Iris-virginicaIris-versicolor0.2066885500410380.5224170693217380.270894380637223
286.73.35.72.5Iris-virginicaIris-versicolor0.2231043998100040.4273639008127190.349531699377277
297.42.86.11.9Iris-virginicaIris-versicolor0.2038924773248450.4276131425364130.368494380138742
303.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
313.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
323.34.55.67.8Iris-setosaIris-setosa0.4684579561577740.2521968486769830.279345195165243
334.34.79.61.8Iris-virginicaIris-virginica0.2096280082735720.2285645271043350.561807464622093
344.34.79.61.8Iris-virginicaIris-virginica0.2096280082735720.2285645271043350.561807464622093
354.34.79.61.8Iris-virginicaIris-virginica0.2096280082735720.2285645271043350.561807464622093
364.43.01.30.2Iris-setosaIris-versicolor0.3175680714553390.4445116593348340.237920269209827
374.83.01.40.3Iris-setosaIris-versicolor0.3080570423522190.4593431147710240.232599842876758
385.72.84.51.3Iris-versicolorIris-versicolor0.07778585734524410.8344670801297320.0877470625250235
395.82.64.01.2Iris-versicolorIris-versicolor0.0875488351626650.8246919305293650.08775923430797
406.12.94.71.4Iris-versicolorIris-versicolor0.08992369814600540.7997322251039340.110344076750061
417.23.66.12.5Iris-virginicaIris-virginica0.2232676973403440.3777428372436720.398989465415985
424.34.79.61.8Iris-virginicaIris-virginica0.2096280082735720.2285645271043350.561807464622093
435.02.03.51.0Iris-versicolorIris-versicolor0.2192585392058360.587355809684350.193385651109814
445.13.41.50.2Iris-setosaIris-versicolor0.3011375787297820.4654628076098220.233399613660396
455.23.51.50.2Iris-setosaIris-versicolor0.3003093701928250.4660241717998920.233666458007283
465.24.11.50.1Iris-setosaIris-versicolor0.3071907038455690.4516173294388140.241191966715617
475.82.73.91.2Iris-versicolorIris-versicolor0.0964247583162260.8093667417447760.0942084999389981
486.62.94.61.3Iris-versicolorIris-versicolor0.1199187263945610.7319588893481890.14812238425725
496.73.14.71.5Iris-versicolorIris-versicolor0.1448978474396010.6737559572762550.181346195284144
507.32.96.31.8Iris-virginicaIris-versicolor0.198476150090520.4068869973756780.394636852533801
Rows: 1-50 | 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([[ 9, 17,  0],
       [ 0, 13,  0],
       [ 0,  6,  5]])

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.

Specific confusion matrix:

model.confusion_matrix(pos_label = "Iris-setosa", cutoff = 0.6)
Out[6]: 
array([[24,  0],
       [26,  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(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]: {'p': 2}

And to manually change some of the parameters:

model.set_params({'p': 3})

Model Register

As this model is not native, it does not support model management and versioning. However, it is possible to use the SQL code it generates for deployment.

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 POWER(POWER("SepalLengthCm" - 5.38988764044944, 2) + POWER("SepalWidthCm" - 3.86179775280899, 2) + POWER("PetalLengthCm" - 7.63258426966292, 2) + POWER("PetalWidthCm" - 1.90898876404494, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.03918918918919, 2) + POWER("SepalWidthCm" - 3.99594594594595, 2) + POWER("PetalLengthCm" - 3.77027027027027, 2) + POWER("PetalWidthCm" - 4.42972972972973, 2), 1 / 2) AND POWER(POWER("SepalLengthCm" - 5.38988764044944, 2) + POWER("SepalWidthCm" - 3.86179775280899, 2) + POWER("PetalLengthCm" - 7.63258426966292, 2) + POWER("PetalWidthCm" - 1.90898876404494, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 5.98378378378378, 2) + POWER("SepalWidthCm" - 2.78648648648649, 2) + POWER("PetalLengthCm" - 4.29189189189189, 2) + POWER("PetalWidthCm" - 1.34594594594595, 2), 1 / 2) THEN \'Iris-virginica\' WHEN POWER(POWER("SepalLengthCm" - 5.98378378378378, 2) + POWER("SepalWidthCm" - 2.78648648648649, 2) + POWER("PetalLengthCm" - 4.29189189189189, 2) + POWER("PetalWidthCm" - 1.34594594594595, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.03918918918919, 2) + POWER("SepalWidthCm" - 3.99594594594595, 2) + POWER("PetalLengthCm" - 3.77027027027027, 2) + POWER("PetalWidthCm" - 4.42972972972973, 2), 1 / 2) 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=object)

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, p: int = 2) → None

Must be overridden in the child class

Methods

__init__([name, overwrite_model, p])

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

NearestCentroid models are not stored in the Vertica DB.

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