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)
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 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.01.00.90170940170940160.96723646723646720.9597902097902097[null]
prc_auc1.00.99999999999999980.90089782399564990.96696594133188320.9594582007254931[null]
accuracy0.77272727272727270.59090909090909090.81818181818181820.72727272727272740.75413223140495870.7272727272727273
log_loss0.2194100158111110.1848835322695210.2243855228853760.209559690322002640.21438321525343965[null]
precision1.00.33333333333333331.00.77777777777777770.86363636363636360.5909090909090909
recall0.41176470588235291.00.55555555555555560.65577342047930280.59090909090909090.5909090909090909
f1_score0.58333333333333340.50.71428571428571430.59920634920634930.61985930735930740.5909090909090909
mcc0.54815777613363380.402373908081478270.6517949073958690.53410886387032710.56073535682115280.38636363636363635
informedness0.41176470588235280.485714285714285770.55555555555555560.48434484905073140.48571428571428570.38636363636363624
markedness0.72972972972972980.333333333333333260.76470588235294110.60925631513866810.66295707472178060.38636363636363624
csi0.41176470588235290.33333333333333330.55555555555555560.43355119825708060.454545454545454530.41935483870967744
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.00.90170940170940160.96723646723646720.9597902097902097[null]
prc_auc1.00.99999999999999980.90089782399564990.96696594133188320.9594582007254931[null]
accuracy0.70454545454545460.204545454545454560.59090909090909090.50.55578512396694220.5
log_loss0.2194100158111110.1848835322695210.2243855228853760.209559690322002640.21438321525343965[null]
precision0.56666666666666670.204545454545454560.50.423737373737373750.465323691460055140.4
recall1.01.01.01.01.01.0
f1_score0.72340425531914890.3396226415094340.66666666666666660.5765645211650830.62169263895478270.5714285714285715
mcc0.54205826300668750.00.39223227027636810.31143017776101850.369890257638370750.31622776601683794
informedness0.51851851851851860.00.30769230769230770.27540360873694210.326210826210826250.25
markedness0.5666666666666667-0.79545454545454540.50.090404040404040420.260778236914600560.3999999999999999
csi0.56666666666666670.204545454545454560.50.423737373737373750.465323691460055140.4
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.5992063492063493

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)
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.33.01.10.1Iris-setosaIris-versicolor
84.34.79.61.8Iris-virginicaIris-virginica
94.34.79.61.8Iris-virginicaIris-virginica
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.43.21.30.2Iris-setosaIris-versicolor
164.83.11.60.2Iris-setosaIris-versicolor
174.83.41.90.2Iris-setosaIris-versicolor
184.92.54.51.7Iris-virginicaIris-versicolor
194.93.11.50.1Iris-setosaIris-versicolor
204.93.11.50.1Iris-setosaIris-versicolor
215.03.21.20.2Iris-setosaIris-versicolor
225.03.61.40.2Iris-setosaIris-versicolor
235.43.41.50.4Iris-setosaIris-versicolor
245.43.41.70.2Iris-setosaIris-versicolor
255.52.34.01.3Iris-versicolorIris-versicolor
265.63.04.51.5Iris-versicolorIris-versicolor
275.73.04.21.2Iris-versicolorIris-versicolor
285.82.64.01.2Iris-versicolorIris-versicolor
295.82.74.11.0Iris-versicolorIris-versicolor
305.82.75.11.9Iris-virginicaIris-versicolor
315.82.85.12.4Iris-virginicaIris-versicolor
325.93.04.21.5Iris-versicolorIris-versicolor
336.32.34.41.3Iris-versicolorIris-versicolor
346.42.75.31.9Iris-virginicaIris-versicolor
356.42.85.62.2Iris-virginicaIris-versicolor
366.63.04.41.4Iris-versicolorIris-versicolor
376.73.14.71.5Iris-versicolorIris-versicolor
386.93.15.12.3Iris-virginicaIris-versicolor
396.93.15.42.1Iris-virginicaIris-versicolor
407.23.05.81.6Iris-virginicaIris-versicolor
417.72.86.72.0Iris-virginicaIris-virginica
423.34.55.67.8Iris-setosaIris-setosa
434.34.79.61.8Iris-virginicaIris-virginica
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(15)
123
prediction_irissetosa
Float(22)
123
prediction_irisversicolor
Float(22)
123
prediction_irisvirginica
Float(22)
13.34.55.67.8Iris-setosaIris-setosa0.3963818147717460.2856705315422050.317947653686049
23.34.55.67.8Iris-setosaIris-setosa0.3963818147717460.2856705315422050.317947653686049
33.34.55.67.8Iris-setosaIris-setosa0.3963818147717460.2856705315422050.317947653686049
43.34.55.67.8Iris-setosaIris-setosa0.3963818147717460.2856705315422050.317947653686049
53.34.55.67.8Iris-setosaIris-setosa0.3963818147717460.2856705315422050.317947653686049
63.34.55.67.8Iris-setosaIris-setosa0.3963818147717460.2856705315422050.317947653686049
74.33.01.10.1Iris-setosaIris-versicolor0.3797345528423370.3923571746139910.227908272543672
84.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
94.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
104.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
114.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
124.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
134.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
144.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
154.43.21.30.2Iris-setosaIris-versicolor0.3812395493187940.3945114349359130.224249015745294
164.83.11.60.2Iris-setosaIris-versicolor0.3630491837898770.4163272478393060.220623568370818
174.83.41.90.2Iris-setosaIris-versicolor0.3601983439484550.4202502170889230.219551438962622
184.92.54.51.7Iris-virginicaIris-versicolor0.2433277468000570.5460511243672990.210621128832644
194.93.11.50.1Iris-setosaIris-versicolor0.3596466726015950.417129268430670.223224058967735
204.93.11.50.1Iris-setosaIris-versicolor0.3596466726015950.417129268430670.223224058967735
215.03.21.20.2Iris-setosaIris-versicolor0.3688521115575970.4074781313177430.22366975712466
225.03.61.40.2Iris-setosaIris-versicolor0.3710830369510490.405496442460180.223420520588772
235.43.41.50.4Iris-setosaIris-versicolor0.3609717422795160.42088575749750.218142500222984
245.43.41.70.2Iris-setosaIris-versicolor0.3482806299380780.431256510496980.220462859564941
255.52.34.01.3Iris-versicolorIris-versicolor0.1627488073047860.6991335299039450.138117662791268
265.63.04.51.5Iris-versicolorIris-versicolor0.133508541710980.7325178056895220.133973652599498
275.73.04.21.2Iris-versicolorIris-versicolor0.09924483627195440.8082759767180310.0924791870100147
285.82.64.01.2Iris-versicolorIris-versicolor0.09682524315402440.8170722348481620.0861025219978133
295.82.74.11.0Iris-versicolorIris-versicolor0.1002327305778280.8046330280557390.095134241366433
305.82.75.11.9Iris-virginicaIris-versicolor0.1868989889420920.5553789965804170.257722014477492
315.82.85.12.4Iris-virginicaIris-versicolor0.2308299185594650.4759557247343660.29321435670617
325.93.04.21.5Iris-versicolorIris-versicolor0.08901036084663540.8296097780960140.0813798610573502
336.32.34.41.3Iris-versicolorIris-versicolor0.1299363257998230.7270173750628420.143046299137335
346.42.75.31.9Iris-virginicaIris-versicolor0.1862424176527470.5088874180130170.304870164334236
356.42.85.62.2Iris-virginicaIris-versicolor0.1982239936034420.4229906741559910.378785332240567
366.63.04.41.4Iris-versicolorIris-versicolor0.1497557253288250.6817145081868520.168529766484323
376.73.14.71.5Iris-versicolorIris-versicolor0.1711148112787270.6090422049505810.219842983770691
386.93.15.12.3Iris-virginicaIris-versicolor0.2199003574969610.4628976629228020.317201979580237
396.93.15.42.1Iris-virginicaIris-versicolor0.2045841058110450.440627250479180.354788643709775
407.23.05.81.6Iris-virginicaIris-versicolor0.1913528008359830.4098228390023490.398824360161668
417.72.86.72.0Iris-virginicaIris-virginica0.1975157429643660.3310454702609230.471438786774711
423.34.55.67.8Iris-setosaIris-setosa0.3963818147717460.2856705315422050.317947653686049
434.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
444.34.79.61.8Iris-virginicaIris-virginica0.2321070950053530.261972057445780.505920847548867
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([[ 7, 10,  0],
       [ 0,  9,  0],
       [ 0,  8, 10]])

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([[27,  0],
       [17,  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.74090909090909, 2) + POWER("SepalWidthCm" - 3.6469696969697, 2) + POWER("PetalLengthCm" - 7.10606060606061, 2) + POWER("PetalWidthCm" - 1.94242424242424, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.33582089552239, 2) + POWER("SepalWidthCm" - 3.87910447761194, 2) + POWER("PetalLengthCm" - 3.12985074626866, 2) + POWER("PetalWidthCm" - 3.29701492537313, 2), 1 / 2) AND POWER(POWER("SepalLengthCm" - 5.74090909090909, 2) + POWER("SepalWidthCm" - 3.6469696969697, 2) + POWER("PetalLengthCm" - 7.10606060606061, 2) + POWER("PetalWidthCm" - 1.94242424242424, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 5.92439024390244, 2) + POWER("SepalWidthCm" - 2.76829268292683, 2) + POWER("PetalLengthCm" - 4.25609756097561, 2) + POWER("PetalWidthCm" - 1.32682926829268, 2), 1 / 2) THEN \'Iris-virginica\' WHEN POWER(POWER("SepalLengthCm" - 5.92439024390244, 2) + POWER("SepalWidthCm" - 2.76829268292683, 2) + POWER("PetalLengthCm" - 4.25609756097561, 2) + POWER("PetalWidthCm" - 1.32682926829268, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.33582089552239, 2) + POWER("SepalWidthCm" - 3.87910447761194, 2) + POWER("PetalLengthCm" - 3.12985074626866, 2) + POWER("PetalWidthCm" - 3.29701492537313, 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