Loading...

verticapy.machine_learning.vertica.neighbors.KNeighborsRegressor

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

[Beta Version] Creates a KNeighborsRegressor 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 KNeighborsRegressor 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.

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.

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.

Model Initialization

First we import the KNeighborsRegressor model:

from verticapy.machine_learning.vertica import KNeighborsRegressor

Then we can create the model:

model = KNeighborsRegressor()

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.

Important

The model name is crucial for the model management system and versioning. It’s highly recommended to provide a name if you plan to reuse the model later.

Model Training

We can now fit the model:

model.fit(
    train,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density",
    ],
    "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.

Metrics

We can get the entire report using:

result = model.report()
value
explained_variance0.327634969460444
max_error2.8
median_absolute_error0.4
mean_absolute_error0.536566589684373
mean_squared_error0.490685142417244
root_mean_squared_error0.700225411853709
r20.324226399436746
r2_adj0.32108813194187
aic-910.663781877801
bic-874.640996362191
Rows: 1-10 | 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 = ["mse", "r2"]).

For KNeighborsRegressor, we can easily get the ANOVA table using:

result = model.report(metrics = "anova")
Df
SS
MS
F
p_value
Regression6343.76816012317257.294693353861994115.023842086502741.6396704849276214e-116
Residual1292643.560.4981114551083591
Total1298942.505003849115
Rows: 1-3 | Columns: 6

You can also use the KNeighborsRegressor.score function to compute the R-squared value:

model.score()
Out[4]: 0.317181344001621

Prediction

Prediction is straight-forward:

model.predict(
    test,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density"
    ],
    "prediction",
)
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
density
Float(22)
123
free_sulfur_dioxide
Numeric(9)
123
total_sulfur_dioxide
Numeric(9)
123
pH
Numeric(8)
123
sulphates
Numeric(8)
123
alcohol
Float(22)
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
prediction
Float(22)
17.20.210.371.60.0490.992423.094.03.160.4810.971white5.6
25.90.370.146.30.0360.994434.0185.03.170.639.850white5.2
35.00.30.333.70.030.988754.0173.03.360.313.071white7.0
45.50.120.331.00.0380.9916423.0131.03.250.459.850white5.2
55.60.410.227.10.050.993144.0154.03.30.410.550white5.6
67.20.30.261.50.0410.9915446.0178.03.190.5611.360white5.6
75.90.20.231.50.0370.9902138.093.03.360.4912.060white6.0
86.10.30.562.80.0440.992447.0179.03.30.5710.971white6.2
96.90.280.379.10.0370.994816.076.03.050.5411.150white5.4
106.00.10.241.10.0410.992715.065.03.610.6110.371white6.2
116.20.250.547.00.0460.9945458.0176.03.190.710.450white5.4
126.80.360.321.60.0390.994810.0124.03.30.679.650white5.8
136.50.330.382.50.0470.9896430.0148.03.170.4312.760white6.0
146.00.410.051.50.0630.993217.0120.03.210.569.260white5.2
156.00.290.4110.80.0480.993755.0149.03.090.5910.966666666666771white6.4
167.10.380.2913.60.0410.9946130.0137.03.020.9612.160white5.8
178.40.620.092.20.0840.996411.0108.03.150.669.850red5.2
186.70.240.368.40.0420.9947342.0123.03.340.5210.960white5.8
196.20.270.181.50.0280.9922820.0111.03.410.510.050white6.8
206.20.170.31.10.0370.99314.079.03.50.5410.360white6.4
216.40.460.084.90.0460.9944534.0144.03.10.5610.050white5.8
226.00.240.341.00.0360.9909752.0184.03.440.4411.4560white6.0
235.90.620.283.50.0390.990755.0152.03.440.4412.060white6.2
248.20.370.361.00.0340.990617.093.03.040.3211.781white6.6
259.90.490.583.50.0941.00049.043.03.290.589.050red6.2
265.80.220.31.10.0470.99236.0131.03.260.4510.450white5.8
278.90.430.451.90.0520.99486.016.03.350.712.560red5.8
287.70.310.364.30.0260.9915215.087.03.110.4812.050white5.6
297.10.240.4117.80.0460.999839.0145.03.320.398.750white5.8
306.60.410.2710.70.110.9967220.0103.03.080.419.060white5.4
316.80.180.249.80.0580.995264.0188.03.130.5110.660white6.4
325.70.180.361.20.0460.991999.071.03.70.6810.971white6.2
336.40.160.441.20.0510.9905839.0122.03.110.7511.371white6.8
347.00.150.2814.70.0510.9979229.0149.02.960.399.071white7.0
356.80.9150.294.80.070.9957715.039.03.530.5411.150red5.8
367.90.530.242.00.0720.99615.0105.03.270.549.460red5.2
376.70.340.431.60.0410.9901429.0114.03.230.4412.660white6.0
386.40.230.268.10.0540.995447.0181.03.120.499.450white5.0
396.20.350.218.10.0690.9990833.0158.03.150.58.860white5.4
407.20.40.6210.80.0410.997670.0189.03.080.498.640white4.8
418.30.270.392.40.0580.995516.0107.03.280.5910.350white6.4
425.90.190.211.70.0450.9934157.0135.03.320.449.550red5.8
436.60.560.228.90.0340.9967527.0133.03.20.519.150white5.0
446.90.380.3813.10.1120.9979214.094.03.020.489.250white5.6
457.70.8350.02.60.0810.99756.014.03.30.529.350red5.4
467.10.440.6211.80.0440.997552.0152.03.120.468.760white5.6
477.70.270.683.50.3580.99725.010.03.251.089.971red6.6
486.40.360.532.20.230.993419.035.03.370.9312.460red5.8
497.00.20.358.80.0370.9938831.0103.03.130.4911.060white6.0
508.00.30.285.70.0440.994831.0124.03.160.5110.260white6.0
517.50.420.311.60.080.997815.042.03.310.649.050red5.6
527.00.140.41.70.0350.991116.085.03.190.4211.860white6.2
536.80.480.252.00.0760.995329.061.03.340.610.450red6.2
547.60.230.341.60.0430.9930524.0129.03.120.710.450white4.8
557.90.490.321.90.0820.996817.0144.03.20.559.550red5.4
566.90.220.497.00.0630.995750.0168.03.540.510.360white5.8
576.40.380.268.20.0430.9923428.098.02.990.3111.460white5.2
587.60.310.2910.50.040.996621.0145.03.040.359.450white5.4
597.00.310.521.70.0290.99185.061.03.070.4310.450white5.4
606.40.310.538.80.0570.9964236.0221.03.170.449.150white5.0
616.70.170.4210.40.0380.9962885.0182.03.040.448.960white6.0
626.10.60.121.80.050.9926811.076.03.420.4810.440white5.2
637.40.310.281.60.050.992933.0137.03.310.5610.560white6.2
644.70.670.091.00.020.987225.09.03.30.3413.650white5.2
656.70.30.4418.750.0570.9995665.0224.03.110.539.150white5.0
666.90.280.411.70.050.99310.0136.03.160.7111.460white6.8
676.80.250.285.00.0350.9904842.0126.03.120.3812.671white6.0
687.50.380.2912.70.050.998625.0209.03.250.599.360white5.0
697.80.30.2916.850.0540.999823.0135.03.160.389.060white5.6
705.60.120.332.90.0440.9889621.073.03.170.3212.981white6.6
717.60.230.298.60.0530.996365.0146.03.110.329.860white5.2
725.90.360.045.70.0460.993421.087.03.220.5110.250white5.0
736.90.410.312.00.0790.9966821.051.03.470.559.560red5.8
747.20.50.182.10.0710.9976112.031.03.520.729.660red5.8
756.20.210.241.20.0510.9903631.095.03.240.5711.360white5.6
768.80.190.35.00.0280.9924234.0120.02.940.4711.250white5.8
778.50.20.41.10.0460.9919431.0106.03.00.3510.540white5.4
789.00.80.122.40.0830.998368.028.03.330.6510.460red5.4
796.60.310.281.40.0350.9883628.0107.03.00.413.260white5.6
8010.40.520.452.00.080.997746.013.03.220.7611.460red5.8
816.70.240.33.850.0420.99189105.0179.03.040.5911.381white7.6
826.30.230.31.80.0330.990616.091.03.280.411.860white5.6
8311.50.450.53.00.0781.000319.047.03.261.1111.060red5.8
846.20.560.091.70.0530.9940224.032.03.540.611.350red5.2
857.00.530.021.00.0360.99339.0107.03.20.329.050white5.2
867.00.150.2814.70.0510.9979229.0149.02.960.399.071white7.0
876.10.270.251.80.0410.99299.0109.03.080.549.050white5.2
885.10.140.250.70.0390.991915.089.03.220.439.260white5.6
897.21.00.03.00.1020.995867.016.03.430.4610.050red4.8
906.50.430.2811.250.0320.992231.087.03.020.3812.460white6.4
916.60.250.31.60.0460.99332.0134.03.420.5110.171white6.0
926.90.380.2913.650.0480.9978452.0189.03.00.69.560white6.0
938.60.6850.11.60.0920.997453.012.03.310.659.5560red5.4
947.50.170.371.50.060.993618.075.03.540.8810.750white5.4
956.10.280.278.00.0480.9949841.0162.03.210.519.950white5.2
967.30.510.2911.30.0340.9968361.0224.03.140.569.560white5.2
979.40.160.231.60.0420.994214.067.03.070.329.550white4.8
988.00.280.427.10.0450.995941.0169.03.170.4310.650white5.6
998.00.330.532.50.0910.997618.080.03.370.89.660red6.2
1005.30.320.126.60.0430.993722.0141.03.360.610.460white6.8
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.

Parameter Modification

In order to see the parameters:

model.get_params()
Out[5]: {'n_neighbors': 5, 'p': 2}

And to manually change some of the parameters:

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

Model Register

In order to register the model for tracking and versioning:

model.register("model_v1")

Please refer to /notebooks/ml/model_tracking_versioning/index.ipynb for more details on model tracking and versioning.

__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

contour([nbins, chart])

Draws the model's contour plot.

deploySQL([X, test_relation, key_columns])

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

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

predict(vdf[, X, name, inplace])

Predicts 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'.

regression_report([metrics])

Computes a regression report

report([metrics])

Computes a regression report

score([metric])

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