Loading...

verticapy.machine_learning.vertica.neighbors.KNeighborsClassifier

class verticapy.machine_learning.vertica.neighbors.KNeighborsClassifier(name: str = None, overwrite_model: bool = False, n_neighbors: int = 5, p: int = 2)

[Beta Version] Creates a KNeighborsClassifier object using the k-nearest neighbors algorithm. This object uses pure SQL to compute all the distances and final score.

Warning

This algorithm uses a CROSS JOIN during computation and is therefore computationally expensive at O(n * n), where n is the total number of elements. Since KNeighborsClassifier uses the p- distance, it is highly sensitive to unnormalized data.

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

n_neighbors: int, optional

Number of neighbors to consider when computing the score.

p: int, optional

The p of the p-distances (distance metric used during the model computation).

Attributes

Many attributes are created during the fitting phase.

n_neighbors_: int

Number of neighbors.

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

import verticapy.datasets as vpd

data = vpd.load_winequality()
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
13.90.2250.44.20.0329.0118.00.9893.570.3612.881white
24.70.3350.141.30.03669.0168.00.992123.470.4610.550white
34.70.4550.181.90.03633.0106.00.987463.210.8314.071white
44.70.7850.03.40.03623.0134.00.989813.530.9213.860white
54.90.3450.341.00.06832.0143.00.991383.240.410.150white
64.90.3450.341.00.06832.0143.00.991383.240.410.150white
74.90.420.02.10.04816.042.00.991543.710.7414.071red
85.00.270.41.20.07642.0124.00.992043.320.4710.160white
95.00.310.06.40.04643.0166.00.9943.30.639.960white
105.00.40.54.30.04629.080.00.99023.490.6613.660red
115.00.440.0418.60.03938.0128.00.99853.370.5710.260white
125.10.110.321.60.02812.090.00.990083.570.5212.260white
135.10.140.250.70.03915.089.00.99193.220.439.260white
145.10.1650.225.70.04742.0146.00.99343.180.559.960white
155.10.330.221.60.02718.089.00.98933.510.3812.571white
165.10.330.221.60.02718.089.00.98933.510.3812.571white
175.10.330.221.60.02718.089.00.98933.510.3812.571white
185.10.390.211.70.02715.072.00.98943.50.4512.560white
195.20.20.273.20.04716.093.00.992353.440.5310.171white
205.20.210.311.70.04817.061.00.989533.240.3712.071white
215.20.220.466.20.06641.0187.00.993623.190.429.7333333333333350white
225.20.310.22.40.02727.0117.00.988863.560.4513.071white
235.20.320.251.80.10313.050.00.99573.380.559.250red
245.20.340.376.20.03142.0133.00.990763.250.4112.560white
255.20.360.021.60.03124.0104.00.98963.440.3512.260white
265.20.3650.0813.50.04137.0142.00.9973.460.399.960white
275.20.480.041.60.05419.0106.00.99273.540.6212.271red
285.20.50.182.00.03623.0129.00.989493.360.7713.471white
295.30.160.391.00.02840.0101.00.991563.570.5910.660white
305.30.160.391.00.02840.0101.00.991563.570.5910.660white
315.30.1650.241.10.05125.0105.00.99253.320.479.150white
325.30.230.560.90.04146.0141.00.991193.160.629.750white
335.30.30.31.20.02925.093.00.987423.310.413.671white
345.30.330.31.20.04825.0119.00.990453.320.6211.360white
355.30.360.276.30.02840.0132.00.991863.370.411.660white
365.30.360.276.30.02840.0132.00.991863.370.411.660white
375.30.40.253.90.03145.0130.00.990723.310.5811.7571white
385.30.470.112.20.04816.089.00.991823.540.8813.671red
395.30.470.112.20.04816.089.00.991823.540.8813.566666666666771red
405.30.7150.191.50.1617.062.00.993953.620.6111.050red
415.40.220.291.20.04569.0152.00.991783.760.6311.071white
425.40.5950.12.80.04226.080.00.99323.360.389.350white
435.40.740.091.70.08916.026.00.994023.670.5611.660red
445.50.120.331.00.03823.0131.00.991643.250.459.850white
455.50.120.331.00.03823.0131.00.991643.250.459.850white
465.50.140.274.60.02922.0104.00.99493.340.449.050white
475.50.140.274.60.02922.0104.00.99493.340.449.050white
485.50.160.311.20.02631.068.00.98983.330.4411.6560white
495.50.160.311.20.02631.068.00.98983.330.4411.633333333333360white
505.50.180.225.50.03710.086.00.991563.460.4412.250white
515.50.240.451.70.04622.0113.00.992243.220.4810.050white
525.50.290.31.10.02220.0110.00.988693.340.3812.871white
535.50.310.293.00.02716.0102.00.990673.230.5611.260white
545.50.320.454.90.02825.0191.00.99223.510.4911.571white
555.50.350.351.10.04514.0167.00.9923.340.689.960white
565.50.3750.381.70.03617.098.00.991423.290.3910.560white
575.60.150.265.550.05151.0139.00.993363.470.511.060white
585.60.150.315.30.0388.079.00.99233.30.3910.560white
595.60.160.271.40.04453.0168.00.99183.280.3710.160white
605.60.1750.290.80.04320.067.00.991123.280.489.960white
615.60.1850.197.10.04836.0110.00.994383.260.419.560white
625.60.1850.197.10.04836.0110.00.994383.260.419.560white
635.60.220.321.20.02429.097.00.988233.20.4613.0571white
645.60.260.181.40.03418.0135.00.991743.320.3510.260white
655.60.260.265.70.03112.080.00.99233.250.3810.850white
665.60.260.511.40.02925.093.00.994283.230.4910.560white
675.60.280.284.20.04452.0158.00.9923.350.4410.771white
685.60.30.16.40.04334.0142.00.993823.140.489.850white
695.60.350.145.00.04648.0198.00.99373.30.7110.350white
705.60.490.134.50.03917.0116.00.99073.420.913.771white
715.60.490.134.50.03917.0116.00.99073.420.913.771white
725.60.660.02.20.0873.011.00.993783.710.6312.871red
735.60.660.02.20.0873.011.00.993783.710.6312.871red
745.70.150.4711.40.03549.0128.00.994563.030.3410.581white
755.70.180.262.20.02321.095.00.98933.070.5412.360white
765.70.180.361.20.0469.071.00.991993.70.6810.971white
775.70.20.32.50.04638.0125.00.992763.340.59.960white
785.70.210.320.90.03838.0121.00.990743.240.4610.660white
795.70.210.374.50.0458.0140.00.993323.290.6210.660white
805.70.220.216.00.04441.0113.00.998623.220.468.960white
815.70.220.216.00.04441.0113.00.998623.220.468.960white
825.70.220.216.00.04441.0113.00.998623.220.468.960white
835.70.220.216.00.04441.0113.00.998623.220.468.960white
845.70.220.216.00.04441.0113.00.998623.220.468.960white
855.70.220.293.50.0427.0146.00.989993.170.3612.160white
865.70.230.289.650.02526.0121.00.99253.280.3811.360white
875.70.250.2612.50.04952.5106.00.996913.080.459.460white
885.70.250.2612.50.04952.5120.00.996913.080.459.460white
895.70.250.2711.50.0424.0120.00.994113.330.3110.860white
905.70.260.2417.80.05923.0124.00.997733.30.510.150white
915.70.260.2417.80.05923.0124.00.997733.30.510.150white
925.70.260.2417.80.05923.0124.00.997733.30.510.150white
935.70.270.321.20.04620.0155.00.99343.80.4110.260white
945.70.280.2417.50.04460.0167.00.99893.310.449.450white
955.70.320.181.40.02926.0104.00.99063.440.3711.060white
965.70.320.384.750.03323.094.00.9913.420.4211.871white
975.70.360.344.20.02621.077.00.99073.410.4511.960white
985.80.140.156.10.04227.0123.00.993623.060.69.960white
995.80.150.321.20.03714.0119.00.991373.190.510.260white
1005.80.170.341.80.04596.0170.00.990353.380.911.881white
Rows: 1-100 | Columns: 14

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.

There are multiple classes for the “quality” column. Let us filter the data for classes between 5 and 7:

data = data[data["quality"]>=5]
data = data[data["quality"]<=7]

We can the balance the dataset to ensure equal representation:

data = data.balance(column="quality", x = 1)

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.

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 KNeighborsClassifier model:

from verticapy.machine_learning.vertica import KNeighborsClassifier

Then we can create the model:

model = KNeighborsClassifier(
   n_neighbors = 10,
   p = 2,
)

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.

Model Training

We can now fit the model:

model.fit(
    train,
    [
        "fixed_acidity",
        "volatile_acidity",
        "density",
        "pH",
    ],
    "quality",
    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()
5
6
7
avg_macro
avg_weighted
avg_micro
auc0.72181148373983770.67223784878487840.71792355371900860.70399096208124160.7036328990494615[null]
prc_auc0.60556381102988620.49625907898799650.65096737609339480.58426342203709250.583835438950684[null]
accuracy0.7233384853168470.67233384853168470.68315301391035540.69294178258629570.69233102967208060.6929417825862957
log_loss0.2473723541791210.2630882513953990.2437430817518220.251401229108780650.25148218022792374[null]
precision0.57368421052631580.51869158878504670.53086419753086420.5410799989474090.54042489114134120.5394126738794436
recall0.52657004830917880.50454545454545450.58636363636363630.53915971307275660.53941267387944360.5394126738794436
f1_score0.549118387909320.51152073732718880.55723542116630660.53929151546760510.53909406671684430.5394126738794436
mcc0.35076054301400150.265137353064097250.312424860600236660.309440918892778470.30861069460595330.3091190108191654
informedness0.34247913921826980.26332765595060660.31938471364700870.3083971696052950.30771236959761550.3091190108191655
markedness0.359242197397212860.266959487168418440.30561667277838910.310606119114673450.30962888724810.3091190108191655
csi0.37847222222222220.343653250773993830.386227544910179630.369451005968798540.36926974505489670.3693121693121693
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)
5
6
7
avg_macro
avg_weighted
avg_micro
auc0.71822684687451990.65833898688376250.71841138659320490.69832574011716240.6977992963308579[null]
prc_auc0.60035024320779870.49915739524974180.62112640088962210.57354467978238770.5728036677514173[null]
accuracy0.586030664395230.52884615384615390.558101472995090.55765943041215790.55695277133546730.5570801317233809
log_loss0.2457900249758020.2630882513953990.2491761522656730.252684809545624670.2528618316457757[null]
precision0.44772117962466490.41777777777777780.43971631205673760.435071756486393460.43474977308704250.43418940609951845
recall0.81862745098039210.85454545454545450.84931506849315060.84082932467299910.84136858475894250.8413685847589425
f1_score0.57885615251299830.56119402985074630.57943925233644860.57316314490006440.57301172308164550.5727898358920064
mcc0.277780312384476650.21950927515816710.25426769250913210.25051909335059190.249834912784960980.25016134804760454
informedness0.280768443147493940.206030603060305850.24472323175845690.243840759322085570.242920502069141730.24340420816861164
markedness0.274823983362982640.23386973180076630.26418439716312060.257626037442289850.25718789514920760.25710607276618513
csi0.40731707317073170.39004149377593360.407894736842105250.40175110126292350.401603046541144150.4013353115727003
Rows: 1-11 | Columns: 7

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

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

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,
    [
        "fixed_acidity",
        "volatile_acidity",
        "density",
        "pH",
    ],
    "prediction",
)
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
prediction
Integer
15.00.240.987743.320.341.10.03449.0158.00.3213.171white7
25.10.110.990083.570.321.60.02812.090.00.5212.260white7
35.10.5850.992643.560.01.70.04414.086.00.9412.971red7
45.20.480.99273.540.041.60.05419.0106.00.6212.271red7
55.30.230.991193.160.560.90.04146.0141.00.629.750white7
65.60.180.989843.510.581.250.03429.0129.00.612.071white6
75.60.240.989813.040.342.00.04114.073.00.4511.671white6
85.60.350.99373.30.145.00.04648.0198.00.7110.350white5
95.70.240.993913.110.476.30.06935.0182.00.469.7550white7
105.70.460.99323.130.461.40.0431.0169.00.478.850white5
115.80.310.989063.250.324.50.02428.094.00.5213.771white7
125.80.310.99163.180.331.20.03623.099.00.610.560white5
135.80.360.992123.10.51.00.12763.0178.00.459.750white5
145.80.420.9893.320.32.20.03526.0129.00.4712.960white7
155.90.230.991393.310.243.80.03861.0152.00.511.371white5
165.90.240.992253.390.32.00.03328.092.00.6910.971white7
176.00.130.99483.590.285.70.03856.0189.50.4310.671white6
186.00.220.988623.220.281.10.03447.090.00.3812.660white6
196.00.230.995713.050.159.70.048101.0207.00.39.150white6
206.00.240.99463.520.326.30.0334.0129.00.4110.450white7
216.00.290.996583.110.2115.550.04320.0142.00.5410.160white7
226.10.210.991843.220.191.40.04651.0131.00.3910.550white5
236.10.220.989663.030.41.850.03125.0111.00.311.871white6
246.10.250.99633.140.1810.50.04941.0124.00.3510.550white5
256.10.320.996333.420.252.30.07123.058.00.9710.650red5
266.20.210.99333.490.271.70.03841.0150.00.7110.571white7
276.20.320.99493.180.167.00.04530.0136.00.479.660white5
286.20.350.990053.10.293.90.04122.079.00.5912.066666666666760white5
296.20.360.989363.20.324.00.03644.092.00.513.371white7
306.20.60.99493.450.082.00.0932.044.00.5810.550red6
316.30.220.992043.240.32.00.0523.0120.00.4710.460white6
326.30.270.99263.450.380.90.0517.0140.00.510.571white6
336.30.270.996913.180.4611.10.05344.0177.00.679.450white5
346.30.410.992743.160.160.90.03225.098.00.429.550white5
356.40.150.991123.170.41.30.05361.0146.00.6811.060white7
366.40.170.99163.460.271.50.03720.098.00.4211.071white6
376.40.210.992783.150.285.90.04729.0101.00.411.060white7
386.40.240.99423.010.495.80.05325.0120.00.9810.560white5
396.40.280.993543.10.561.70.15649.0106.00.379.260white5
406.40.450.99052.970.071.10.0310.0131.00.2810.850white5
416.40.570.995193.470.122.30.1225.036.00.7111.371red6
426.50.160.9923.180.334.80.04345.0114.00.4411.260white7
436.50.230.993.190.391.90.03641.098.00.4311.971white6
446.50.250.997763.20.2717.40.06429.0140.00.4910.160white6
456.50.280.991113.280.343.60.0429.0121.00.4812.171white7
466.50.280.99123.320.292.70.03826.0107.00.4111.671white7
476.50.320.99433.030.238.50.05120.0138.00.4210.750white5
486.50.350.996923.180.3110.20.06958.0170.00.499.450white5
496.50.360.99023.10.492.90.0316.094.00.4912.171white5
506.60.20.994963.110.2710.90.03829.0130.00.4410.571white6
516.60.210.992942.960.58.70.03641.0191.00.5611.060white6
526.60.220.990093.150.371.60.0431.0101.00.6612.050white7
536.60.220.990193.170.351.40.0523.083.00.4812.071white7
546.60.220.999063.080.2317.30.04737.0118.00.468.860white6
556.60.230.990843.290.321.70.02426.0102.00.611.860white6
566.60.230.997563.330.2914.450.05729.0144.00.5410.260white6
576.60.240.993813.130.311.30.02611.077.00.5512.871white6
586.60.290.99133.150.396.750.03122.098.00.812.971white5
596.60.30.99563.180.458.00.03854.0200.00.489.550white5
606.60.320.991983.40.264.60.03126.0120.00.7312.571white7
616.60.440.994443.420.092.20.0639.018.00.6911.360red6
626.60.640.990363.110.284.40.03219.078.00.6212.960white5
636.70.160.996662.880.3212.50.03518.0156.00.369.060white6
646.70.210.99533.120.429.10.04931.0150.00.749.971white7
656.70.280.990643.260.282.40.01236.0100.00.3911.771red7
666.70.370.99533.160.416.30.06122.0149.00.479.660white5
676.70.450.991223.120.35.30.03627.0165.00.4612.260white6
686.70.480.98893.150.321.40.02122.0121.00.5312.771white5
696.80.160.99233.490.42.30.03718.0102.00.4211.471white6
706.80.160.995183.20.2910.40.04659.0143.00.410.860white6
716.80.180.994983.420.327.20.04717.0109.00.4410.460white6
726.80.211.03.270.3618.10.04632.0133.00.488.850white5
736.80.220.99183.240.316.30.03533.0170.00.6612.660white6
746.80.230.99883.180.3916.10.05371.0194.00.6410.260white6
756.80.241.000553.00.4919.30.05755.0247.00.568.750white6
766.80.250.994023.530.241.60.04539.0164.00.5810.850white6
776.80.320.990613.330.33.30.02915.080.00.6312.671white7
786.80.330.992263.060.317.40.04534.0143.00.5512.260white5
796.80.360.99283.360.321.80.0674.08.00.5512.871red7
806.80.390.992123.180.347.40.0238.0133.00.4412.071white5
816.90.190.993153.210.351.70.03633.0101.00.5410.871white6
826.90.250.99483.160.265.20.02436.0135.00.7210.771white6
836.90.280.988822.980.242.10.03449.0121.00.4313.271white5
846.90.340.991653.360.34.70.02934.0148.00.4912.371white7
857.00.20.99283.190.345.70.03532.083.00.4611.560white6
867.00.230.99123.160.351.40.03631.0113.00.4810.871white7
877.00.230.993723.180.282.70.05316.092.00.569.350white5
887.00.240.989883.160.351.00.03242.0104.00.3711.771white6
897.00.240.993.060.341.40.03127.0107.00.3911.960white7
907.00.340.99373.010.13.50.04417.063.00.399.250white5
917.00.490.99743.340.495.60.0626.0121.00.7610.550red5
927.00.550.99593.360.132.20.07515.035.00.599.760red5
937.10.220.98993.150.332.80.03348.0153.00.3812.771white7
947.10.340.993.30.491.50.02726.0126.00.3312.271white7
957.10.350.98973.260.273.10.03428.0134.00.3813.171white7
967.10.3650.99413.150.141.20.05524.084.00.438.950white5
977.10.460.996243.360.142.80.07615.037.00.4910.750red5
987.20.180.99193.140.411.20.04841.097.00.4510.450white7
997.20.230.991663.180.391.50.05326.0106.00.4711.160white6
1007.20.240.99583.170.197.70.04553.0176.00.389.550white6
Rows: 1-100 | Columns: 15

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,
    [
        "fixed_acidity",
        "volatile_acidity",
        "density",
        "pH",
    ],
    "prediction",
)
123
fixed_acidity
Numeric(8)
123
volatile_acidity
Numeric(9)
123
density
Float(22)
123
pH
Numeric(8)
123
citric_acid
Numeric(8)
123
residual_sugar
Numeric(9)
123
chlorides
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
prediction
Integer
123
prediction_5
Float(22)
123
prediction_6
Float(22)
123
prediction_7
Float(22)
14.20.170.989993.650.361.80.02993.0161.00.8912.071white70.40.20.4
24.70.1450.99083.760.291.00.04235.090.00.4911.360white70.10.30.6
35.00.2550.992383.750.222.70.04346.0153.00.7611.360white70.00.30.7
45.20.480.99273.540.041.60.05419.0106.00.6212.271red70.00.10.9
55.40.150.988783.040.322.50.03710.051.00.5812.660white60.40.30.3
65.60.180.99543.490.310.20.02828.0131.00.4210.871white70.10.50.4
75.60.490.99073.420.134.50.03917.0116.00.913.771white70.40.30.3
85.60.540.99423.720.041.70.0495.013.00.5811.450red50.40.20.4
95.80.190.99353.340.494.90.0444.0118.00.389.571white70.20.20.6
105.80.190.993623.770.241.30.04438.0128.00.610.650white50.50.00.5
115.80.310.989063.250.324.50.02428.094.00.5213.771white70.00.40.6
125.80.350.99123.350.293.20.03441.0151.00.5811.633333333333371white70.10.20.7
135.80.360.992123.10.51.00.12763.0178.00.459.750white50.40.20.4
145.90.190.98973.090.370.80.0273.021.00.3110.850white70.20.20.6
155.90.190.993413.320.211.70.04557.0135.00.449.550white70.40.10.5
165.90.20.994263.310.2812.80.03829.0132.00.5711.871white50.50.10.4
175.90.320.989643.220.284.70.03934.094.00.5713.171white50.60.20.2
185.90.340.988923.40.312.00.0338.0142.00.4112.971white70.10.30.6
195.90.420.991843.250.362.40.03419.077.00.4810.950white50.60.20.2
206.00.130.99483.590.285.70.03856.0189.50.4310.671white60.10.40.5
216.00.220.988623.220.281.10.03447.090.00.3812.660white60.20.50.3
226.00.250.993983.160.45.70.05256.0152.00.8810.560white70.30.30.4
236.00.340.99513.250.245.40.0623.0126.00.449.071white70.20.40.4
246.10.320.996333.420.252.30.07123.058.00.9710.650red70.20.20.6
256.10.350.99343.230.071.40.06922.0108.00.529.250white50.60.20.2
266.20.150.9933.380.461.60.03938.0123.00.519.760white70.10.40.5
276.20.70.996223.540.155.10.07613.027.00.611.960red60.60.40.0
286.30.220.989983.140.282.40.04238.0102.00.3711.671white60.30.60.1
296.30.250.99683.180.4411.60.04148.0195.00.529.550white60.40.30.3
306.30.270.99363.280.232.90.04713.0100.00.439.850white50.30.60.1
316.30.30.99593.440.481.80.06918.061.00.7810.360red70.00.40.6
326.40.220.989583.180.341.40.02356.0115.00.711.760white60.10.50.4
336.50.080.9913.340.331.90.02823.093.00.712.071white70.10.10.8
346.50.240.989283.250.281.10.03426.083.00.3312.360white70.20.30.5
356.50.250.997763.20.2717.40.06429.0140.00.4910.160white60.30.40.3
366.50.250.9983.580.3512.00.05547.0179.00.4710.050white70.30.30.4
376.50.280.990743.170.254.80.02954.0128.00.4412.271white60.30.60.1
386.50.350.996923.180.3110.20.06958.0170.00.499.450white50.40.30.3
396.60.20.994963.110.2710.90.03829.0130.00.4410.571white60.00.50.5
406.60.220.999063.080.2317.30.04737.0118.00.468.860white60.20.60.2
416.60.230.990843.290.321.70.02426.0102.00.611.860white60.30.50.2
426.60.240.993813.130.311.30.02611.077.00.5512.871white60.30.40.3
436.60.260.99813.110.5615.40.05332.0141.00.499.350white50.40.40.2
446.60.560.993973.420.142.40.06413.029.00.6211.771red60.40.40.2
456.60.640.990363.110.284.40.03219.078.00.6212.960white50.60.40.0
466.70.190.989123.310.391.00.03214.071.00.3813.071white60.00.60.4
476.70.190.991732.90.323.70.04126.076.00.5710.571white60.00.60.4
486.70.210.989493.240.341.50.03545.0123.00.3612.671white70.00.50.5
496.70.240.998023.420.2612.60.05344.0182.00.429.750white70.30.30.4
506.70.30.99743.040.512.10.04538.0127.00.538.960white60.20.40.4
516.70.310.988673.090.32.40.03830.083.00.3612.871white60.30.50.2
526.70.340.99823.190.315.60.05451.0196.00.499.350white50.60.20.2
536.70.350.991883.130.329.00.03229.0113.00.6512.971white70.20.40.4
546.80.170.9933.00.175.10.04926.082.00.389.860white70.00.30.7
556.80.211.03.270.3618.10.04632.0133.00.488.850white60.40.40.2
566.80.220.99183.240.316.30.03533.0170.00.6612.660white60.40.60.0
576.80.220.99843.440.313.60.05550.0180.00.399.850white60.10.60.3
586.80.230.99883.180.3916.10.05371.0194.00.6410.260white60.40.50.1
596.80.30.99123.090.352.80.03810.0164.00.5312.060white60.50.40.1
606.80.360.99283.360.321.80.0674.08.00.5512.871red70.20.10.7
616.80.40.99043.120.292.80.04427.097.00.4211.260white70.40.20.4
626.80.590.99623.410.066.00.0611.018.00.5910.871red60.30.50.2
636.80.690.99973.460.05.60.12421.058.00.7210.250red60.20.60.2
646.90.180.99343.270.361.30.03640.0117.00.959.571white70.10.30.6
656.90.20.9923.380.376.20.02724.097.00.4912.271white50.40.40.2
666.90.250.99483.160.265.20.02436.0135.00.7210.771white60.30.60.1
676.90.330.992263.110.317.70.0429.0135.00.5712.350white50.60.30.1
686.90.380.99323.380.328.50.04436.0152.00.3512.071white70.20.30.5
696.90.430.995943.170.289.40.05629.0183.00.439.450white50.50.20.3
706.90.520.996853.460.252.60.08110.037.00.511.050red50.40.30.3
717.00.140.99563.220.329.00.03954.0141.00.439.460white60.20.50.3
727.00.160.99363.080.731.00.13858.0150.00.39.250white70.10.50.4
737.00.160.9982.910.2514.30.04427.0149.00.469.260white70.10.30.6
747.00.240.994363.20.36.70.03937.0125.00.399.950white70.40.40.2
757.00.360.995583.40.212.30.08620.065.00.5410.160red70.30.20.5
767.00.490.99743.340.495.60.0626.0121.00.7610.550red50.50.50.0
777.10.170.99583.350.387.40.05249.0182.00.529.660white60.20.40.4
787.10.260.99863.070.3414.40.06735.0189.00.539.171white70.20.10.7
797.10.280.990693.150.311.50.05320.098.00.511.450white50.60.20.2
807.10.350.98973.260.273.10.03428.0134.00.3813.171white70.20.20.6
817.10.440.98963.070.372.70.04135.0128.00.4313.571white70.40.10.5
827.10.750.992423.390.012.20.05911.018.00.412.860red50.60.30.1
837.20.160.989583.120.291.00.03140.0123.00.412.171white50.30.20.5
847.20.160.99223.270.320.80.0450.0121.00.3310.060white50.30.30.4
857.20.180.99253.320.311.10.04520.073.00.410.871white50.30.50.2
867.20.220.991963.250.241.40.04117.0159.00.5311.260white50.60.10.3
877.20.230.99792.980.3914.20.05849.0192.00.489.071white60.00.40.6
887.20.240.994113.360.293.00.03617.0117.00.6810.160white60.20.50.3
897.20.240.99583.170.197.70.04553.0176.00.389.550white60.30.50.2
907.20.250.99992.970.3918.950.03842.0155.00.479.060white70.10.30.6
917.20.290.99773.080.413.60.04566.0231.00.599.660white70.30.40.3
927.20.320.995683.20.48.70.03845.0154.00.4710.460white50.60.20.2
937.20.370.99033.30.152.00.02927.087.00.5912.671white70.10.10.8
947.20.610.996413.250.084.00.08226.0108.00.519.450red60.50.40.1
957.30.250.99113.290.411.80.03752.0165.00.3912.271white70.20.30.5
967.30.250.99863.040.3613.10.0535.0200.00.468.971white70.00.40.6
977.30.261.03.060.3317.850.04941.5195.00.449.171white70.00.50.5
987.30.40.99693.410.31.70.0833.079.00.659.560red50.70.10.2
997.30.450.99783.330.365.90.07412.087.00.8310.550red50.50.20.3
1007.40.130.9953.360.394.70.04236.0137.00.5610.371white70.10.30.6
Rows: 1-100 | Columns: 18

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 of your choice by specifying the desired cutoff.

model.confusion_matrix(cutoff = 0.5)
Out[5]: 
array([[108,  49,  50],
       [ 47, 113,  60],
       [ 34,  52, 134]])

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 = "5", cutoff = 0.6)
Out[6]: 
array([[375,   9],
       [178,  27]])

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 = "5")

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 = "5")

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]: {'n_neighbors': 10, 'p': 2}

And to manually change some of the parameters:

model.set_params({'n_neighbors': 8})

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

It is not possible to export this type of model, but you can still examine the SQL code generated by using the deploySQL() method.

__init__(name: str = None, overwrite_model: bool = False, n_neighbors: int = 5, p: int = 2) → None

Must be overridden in the child class

Methods

__init__([name, overwrite_model, n_neighbors, 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, test_relation, predict, ...])

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

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