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()
value
auc0.6612807131745315
prc_auc0.5385520120216585
accuracy0.657556270096463
log_loss0.269525468230416
precision0.6428571428571429
recall0.19313304721030042
f1_score0.297029702970297
mcc0.19736562886403478
informedness0.1288656950252105
markedness0.3022774327122155
csi0.1744186046511628
Rows: 1-11 | Columns: 2

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)
value
auc0.6612807131745315
prc_auc0.5385520120216585
accuracy0.5514469453376206
log_loss0.269525468230416
precision0.4470046082949309
recall0.8326180257510729
f1_score0.5817091454272865
mcc0.2272905313259077
informedness0.21565144477420928
markedness0.2395577997842926
csi0.41014799154334036
Rows: 1-11 | Columns: 2

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.5095468273527024

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.20.170.989993.650.361.80.02993.0161.00.8912.071white7
24.70.670.987223.30.091.00.025.09.00.3413.650white5
34.80.210.993243.660.2110.20.03717.0112.00.4812.271white7
45.00.240.987743.320.341.10.03449.0158.00.3213.171white6
55.00.350.992413.390.257.80.03124.0116.00.411.360white6
65.00.40.99023.490.54.30.04629.080.00.6613.660red7
75.10.260.989463.350.331.10.02746.0113.00.4311.471white7
85.10.30.989443.290.32.30.04840.0150.00.4612.260white6
95.10.3050.993.40.131.750.03617.073.00.5112.333333333333350white6
105.10.330.98933.510.221.60.02718.089.00.3812.571white6
115.10.350.991883.380.266.80.03436.0120.00.411.560white6
125.10.510.99243.460.182.10.04216.0101.00.8712.971red7
135.20.310.988863.560.22.40.02727.0117.00.4513.071white7
145.20.480.99273.540.041.60.05419.0106.00.6212.271red7
155.30.20.992783.410.313.60.03622.091.00.59.860white6
165.30.230.991193.160.560.90.04146.0141.00.629.750white6
175.30.30.987423.310.31.20.02925.093.00.413.671white6
185.30.310.993213.340.3810.50.03153.0140.00.4611.760white6
195.40.180.994453.420.244.80.04130.0113.00.49.460white5
205.40.220.990923.290.356.50.02926.087.00.4412.571white6
215.40.220.991783.760.291.20.04569.0152.00.6311.071white6
225.40.310.99313.290.473.00.05346.0144.00.7610.050white7
235.50.120.991643.250.331.00.03823.0131.00.459.850white5
245.50.160.98983.330.311.20.02631.068.00.4411.6560white6
255.50.160.990763.430.261.50.03235.0100.00.7712.060white6
265.50.160.99383.240.224.50.0330.0102.00.369.460white7
275.50.170.992433.280.232.90.03910.0108.00.510.050white6
285.60.130.99483.340.274.80.02822.0104.00.459.260white5
295.60.180.989843.510.581.250.03429.0129.00.612.071white6
305.60.220.988233.20.321.20.02429.097.00.4613.0571white6
315.60.260.993153.440.010.20.03813.0111.00.4612.460white7
325.60.260.994283.230.511.40.02925.093.00.4910.560white7
335.60.280.991443.210.46.10.03436.0118.00.4312.171white7
345.60.2950.993783.210.22.20.04918.0134.00.6810.050white5
355.60.350.99023.370.371.00.0386.072.00.3411.450white7
365.60.6150.99433.580.01.60.08916.059.00.529.950red5
375.60.660.993783.710.02.20.0873.011.00.6312.871red7
385.70.10.99283.270.271.30.04721.0100.00.469.550white6
395.70.1350.99463.310.34.60.04219.0101.00.429.360white6
405.70.210.990743.240.320.90.03838.0121.00.4610.660white6
415.70.220.998623.220.216.00.04441.0113.00.468.960white6
425.70.260.9943.390.2510.40.027.057.00.3710.650white7
435.70.330.99343.380.151.90.0520.093.00.629.950white7
445.70.430.9923.540.35.70.03924.098.00.6112.371white7
455.80.130.994783.360.265.10.03919.0103.00.479.360white6
465.80.190.991073.160.334.20.03849.0133.00.4211.371white6
475.80.260.990443.360.291.00.04235.0101.00.4811.471white7
485.80.280.9923.350.284.20.04452.0158.00.4410.771white7
495.80.30.988713.10.421.10.03619.0113.00.4612.671white5
505.80.30.989633.230.384.90.03922.086.00.5813.171white6
515.80.3150.997042.970.1919.40.03128.0106.00.410.5560white6
525.80.340.99493.460.167.00.03726.0116.00.4510.071white7
535.80.360.99113.340.263.30.03840.0153.00.5511.360white7
545.80.4150.99223.290.131.40.0411.064.00.5210.550white5
555.80.680.99443.540.021.80.08721.094.00.5210.050red5
565.90.150.99453.520.315.80.04153.0155.00.4610.560white7
575.90.170.99313.680.31.40.04225.0119.00.7210.560white5
585.90.210.989843.350.311.80.03345.0142.00.512.760white7
595.90.240.988893.080.281.30.03236.095.00.6412.971white7
605.90.240.992253.390.32.00.03328.092.00.6910.971white6
615.90.250.9973.240.2511.30.05230.0165.00.449.560white5
625.90.280.995423.280.148.60.03230.0142.00.449.560white5
635.90.290.995123.210.167.90.04448.0197.00.369.450white5
645.90.30.99043.190.291.10.03623.056.00.3811.350white6
655.90.320.989453.370.332.10.02735.0138.00.4212.760white7
666.00.160.99513.630.36.70.04343.0153.00.4610.650white7
676.00.170.993623.320.336.00.03630.0111.00.5810.133333333333371white7
686.00.220.988623.220.281.10.03447.090.00.3812.660white6
696.00.230.989613.360.341.30.02523.0111.00.3712.760white7
706.00.240.99383.640.271.90.04840.0170.00.5410.071white5
716.00.260.989443.220.293.10.04137.0144.00.3912.871white6
726.00.270.992153.230.323.60.03536.0133.00.4610.860white7
736.00.270.99243.280.315.00.04354.0170.00.5211.060white7
746.00.280.991263.270.274.10.04650.0147.00.5611.660white7
756.00.280.99533.190.496.80.04861.0222.00.479.350white7
766.00.280.999113.140.2919.30.05136.0174.00.59.050white5
776.00.290.98953.310.272.30.04420.0117.00.4112.771white7
786.00.290.99373.090.4110.80.04855.0149.00.5910.966666666666771white7
796.00.290.99723.330.212.60.04545.0187.00.429.550white7
806.00.310.989523.320.272.30.04219.0120.00.4112.771white7
816.00.320.989123.290.31.90.03341.0142.00.4212.871white7
826.00.320.992583.10.339.90.03222.090.00.4312.171white7
836.00.340.991083.390.323.80.04413.0116.00.4411.871white7
846.00.380.988723.180.263.50.03538.0111.00.4713.671white6
856.00.430.992223.030.347.60.04525.0118.00.3711.060white6
866.10.150.994713.60.296.20.04639.0151.00.4410.660white7
876.10.160.993653.320.346.10.03431.0114.00.5810.133333333333371white7
886.10.180.989623.160.382.30.03328.0111.00.4912.460white5
896.10.20.9913.30.171.60.04846.0129.00.4311.460white7
906.10.250.99633.140.1810.50.04941.0124.00.3510.550white6
916.10.250.997823.070.4815.80.05225.094.00.459.260white5
926.10.270.990763.320.311.50.03517.083.00.4411.171white7
936.10.270.992813.220.326.20.04847.0161.00.611.060white6
946.10.290.98933.210.271.70.02413.076.00.5112.671white6
956.10.30.98953.390.32.10.03150.0163.00.4312.771white7
966.10.370.995583.220.27.60.03149.0170.00.489.550white5
976.10.370.9973.170.4612.00.04261.0210.00.599.760white5
986.10.380.993093.240.471.40.05159.0210.00.59.650white5
996.10.430.99713.370.359.10.05983.0249.00.58.550white7
1006.10.440.99163.260.284.250.03243.0132.00.4711.371white5
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)
14.20.170.989993.650.361.80.02993.0161.00.8912.071white70.30.3
24.70.670.987223.30.091.00.025.09.00.3413.650white50.40.4
34.80.210.993243.660.2110.20.03717.0112.00.4812.271white70.30.2
45.00.240.987743.320.341.10.03449.0158.00.3213.171white60.10.3
55.00.350.992413.390.257.80.03124.0116.00.411.360white60.20.5
65.00.40.99023.490.54.30.04629.080.00.6613.660red70.10.4
75.10.260.989463.350.331.10.02746.0113.00.4311.471white70.20.4
85.10.30.989443.290.32.30.04840.0150.00.4612.260white60.20.6
95.10.3050.993.40.131.750.03617.073.00.5112.333333333333350white60.00.3
105.10.330.98933.510.221.60.02718.089.00.3812.571white60.20.4
115.10.350.991883.380.266.80.03436.0120.00.411.560white60.10.7
125.10.510.99243.460.182.10.04216.0101.00.8712.971red70.00.4
135.20.310.988863.560.22.40.02727.0117.00.4513.071white70.10.3
145.20.480.99273.540.041.60.05419.0106.00.6212.271red70.00.2
155.30.20.992783.410.313.60.03622.091.00.59.860white60.10.7
165.30.230.991193.160.560.90.04146.0141.00.629.750white60.40.3
175.30.30.987423.310.31.20.02925.093.00.413.671white60.30.3
185.30.310.993213.340.3810.50.03153.0140.00.4611.760white60.30.4
195.40.180.994453.420.244.80.04130.0113.00.49.460white50.30.5
205.40.220.990923.290.356.50.02926.087.00.4412.571white60.30.3
215.40.220.991783.760.291.20.04569.0152.00.6311.071white60.00.5
225.40.310.99313.290.473.00.05346.0144.00.7610.050white70.10.3
235.50.120.991643.250.331.00.03823.0131.00.459.850white50.40.3
245.50.160.98983.330.311.20.02631.068.00.4411.6560white60.40.5
255.50.160.990763.430.261.50.03235.0100.00.7712.060white60.30.5
265.50.160.99383.240.224.50.0330.0102.00.369.460white70.40.4
275.50.170.992433.280.232.90.03910.0108.00.510.050white60.30.4
285.60.130.99483.340.274.80.02822.0104.00.459.260white50.20.5
295.60.180.989843.510.581.250.03429.0129.00.612.071white60.10.5
305.60.220.988233.20.321.20.02429.097.00.4613.0571white60.20.5
315.60.260.993153.440.010.20.03813.0111.00.4612.460white70.20.3
325.60.260.994283.230.511.40.02925.093.00.4910.560white70.30.4
335.60.280.991443.210.46.10.03436.0118.00.4312.171white70.30.3
345.60.2950.993783.210.22.20.04918.0134.00.6810.050white50.50.2
355.60.350.99023.370.371.00.0386.072.00.3411.450white70.40.2
365.60.6150.99433.580.01.60.08916.059.00.529.950red50.50.0
375.60.660.993783.710.02.20.0873.011.00.6312.871red70.30.2
385.70.10.99283.270.271.30.04721.0100.00.469.550white60.20.8
395.70.1350.99463.310.34.60.04219.0101.00.429.360white60.20.7
405.70.210.990743.240.320.90.03838.0121.00.4610.660white60.10.7
415.70.220.998623.220.216.00.04441.0113.00.468.960white60.10.8
425.70.260.9943.390.2510.40.027.057.00.3710.650white70.20.4
435.70.330.99343.380.151.90.0520.093.00.629.950white70.20.3
445.70.430.9923.540.35.70.03924.098.00.6112.371white70.30.0
455.80.130.994783.360.265.10.03919.0103.00.479.360white60.10.6
465.80.190.991073.160.334.20.03849.0133.00.4211.371white60.20.2
475.80.260.990443.360.291.00.04235.0101.00.4811.471white70.30.3
485.80.280.9923.350.284.20.04452.0158.00.4410.771white70.30.2
495.80.30.988713.10.421.10.03619.0113.00.4612.671white50.30.3
505.80.30.989633.230.384.90.03922.086.00.5813.171white60.10.5
515.80.3150.997042.970.1919.40.03128.0106.00.410.5560white60.10.5
525.80.340.99493.460.167.00.03726.0116.00.4510.071white70.00.1
535.80.360.99113.340.263.30.03840.0153.00.5511.360white70.40.3
545.80.4150.99223.290.131.40.0411.064.00.5210.550white50.20.7
555.80.680.99443.540.021.80.08721.094.00.5210.050red50.60.3
565.90.150.99453.520.315.80.04153.0155.00.4610.560white70.40.3
575.90.170.99313.680.31.40.04225.0119.00.7210.560white50.40.0
585.90.210.989843.350.311.80.03345.0142.00.512.760white70.20.4
595.90.240.988893.080.281.30.03236.095.00.6412.971white70.40.2
605.90.240.992253.390.32.00.03328.092.00.6910.971white60.30.3
615.90.250.9973.240.2511.30.05230.0165.00.449.560white50.40.5
625.90.280.995423.280.148.60.03230.0142.00.449.560white50.40.5
635.90.290.995123.210.167.90.04448.0197.00.369.450white50.40.4
645.90.30.99043.190.291.10.03623.056.00.3811.350white60.40.4
655.90.320.989453.370.332.10.02735.0138.00.4212.760white70.10.3
666.00.160.99513.630.36.70.04343.0153.00.4610.650white70.30.2
676.00.170.993623.320.336.00.03630.0111.00.5810.133333333333371white70.20.3
686.00.220.988623.220.281.10.03447.090.00.3812.660white60.30.4
696.00.230.989613.360.341.30.02523.0111.00.3712.760white70.20.1
706.00.240.99383.640.271.90.04840.0170.00.5410.071white50.40.2
716.00.260.989443.220.293.10.04137.0144.00.3912.871white60.30.3
726.00.270.992153.230.323.60.03536.0133.00.4610.860white70.20.3
736.00.270.99243.280.315.00.04354.0170.00.5211.060white70.10.3
746.00.280.991263.270.274.10.04650.0147.00.5611.660white70.00.5
756.00.280.99533.190.496.80.04861.0222.00.479.350white70.20.3
766.00.280.999113.140.2919.30.05136.0174.00.59.050white50.40.2
776.00.290.98953.310.272.30.04420.0117.00.4112.771white70.10.2
786.00.290.99373.090.4110.80.04855.0149.00.5910.966666666666771white70.50.0
796.00.290.99723.330.212.60.04545.0187.00.429.550white70.10.3
806.00.310.989523.320.272.30.04219.0120.00.4112.771white70.20.3
816.00.320.989123.290.31.90.03341.0142.00.4212.871white70.00.3
826.00.320.992583.10.339.90.03222.090.00.4312.171white70.40.2
836.00.340.991083.390.323.80.04413.0116.00.4411.871white70.10.4
846.00.380.988723.180.263.50.03538.0111.00.4713.671white60.20.3
856.00.430.992223.030.347.60.04525.0118.00.3711.060white60.40.3
866.10.150.994713.60.296.20.04639.0151.00.4410.660white70.30.2
876.10.160.993653.320.346.10.03431.0114.00.5810.133333333333371white70.30.2
886.10.180.989623.160.382.30.03328.0111.00.4912.460white50.40.3
896.10.20.9913.30.171.60.04846.0129.00.4311.460white70.40.1
906.10.250.99633.140.1810.50.04941.0124.00.3510.550white60.30.5
916.10.250.997823.070.4815.80.05225.094.00.459.260white50.50.2
926.10.270.990763.320.311.50.03517.083.00.4411.171white70.20.2
936.10.270.992813.220.326.20.04847.0161.00.611.060white60.20.6
946.10.290.98933.210.271.70.02413.076.00.5112.671white60.30.4
956.10.30.98953.390.32.10.03150.0163.00.4312.771white70.30.1
966.10.370.995583.220.27.60.03149.0170.00.489.550white50.70.2
976.10.370.9973.170.4612.00.04261.0210.00.599.760white50.60.2
986.10.380.993093.240.471.40.05159.0210.00.59.650white50.40.2
996.10.430.99713.370.359.10.05983.0249.00.58.550white70.50.2
1006.10.440.99163.260.284.250.03243.0132.00.4711.371white50.60.2
Rows: 1-100 | Columns: 17

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([[101,  52],
       [ 63, 109]])

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([[388,  22],
       [166,  24]])

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