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
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 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.99521531100478460.94265232974910410.97928921358462960.9778593950313572[null]
prc_auc1.00.98340220385674950.94882973733583480.97741064706419470.9774767247850871[null]
accuracy0.77551020408163260.65306122448979590.87755102040816320.76870748299319710.78550603915035410.7687074829931972
log_loss0.2304794893811020.1849938525017430.2032556653840360.206243002422293650.21026783963375226[null]
precision1.00.392857142857142851.00.79761904761904760.86370262390670550.6530612244897959
recall0.451.00.66666666666666660.70555555555555560.65306122448979590.6530612244897959
f1_score0.62068965517241380.56410256410256410.80.66159740642499270.67385553690972410.6530612244897959
mcc0.57118298293979310.465945558148046670.74736775322364460.59483209810382810.61227919094795870.47959183673469385
informedness0.449999999999999960.55263157894736860.66666666666666650.55643274853801170.55263157894736840.47959183673469385
markedness0.72500000000000010.39285714285714280.83783783783783770.65189832689832680.69188795209203370.47959183673469385
csi0.450.392857142857142850.66666666666666660.50317460317460320.51676384839650150.48484848484848486
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.99521531100478460.94265232974910410.97928921358462960.9778593950313572[null]
prc_auc1.00.98340220385674950.94882973733583480.97741064706419470.9774767247850871[null]
accuracy0.63265306122448980.224489795918367350.53061224489795920.46258503401360550.50354019158683890.46258503401360546
log_loss0.2304794893811020.1849938525017430.2032556653840360.206243002422293650.21026783963375226[null]
precision0.52631578947368420.224489795918367350.439024390243902440.396609991878651370.426492705489101340.3828125
recall1.01.01.01.01.01.0
f1_score0.68965517241379310.366666666666666640.61016949152542380.55549711020196120.58794872712381270.5536723163841808
mcc0.4468075912442250.00.33659562807192860.26113440643871780.30601781898324930.27243118397129207
informedness0.37931034482758630.00.258064516129032250.21245828698553950.249619758915802150.19387755102040805
markedness0.5263157894736841-0.77551020408163260.43902439024390240.063276658545317940.2020029095707340.3828125
csi0.52631578947368420.224489795918367350.439024390243902440.396609991878651370.426492705489101340.3828125
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.6615974064249927

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
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.42.91.40.2Iris-setosaIris-versicolor
204.43.21.30.2Iris-setosaIris-versicolor
214.63.11.50.2Iris-setosaIris-versicolor
224.63.61.00.2Iris-setosaIris-versicolor
234.92.43.31.0Iris-versicolorIris-versicolor
244.92.54.51.7Iris-virginicaIris-versicolor
254.93.11.50.1Iris-setosaIris-versicolor
265.03.21.20.2Iris-setosaIris-versicolor
275.13.41.50.2Iris-setosaIris-versicolor
285.13.81.90.4Iris-setosaIris-versicolor
295.33.71.50.2Iris-setosaIris-versicolor
305.43.91.70.4Iris-setosaIris-versicolor
315.52.64.41.2Iris-versicolorIris-versicolor
325.53.51.30.2Iris-setosaIris-versicolor
335.62.93.61.3Iris-versicolorIris-versicolor
345.63.04.51.5Iris-versicolorIris-versicolor
355.72.84.11.3Iris-versicolorIris-versicolor
365.82.85.12.4Iris-virginicaIris-versicolor
376.02.24.01.0Iris-versicolorIris-versicolor
386.32.95.61.8Iris-virginicaIris-versicolor
396.42.85.62.2Iris-virginicaIris-versicolor
406.52.84.61.5Iris-versicolorIris-versicolor
416.73.05.01.7Iris-versicolorIris-versicolor
426.73.14.41.4Iris-versicolorIris-versicolor
436.73.14.71.5Iris-versicolorIris-versicolor
446.83.05.52.1Iris-virginicaIris-versicolor
456.93.15.12.3Iris-virginicaIris-versicolor
467.03.24.71.4Iris-versicolorIris-versicolor
474.34.79.61.8Iris-virginicaIris-virginica
484.34.79.61.8Iris-virginicaIris-virginica
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(15)
123
prediction_irissetosa
Float(22)
123
prediction_irisversicolor
Float(22)
123
prediction_irisvirginica
Float(22)
13.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
23.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
33.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
43.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
53.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
63.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
73.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
83.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
93.34.55.67.8Iris-setosaIris-setosa0.4398152814183250.2656424382779110.294542280303764
104.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
114.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
124.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
134.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
144.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
154.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
164.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
174.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
184.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
194.42.91.40.2Iris-setosaIris-versicolor0.3302685576642260.4371628278905490.232568614445226
204.43.21.30.2Iris-setosaIris-versicolor0.3361615257117070.429978202506810.233860271781483
214.63.11.50.2Iris-setosaIris-versicolor0.3254856305825850.4436105990366230.230903770380793
224.63.61.00.2Iris-setosaIris-versicolor0.3428185806636250.4206012990843060.236580120252069
234.92.43.31.0Iris-versicolorIris-versicolor0.2392394022143760.5738418887137420.186918709071882
244.92.54.51.7Iris-virginicaIris-versicolor0.2174356642647530.5811839988626140.201380336872633
254.93.11.50.1Iris-setosaIris-versicolor0.3161474553362990.452806786467460.231045758196242
265.03.21.20.2Iris-setosaIris-versicolor0.3254197530925880.4425700644679930.232010182439419
275.13.41.50.2Iris-setosaIris-versicolor0.3177907051350420.4521907138766060.230018580988352
285.13.81.90.4Iris-setosaIris-versicolor0.3172939756079210.4560984788414080.226607545550671
295.33.71.50.2Iris-setosaIris-versicolor0.3180480566830470.4499222282137810.232029715103172
305.43.91.70.4Iris-setosaIris-versicolor0.3181337284949530.4528955655712470.2289707059338
315.52.64.41.2Iris-versicolorIris-versicolor0.1039938348294280.785021983464980.110984181705592
325.53.51.30.2Iris-setosaIris-versicolor0.3167110242876130.4510333546818920.232255621030495
335.62.93.61.3Iris-versicolorIris-versicolor0.1586199673784880.7102826386193250.131097394002188
345.63.04.51.5Iris-versicolorIris-versicolor0.1166874955152230.7595552256382630.123757278846513
355.72.84.11.3Iris-versicolorIris-versicolor0.06455644422316680.8728754299349490.0625681258418844
365.82.85.12.4Iris-virginicaIris-versicolor0.2285181157987830.5003613488740770.271120535327141
376.02.24.01.0Iris-versicolorIris-versicolor0.1288941371662440.7386154923968690.132490370436887
386.32.95.61.8Iris-virginicaIris-versicolor0.1851779159255550.4774915156099940.337330568464451
396.42.85.62.2Iris-virginicaIris-versicolor0.2041663138426830.4510509710464540.344782715110863
406.52.84.61.5Iris-versicolorIris-versicolor0.1344300196826790.7027110839359560.162858896381365
416.73.05.01.7Iris-versicolorIris-versicolor0.1772093385256330.5747419534906610.248048707983707
426.73.14.41.4Iris-versicolorIris-versicolor0.1544424238097860.6657257242028580.179831851987356
436.73.14.71.5Iris-versicolorIris-versicolor0.1630878050324870.6284441894328620.208468005534651
446.83.05.52.1Iris-virginicaIris-versicolor0.2041699265854650.4591374724316280.336692600982907
456.93.15.12.3Iris-virginicaIris-versicolor0.2200199482427740.4846404159376770.295339635819549
467.03.24.71.4Iris-versicolorIris-versicolor0.1809765653177650.5821919126099190.236831522072316
474.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
484.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
494.34.79.61.8Iris-virginicaIris-virginica0.2229692128860790.2443908640111890.532639923102732
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([[ 9, 11,  0],
       [ 0, 11,  0],
       [ 0,  6, 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.

Specific confusion matrix:

model.confusion_matrix(pos_label = "Iris-setosa", cutoff = 0.6)
Out[6]: 
array([[29,  0],
       [20,  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.58875, 2) + POWER("SepalWidthCm" - 3.76, 2) + POWER("PetalLengthCm" - 7.3975, 2) + POWER("PetalWidthCm" - 1.92, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.16282051282051, 2) + POWER("SepalWidthCm" - 3.96153846153846, 2) + POWER("PetalLengthCm" - 3.53589743589744, 2) + POWER("PetalWidthCm" - 4.02435897435898, 2), 1 / 2) AND POWER(POWER("SepalLengthCm" - 5.58875, 2) + POWER("SepalWidthCm" - 3.76, 2) + POWER("PetalLengthCm" - 7.3975, 2) + POWER("PetalWidthCm" - 1.92, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 5.8948717948718, 2) + POWER("SepalWidthCm" - 2.75384615384615, 2) + POWER("PetalLengthCm" - 4.24871794871795, 2) + POWER("PetalWidthCm" - 1.32051282051282, 2), 1 / 2) THEN \'Iris-virginica\' WHEN POWER(POWER("SepalLengthCm" - 5.8948717948718, 2) + POWER("SepalWidthCm" - 2.75384615384615, 2) + POWER("PetalLengthCm" - 4.24871794871795, 2) + POWER("PetalWidthCm" - 1.32051282051282, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.16282051282051, 2) + POWER("SepalWidthCm" - 3.96153846153846, 2) + POWER("PetalLengthCm" - 3.53589743589744, 2) + POWER("PetalWidthCm" - 4.02435897435898, 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