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.80.310.0211.10.03620.0114.00.992483.750.4412.460white
23.90.2250.44.20.0329.0118.00.9893.570.3612.881white
34.20.170.361.80.02993.0161.00.989993.650.8912.071white
44.20.2150.235.10.04164.0157.00.996883.420.448.030white
54.40.320.394.30.0331.0127.00.989043.460.3612.881white
64.40.460.12.80.02431.0111.00.988163.480.3413.160white
74.40.540.095.10.03852.097.00.990223.410.412.271white
84.50.190.210.950.03389.0159.00.993323.340.428.050white
94.60.4450.01.40.05311.0178.00.994263.790.5510.250white
104.60.520.152.10.0548.065.00.99343.90.5613.140red
114.70.1450.291.00.04235.090.00.99083.760.4911.360white
124.70.3350.141.30.03669.0168.00.992123.470.4610.550white
134.70.4550.181.90.03633.0106.00.987463.210.8314.071white
144.70.60.172.30.05817.0106.00.99323.850.612.960red
154.70.670.091.00.025.09.00.987223.30.3413.650white
164.70.7850.03.40.03623.0134.00.989813.530.9213.860white
174.80.130.321.20.04240.098.00.98983.420.6411.871white
184.80.170.282.90.0322.0111.00.99023.380.3411.371white
194.80.210.2110.20.03717.0112.00.993243.660.4812.271white
204.80.2250.381.20.07447.0130.00.991323.310.410.360white
214.80.260.2310.60.03423.0111.00.992743.460.2811.571white
224.80.290.231.10.04438.0180.00.989243.280.3411.960white
234.80.330.06.50.02834.0163.00.99373.350.619.950white
244.80.340.06.50.02833.0163.00.99393.360.619.960white
254.80.650.121.10.0134.010.00.992463.320.3613.540white
264.90.2350.2711.750.0334.0118.00.99543.070.59.460white
274.90.330.311.20.01639.0150.00.987133.330.5914.081white
284.90.3350.141.30.03669.0168.00.992123.470.4610.466666666666750white
294.90.3350.141.30.03669.0168.00.992123.470.4610.466666666666750white
304.90.3450.341.00.06832.0143.00.991383.240.410.150white
314.90.3450.341.00.06832.0143.00.991383.240.410.150white
324.90.420.02.10.04816.042.00.991543.710.7414.071red
334.90.470.171.90.03560.0148.00.989643.270.3511.560white
345.00.170.561.50.02624.0115.00.99063.480.3910.871white
355.00.20.41.90.01520.098.00.98973.370.5512.0560white
365.00.2350.2711.750.0334.0118.00.99543.070.59.460white
375.00.240.195.00.04317.0101.00.994383.670.5710.050white
385.00.240.212.20.03931.0100.00.990983.690.6211.760white
395.00.240.341.10.03449.0158.00.987743.320.3213.171white
405.00.2550.222.70.04346.0153.00.992383.750.7611.360white
415.00.270.324.50.03258.0178.00.989563.450.3112.671white
425.00.270.324.50.03258.0178.00.989563.450.3112.671white
435.00.270.41.20.07642.0124.00.992043.320.4710.160white
445.00.290.545.70.03554.0155.00.989763.270.3412.981white
455.00.30.333.70.0354.0173.00.98873.360.313.071white
465.00.310.06.40.04643.0166.00.9943.30.639.960white
475.00.330.161.50.04910.097.00.99173.480.4410.760white
485.00.330.161.50.04910.097.00.99173.480.4410.760white
495.00.330.161.50.04910.097.00.99173.480.4410.760white
505.00.330.184.60.03240.0124.00.991143.180.411.060white
515.00.330.2311.80.0323.0158.00.993223.410.6411.860white
525.00.350.257.80.03124.0116.00.992413.390.411.360white
535.00.350.257.80.03124.0116.00.992413.390.411.360white
545.00.380.011.60.04826.060.00.990843.70.7514.060red
555.00.40.54.30.04629.080.00.99023.490.6613.660red
565.00.420.242.00.0619.050.00.99173.720.7414.081red
575.00.440.0418.60.03938.0128.00.99853.370.5710.260white
585.00.4550.181.90.03633.0106.00.987463.210.8314.071white
595.00.550.148.30.03235.0164.00.99183.530.5112.581white
605.00.610.121.30.00965.0100.00.98743.260.3713.550white
615.00.740.01.20.04116.046.00.992584.010.5912.560red
625.01.020.041.40.04541.085.00.99383.750.4810.540red
635.01.040.241.60.0532.096.00.99343.740.6211.550red
645.10.110.321.60.02812.090.00.990083.570.5212.260white
655.10.140.250.70.03915.089.00.99193.220.439.260white
665.10.1650.225.70.04742.0146.00.99343.180.559.960white
675.10.210.281.40.04748.0148.00.991683.50.4910.450white
685.10.230.181.00.05313.099.00.989563.220.3911.550white
695.10.250.361.30.03540.078.00.98913.230.6412.171white
705.10.260.331.10.02746.0113.00.989463.350.4311.471white
715.10.260.346.40.03426.099.00.994493.230.419.260white
725.10.290.288.30.02627.0107.00.993083.360.3711.060white
735.10.290.288.30.02627.0107.00.993083.360.3711.060white
745.10.30.32.30.04840.0150.00.989443.290.4612.260white
755.10.3050.131.750.03617.073.00.993.40.5112.333333333333350white
765.10.310.30.90.03728.0152.00.9923.540.5610.160white
775.10.330.221.60.02718.089.00.98933.510.3812.571white
785.10.330.221.60.02718.089.00.98933.510.3812.571white
795.10.330.221.60.02718.089.00.98933.510.3812.571white
805.10.330.276.70.02244.0129.00.992213.360.3911.071white
815.10.350.266.80.03436.0120.00.991883.380.411.560white
825.10.350.266.80.03436.0120.00.991883.380.411.560white
835.10.350.266.80.03436.0120.00.991883.380.411.560white
845.10.390.211.70.02715.072.00.98943.50.4512.560white
855.10.420.01.80.04418.088.00.991573.680.7313.671red
865.10.420.011.50.01725.0102.00.98943.380.3612.371white
875.10.470.021.30.03418.044.00.99213.90.6212.860red
885.10.510.182.10.04216.0101.00.99243.460.8712.971red
895.10.520.062.70.05230.079.00.99323.320.439.350white
905.10.5850.01.70.04414.086.00.992643.560.9412.971red
915.20.1550.331.60.02813.059.00.989753.30.8411.981white
925.20.1550.331.60.02813.059.00.989753.30.8411.981white
935.20.160.340.80.02926.077.00.991553.250.5110.160white
945.20.170.270.70.0311.068.00.992183.30.419.850white
955.20.1850.221.00.0347.0123.00.992183.550.4410.1560white
965.20.20.273.20.04716.093.00.992353.440.5310.171white
975.20.210.311.70.04817.061.00.989533.240.3712.071white
985.20.220.466.20.06641.0187.00.993623.190.429.7333333333333350white
995.20.240.157.10.04332.0134.00.993783.240.489.960white
1005.20.240.453.80.02721.0128.00.9923.550.4911.281white
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.73734500793324310.66689239598278340.69742530734487860.70055423708696850.7012626404400855[null]
prc_auc0.61265384057509640.50927905714017630.59312741377699640.5716867704974230.5731254666888956[null]
accuracy0.71165644171779140.66717791411042950.68558282208588960.68813905930470350.6885773834167640.6881390593047034
log_loss0.3962973275227380.2590648616979780.2605382888132250.3053001593446470.3060869822824677[null]
precision0.58291457286432160.475490196078431350.53815261044176710.53218579312817340.53343063792976410.5322085889570553
recall0.52488687782805430.468599033816425150.59821428571428570.53056673245292170.53220858895705530.5322085889570553
f1_score0.55238095238095230.47201946472019470.56659619450317130.53033220386810610.53175117675150470.5322085889570553
mcc0.34158946383959070.229050260926889650.322091326829370360.29757701719861690.29916124651870350.2983128834355828
informedness0.332311471795571740.22814959561417790.32952269692923910.296661254779662930.29828356698022350.2983128834355828
markedness0.351126493394122940.229954481792717050.314827548407027540.298636174531289160.300185434991361630.2983128834355828
csi0.38157894736842110.30891719745222930.39528023598820060.361925460269616970.363217147396302840.3625914315569488
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.73734500793324310.66689239598278340.69742530734487860.70055423708696850.7013805650621611[null]
prc_auc0.61265384057509640.50927905714017630.59312741377699640.5716867704974230.5732607891038976[null]
accuracy0.62211221122112210.52857142857142860.5848739495798320.5785191964574610.57976228139465060.5778263244128892
log_loss0.3962973275227380.2590648616979780.2605382888132250.3053001593446470.3063726946715224[null]
precision0.489473684210526340.393518518518518550.46913580246913580.45070933506606020.45214988989964620.4486442070665571
recall0.84162895927601810.82926829268292680.85585585585585590.8422510359382670.84259259259259260.8425925925925926
f1_score0.61896838602329450.5337519623233910.60606060606060610.58626031813576380.58758737674829910.5855227882037534
mcc0.33616600692637370.21476517772132250.28988659922148150.280272594623059260.28190489813390220.2789327161202738
informedness0.337732855379914150.21279770444763280.279448349153442970.276659636326996640.278240777720355050.27539056385210214
markedness0.334606427573358230.21675084175084170.300714749837556860.28402400638725230.28571082950088320.28252042856492854
csi0.448192771084337360.36402569593147750.434782608695652160.415667025237155650.41697161914510210.4139499620924943
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.3538934088340248

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
14.60.4450.994263.790.01.40.05311.0178.00.5510.250white6
24.70.4550.987463.210.181.90.03633.0106.00.8314.071white5
34.90.3350.992123.470.141.30.03669.0168.00.4610.466666666666750white6
45.00.170.99063.480.561.50.02624.0115.00.3910.871white7
55.00.240.987743.320.341.10.03449.0158.00.3213.171white7
65.10.260.989463.350.331.10.02746.0113.00.4311.471white6
75.10.330.98933.510.221.60.02718.089.00.3812.571white6
85.10.5850.992643.560.01.70.04414.086.00.9412.971red7
95.20.310.988863.560.22.40.02727.0117.00.4513.071white7
105.20.370.99023.370.331.20.02813.081.00.3811.760white6
115.30.1650.99253.320.241.10.05125.0105.00.479.150white6
125.30.20.992783.410.313.60.03622.091.00.59.860white7
135.30.260.99523.820.235.150.03448.0160.00.5110.571white6
145.30.40.990723.310.253.90.03145.0130.00.5811.7571white7
155.40.220.990923.290.356.50.02926.087.00.4412.571white7
165.40.220.991783.760.291.20.04569.0152.00.6311.071white7
175.40.230.989763.240.361.50.0374.0121.00.9912.171white5
185.40.4150.992653.540.191.60.03927.088.00.4110.071white7
195.40.420.994713.780.272.00.09223.055.00.6412.371red7
205.40.590.99443.340.077.00.04536.0147.00.579.760white5
215.40.5950.99323.360.12.80.04226.080.00.389.350white7
225.50.120.991643.250.331.00.03823.0131.00.459.850white6
235.50.140.99493.340.274.60.02922.0104.00.449.050white5
245.50.190.990263.50.270.90.0452.0103.00.3911.250white7
255.50.230.992093.190.192.20.04439.0161.00.4310.460white6
265.50.240.9943.270.328.70.0619.0102.00.3110.450white7
275.60.160.99183.280.271.40.04453.0168.00.3710.160white7
285.60.180.989843.510.581.250.03429.0129.00.612.071white6
295.60.1850.99183.550.491.10.0328.0117.00.4510.360white7
305.60.190.99183.230.391.10.04317.067.00.5310.360white6
315.60.230.994293.190.258.00.04331.0101.00.4210.460white6
325.60.240.989813.040.342.00.04114.073.00.4511.671white6
335.60.330.991263.490.281.20.03133.097.00.5810.960white7
345.60.420.989153.220.342.40.02234.097.00.3812.871white6
355.70.140.994693.320.35.40.04526.0105.00.459.350white6
365.70.150.99133.220.283.70.04557.0151.00.2711.260white6
375.70.160.99363.060.266.30.04328.0113.00.589.960white7
385.70.180.991993.70.361.20.0469.071.00.6810.971white5
395.70.220.989483.350.281.30.02726.0101.00.3812.571white6
405.70.220.990993.440.251.10.0597.0175.00.6211.160white7
415.70.240.993913.110.476.30.06935.0182.00.469.7550white6
425.70.250.991423.30.211.50.04421.0108.00.5911.060white7
435.70.250.995243.230.3212.20.04143.0127.00.5310.471white6
445.70.250.996913.080.2612.50.04952.5106.00.459.460white6
455.70.260.989953.480.31.80.03930.0105.00.5212.571white6
465.70.260.99423.270.274.10.20173.5189.50.389.460white6
475.80.170.992023.430.361.30.03611.070.00.6810.471white7
485.80.190.991073.160.334.20.03849.0133.00.4211.371white6
495.80.190.993623.770.241.30.04438.0128.00.610.650white5
505.80.20.990433.590.241.40.03365.0169.00.5612.371white5
515.80.260.99613.310.249.20.04455.0152.00.389.450white6
525.80.280.989523.320.31.50.02631.0114.00.612.571white5
535.80.290.98943.390.212.60.02512.0120.00.7914.071white7
545.80.290.993663.110.3810.70.03849.0136.00.5911.260white6
555.80.30.988712.960.231.50.03437.0121.00.3412.160white7
565.80.310.99493.190.317.50.05255.0230.00.469.850white6
575.80.320.990673.440.312.70.04925.0153.00.7312.271white7
585.90.190.98973.090.370.80.0273.021.00.3110.850white6
595.90.20.990213.360.231.50.03738.093.00.4912.060white7
605.90.220.990693.310.31.30.05242.086.00.4711.5560white6
615.90.220.992323.20.381.30.04624.090.00.4710.060white6
625.90.260.992343.630.242.40.04627.0132.00.7311.350white7
635.90.280.988363.080.391.40.03147.0147.00.6412.971white7
645.90.340.988923.40.312.00.0338.0142.00.4112.971white7
655.90.340.990163.090.33.80.03557.0135.00.3412.060white5
665.90.340.990343.020.252.00.04212.0110.00.5411.460white7
675.90.550.995123.520.12.20.06239.051.00.7611.260red6
685.90.620.99073.440.283.50.03955.0152.00.4412.060white5
696.00.160.991433.220.361.60.04213.061.00.5410.860white6
706.00.170.995363.120.299.70.04433.098.00.369.260white6
716.00.190.990333.220.291.20.04629.092.00.5311.360white6
726.00.190.99163.540.291.10.04767.0152.00.5911.171white7
736.00.210.9973.340.2412.10.0555.0164.00.399.450white7
746.00.220.993563.170.3312.20.03325.097.00.4211.371white6
756.00.230.995713.050.159.70.048101.0207.00.39.150white6
766.00.260.989443.220.293.10.04137.0144.00.3912.871white6
776.00.280.991613.280.526.20.02837.0104.00.5111.871white7
786.00.280.99293.080.251.80.0428.0108.00.559.050white6
796.00.280.998963.10.2417.80.04742.0111.00.458.960white6
806.00.320.988023.070.31.30.02518.0112.00.6413.371white7
816.00.320.989123.290.31.90.03341.0142.00.4212.871white7
826.00.320.995193.180.37.30.17446.0159.00.499.450white7
836.00.330.99243.120.270.80.18512.0188.00.629.450white6
846.00.40.99313.170.31.60.04730.0117.00.4810.160white7
856.00.410.992663.30.231.10.06622.0148.00.479.6333333333333350white7
866.00.420.995223.390.192.00.07522.047.00.7810.060red5
876.10.1050.99123.410.311.30.03755.0145.00.4111.171white7
886.10.220.989663.030.41.850.03125.0111.00.311.871white5
896.10.220.994813.210.2512.10.03554.0135.00.410.750white7
906.10.250.9913.340.31.20.03642.0107.00.5610.871white7
916.10.270.990763.320.311.50.03517.083.00.4411.171white7
926.10.280.992963.220.37.750.03133.0139.00.4611.060white5
936.10.30.99273.190.471.40.04950.0187.00.459.550white5
946.10.330.996573.390.327.80.05252.0183.00.659.550white5
956.10.340.9913.290.464.70.02921.094.00.6212.360white6
966.10.360.990313.060.268.150.03514.088.00.2713.071white7
976.10.360.99173.40.272.10.03516.0100.00.7111.571white5
986.10.360.99783.150.5815.00.04442.0115.00.519.050white5
996.10.440.99163.260.284.250.03243.0132.00.4711.371white6
1006.150.210.990763.390.373.20.02120.080.00.4712.050white5
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.60.4450.994263.790.01.40.05311.0178.00.5510.250white60.10.50.4
24.70.4550.987463.210.181.90.03633.0106.00.8314.071white50.40.30.3
34.90.3350.992123.470.141.30.03669.0168.00.4610.466666666666750white60.00.40.6
45.00.170.99063.480.561.50.02624.0115.00.3910.871white70.10.30.6
55.00.240.987743.320.341.10.03449.0158.00.3213.171white70.10.50.4
65.10.260.989463.350.331.10.02746.0113.00.4311.471white60.10.40.5
75.10.330.98933.510.221.60.02718.089.00.3812.571white60.00.30.7
85.10.5850.992643.560.01.70.04414.086.00.9412.971red70.20.30.5
95.20.310.988863.560.22.40.02727.0117.00.4513.071white70.10.20.7
105.20.370.99023.370.331.20.02813.081.00.3811.760white60.10.50.4
115.30.1650.99253.320.241.10.05125.0105.00.479.150white60.10.60.3
125.30.20.992783.410.313.60.03622.091.00.59.860white70.20.40.4
135.30.260.99523.820.235.150.03448.0160.00.5110.571white60.00.50.5
145.30.40.990723.310.253.90.03145.0130.00.5811.7571white70.20.30.5
155.40.220.990923.290.356.50.02926.087.00.4412.571white70.30.10.6
165.40.220.991783.760.291.20.04569.0152.00.6311.071white70.20.20.6
175.40.230.989763.240.361.50.0374.0121.00.9912.171white50.30.30.4
185.40.4150.992653.540.191.60.03927.088.00.4110.071white70.10.20.7
195.40.420.994713.780.272.00.09223.055.00.6412.371red70.10.30.6
205.40.590.99443.340.077.00.04536.0147.00.579.760white50.30.10.6
215.40.5950.99323.360.12.80.04226.080.00.389.350white70.50.10.4
225.50.120.991643.250.331.00.03823.0131.00.459.850white60.30.50.2
235.50.140.99493.340.274.60.02922.0104.00.449.050white50.50.40.1
245.50.190.990263.50.270.90.0452.0103.00.3911.250white70.30.30.4
255.50.230.992093.190.192.20.04439.0161.00.4310.460white60.30.50.2
265.50.240.9943.270.328.70.0619.0102.00.3110.450white70.30.20.5
275.60.160.99183.280.271.40.04453.0168.00.3710.160white70.10.40.5
285.60.180.989843.510.581.250.03429.0129.00.612.071white60.30.30.4
295.60.1850.99183.550.491.10.0328.0117.00.4510.360white70.30.30.4
305.60.190.99183.230.391.10.04317.067.00.5310.360white60.00.70.3
315.60.230.994293.190.258.00.04331.0101.00.4210.460white60.10.50.4
325.60.240.989813.040.342.00.04114.073.00.4511.671white60.30.50.2
335.60.330.991263.490.281.20.03133.097.00.5810.960white70.10.30.6
345.60.420.989153.220.342.40.02234.097.00.3812.871white60.10.50.4
355.70.140.994693.320.35.40.04526.0105.00.459.350white60.30.60.1
365.70.150.99133.220.283.70.04557.0151.00.2711.260white60.20.80.0
375.70.160.99363.060.266.30.04328.0113.00.589.960white70.10.40.5
385.70.180.991993.70.361.20.0469.071.00.6810.971white50.40.10.5
395.70.220.989483.350.281.30.02726.0101.00.3812.571white60.30.40.3
405.70.220.990993.440.251.10.0597.0175.00.6211.160white70.10.40.5
415.70.240.993913.110.476.30.06935.0182.00.469.7550white60.30.60.1
425.70.250.991423.30.211.50.04421.0108.00.5911.060white70.10.50.4
435.70.250.995243.230.3212.20.04143.0127.00.5310.471white60.20.60.2
445.70.250.996913.080.2612.50.04952.5106.00.459.460white60.20.60.2
455.70.260.989953.480.31.80.03930.0105.00.5212.571white60.10.40.5
465.70.260.99423.270.274.10.20173.5189.50.389.460white60.10.80.1
475.80.170.992023.430.361.30.03611.070.00.6810.471white70.10.30.6
485.80.190.991073.160.334.20.03849.0133.00.4211.371white60.20.20.6
495.80.190.993623.770.241.30.04438.0128.00.610.650white50.30.20.5
505.80.20.990433.590.241.40.03365.0169.00.5612.371white50.40.20.4
515.80.260.99613.310.249.20.04455.0152.00.389.450white60.20.40.4
525.80.280.989523.320.31.50.02631.0114.00.612.571white50.20.20.6
535.80.290.98943.390.212.60.02512.0120.00.7914.071white70.10.00.9
545.80.290.993663.110.3810.70.03849.0136.00.5911.260white60.40.30.3
555.80.30.988712.960.231.50.03437.0121.00.3412.160white70.40.50.1
565.80.310.99493.190.317.50.05255.0230.00.469.850white60.20.50.3
575.80.320.990673.440.312.70.04925.0153.00.7312.271white70.10.00.9
585.90.190.98973.090.370.80.0273.021.00.3110.850white60.00.60.4
595.90.20.990213.360.231.50.03738.093.00.4912.060white70.00.50.5
605.90.220.990693.310.31.30.05242.086.00.4711.5560white60.10.70.2
615.90.220.992323.20.381.30.04624.090.00.4710.060white60.30.60.1
625.90.260.992343.630.242.40.04627.0132.00.7311.350white70.40.10.5
635.90.280.988363.080.391.40.03147.0147.00.6412.971white70.30.30.4
645.90.340.988923.40.312.00.0338.0142.00.4112.971white70.00.10.9
655.90.340.990163.090.33.80.03557.0135.00.3412.060white50.50.30.2
665.90.340.990343.020.252.00.04212.0110.00.5411.460white70.20.30.5
675.90.550.995123.520.12.20.06239.051.00.7611.260red60.30.30.4
685.90.620.99073.440.283.50.03955.0152.00.4412.060white50.20.50.3
696.00.160.991433.220.361.60.04213.061.00.5410.860white60.10.50.4
706.00.170.995363.120.299.70.04433.098.00.369.260white60.20.60.2
716.00.190.990333.220.291.20.04629.092.00.5311.360white60.10.60.3
726.00.190.99163.540.291.10.04767.0152.00.5911.171white70.20.30.5
736.00.210.9973.340.2412.10.0555.0164.00.399.450white70.20.20.6
746.00.220.993563.170.3312.20.03325.097.00.4211.371white60.30.50.2
756.00.230.995713.050.159.70.048101.0207.00.39.150white60.30.60.1
766.00.260.989443.220.293.10.04137.0144.00.3912.871white60.30.30.4
776.00.280.991613.280.526.20.02837.0104.00.5111.871white70.00.20.8
786.00.280.99293.080.251.80.0428.0108.00.559.050white60.00.60.4
796.00.280.998963.10.2417.80.04742.0111.00.458.960white60.10.40.5
806.00.320.988023.070.31.30.02518.0112.00.6413.371white70.20.30.5
816.00.320.989123.290.31.90.03341.0142.00.4212.871white70.10.20.7
826.00.320.995193.180.37.30.17446.0159.00.499.450white70.40.10.5
836.00.330.99243.120.270.80.18512.0188.00.629.450white60.10.40.5
846.00.40.99313.170.31.60.04730.0117.00.4810.160white70.40.30.3
856.00.410.992663.30.231.10.06622.0148.00.479.6333333333333350white70.10.40.5
866.00.420.995223.390.192.00.07522.047.00.7810.060red50.40.10.5
876.10.1050.99123.410.311.30.03755.0145.00.4111.171white70.40.20.4
886.10.220.989663.030.41.850.03125.0111.00.311.871white50.40.40.2
896.10.220.994813.210.2512.10.03554.0135.00.410.750white70.50.10.4
906.10.250.9913.340.31.20.03642.0107.00.5610.871white70.30.20.5
916.10.270.990763.320.311.50.03517.083.00.4411.171white70.20.30.5
926.10.280.992963.220.37.750.03133.0139.00.4611.060white50.60.10.3
936.10.30.99273.190.471.40.04950.0187.00.459.550white50.50.10.4
946.10.330.996573.390.327.80.05252.0183.00.659.550white50.40.20.4
956.10.340.9913.290.464.70.02921.094.00.6212.360white60.00.40.6
966.10.360.990313.060.268.150.03514.088.00.2713.071white70.40.10.5
976.10.360.99173.40.272.10.03516.0100.00.7111.571white50.50.10.4
986.10.360.99783.150.5815.00.04442.0115.00.519.050white50.40.40.2
996.10.440.99163.260.284.250.03243.0132.00.4711.371white60.30.50.2
1006.150.210.990763.390.373.20.02120.080.00.4712.050white50.50.20.3
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([[112,  59,  50],
       [ 40, 102,  65],
       [ 45,  50, 129]])

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([[374,  11],
       [183,  38]])

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