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

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.347390801934063
max_error2.8
median_absolute_error0.4
mean_absolute_error0.536349693251534
mean_squared_error0.490705521472393
root_mean_squared_error0.700503762639711
r20.346750425894378
r2_adj0.343728454078932
aic-914.170016124979
bic-878.11971096461
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
Regression6376.71312883435662.78552147239267127.262645104852928.843716283188727e-127
Residual1297639.880.4933538936006168
Total1303979.533742331288
Rows: 1-3 | Columns: 6

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

model.score()
Out[4]: 0.346750425894378

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)
18.10.530.222.20.0780.9967833.089.03.260.469.660red5.2
26.80.210.312.90.0460.991340.0121.03.070.6510.971white6.8
36.60.180.2617.30.0510.998417.0149.03.00.439.460white6.0
47.00.60.34.50.0680.9991420.0110.03.31.1710.250red5.0
56.60.240.388.00.0420.9957756.0187.03.210.469.250white5.0
66.70.350.488.80.0560.9962835.0167.03.040.479.450white6.4
710.40.410.553.20.0760.999622.054.03.150.899.960red5.8
86.30.430.328.80.0420.9917218.0106.03.280.3312.971white5.2
97.20.30.38.10.050.9965240.0188.03.150.499.160white5.4
108.90.840.341.40.050.995544.010.03.120.489.160red5.2
116.00.230.159.70.0480.99571101.0207.03.050.39.150white5.8
126.80.260.3413.90.0340.994939.0134.03.330.5312.060white6.4
137.60.270.332.00.0590.994419.0175.03.220.569.950white5.8
146.70.290.4514.30.0540.9986930.0181.03.140.579.150white5.8
156.30.180.361.20.0340.9907426.0111.03.160.5111.060white6.2
168.90.40.512.60.0520.99513.027.03.320.913.471red5.6
177.10.170.431.30.0230.9906733.0132.03.110.5611.760white6.0
186.30.340.296.20.0460.995229.0227.03.290.5310.160white6.8
198.10.5750.222.10.0770.996712.065.03.290.519.250red5.2
206.50.180.331.40.0290.9911435.0138.03.360.611.571white6.8
216.00.640.051.90.0660.994969.017.03.520.7810.650red5.2
226.80.8150.01.20.2670.9947116.029.03.320.519.830red4.2
237.30.180.291.00.0360.9926.0101.03.090.3711.760white6.0
248.00.230.359.20.0440.99753.0186.03.090.569.571white6.6
258.20.520.341.20.0420.9936618.0167.03.240.3910.650white5.6
267.10.260.198.20.0510.99653.0187.03.160.529.750white5.4
275.10.1650.225.70.0470.993442.0146.03.180.559.960white6.0
288.60.4850.294.10.0260.991819.0101.03.010.3812.450white6.2
297.00.220.281.50.0370.992729.0115.03.110.5510.560white5.8
307.40.380.277.50.0410.9953524.0160.03.170.4310.050white5.2
316.40.260.41.70.1790.99255.060.03.090.5410.150white6.0
329.20.270.3410.50.0430.997449.0228.03.040.4110.460white6.4
335.50.230.192.20.0440.9920939.0161.03.190.4310.460white6.0
346.150.210.373.20.0210.9907620.080.03.390.4712.050white5.6
358.20.780.02.20.0890.997813.026.03.370.469.640red4.8
366.20.370.228.30.0250.996436.0216.03.330.69.660white6.4
377.90.250.295.30.0310.991833.0117.03.060.3211.871white6.2
386.70.340.3116.40.0510.9983420.0146.03.060.549.150white5.2
396.80.370.671.50.1750.9924416.098.03.060.5610.360white5.8
407.50.250.474.10.0410.9918495.0163.02.920.5911.360white6.6
417.00.380.492.50.0970.996233.085.03.390.7711.460red6.2
426.80.250.241.60.0450.9940239.0164.03.530.5810.850white5.2
437.40.6350.12.40.080.9973616.033.03.580.6910.871red5.6
446.40.40.231.60.0660.99585.012.03.340.569.250red6.2
459.80.510.193.20.0810.99848.030.03.230.5810.560red5.6
467.30.230.272.60.0350.9913839.0120.03.040.5911.371white6.6
4710.70.220.568.20.0440.99837.0181.02.870.689.560white5.2
485.90.3950.132.40.0560.9936214.028.03.620.6712.460red6.4
496.70.210.325.40.0470.99529.0140.03.390.469.760white6.6
507.20.290.188.20.0420.9964441.0180.03.160.499.150white5.6
517.50.510.021.70.0840.9953813.031.03.360.5410.560red5.8
526.10.280.234.20.0380.9889813.095.02.970.713.160white6.6
537.00.510.092.10.0620.995844.09.03.350.5410.550red5.6
548.00.770.322.10.0790.9965616.074.03.270.59.860red5.2
558.30.390.710.60.0450.997633.0169.03.090.579.450white5.6
5612.50.560.492.40.0640.99995.027.03.080.8710.950red5.6
575.90.260.41.30.0470.994512.0139.03.450.5310.450white5.8
586.50.190.281.40.0460.9903822.090.03.180.5111.771white6.4
594.90.2350.2711.750.030.995434.0118.03.070.59.460white6.0
605.70.280.33.90.0260.9896336.0105.03.260.5812.7560white6.8
616.90.280.331.20.0390.990416.098.03.070.3911.760white6.0
626.80.190.5814.20.0380.997551.0164.03.120.489.660white5.6
637.00.230.4218.050.050.999935.0144.03.220.428.850white5.0
646.10.260.281.70.0430.9891824.098.03.140.4412.560white6.2
656.70.470.348.90.0430.996431.0172.03.220.69.250white5.6
666.80.180.371.60.0550.993447.0154.03.080.459.150white5.8
677.30.740.081.70.0940.9957610.045.03.240.59.850red5.2
686.40.570.021.80.0670.9974.011.03.460.689.550red5.4
697.10.320.3211.00.0380.993716.066.03.240.411.530white5.6
707.30.330.221.40.0410.9928740.0177.03.140.489.950white5.2
719.10.50.31.90.0650.997748.017.03.320.7110.560red5.6
726.60.70.082.60.1060.9966514.027.03.440.5810.250red5.6
7310.00.410.456.20.0710.997026.014.03.210.4911.871red7.0
747.70.320.482.30.040.991128.0114.03.20.5212.871white5.8
757.60.460.112.60.0790.996812.049.03.210.5710.050red5.8
766.70.260.295.80.0250.992926.074.03.280.5311.060white5.8
776.70.240.418.70.0360.995229.0148.03.220.629.960white5.6
785.80.240.281.40.0380.9871140.076.03.10.2913.971white6.6
797.00.240.362.80.0340.9922.0112.03.190.3812.681white6.6
807.10.270.2710.40.0410.9933526.0114.03.040.5211.571white6.0
818.00.420.322.50.080.9980126.0122.03.221.079.750red6.0
826.80.360.321.60.0390.994810.0124.03.30.679.650white5.6
834.80.130.321.20.0420.989840.098.03.420.6411.871white6.8
847.10.220.3216.90.0560.999849.0158.03.370.389.660white5.8
858.90.30.354.60.0320.9945832.0148.03.150.4511.571white6.6
866.40.260.261.10.0520.9930422.0176.03.090.549.350white6.0
8710.30.430.442.40.2140.99945.012.03.190.639.560red6.0
885.60.320.337.40.0370.9926825.095.03.250.4911.160white5.6
897.00.160.328.30.0450.995838.0126.03.210.349.250white5.8
906.90.320.137.80.0420.99611.0117.03.230.379.250white5.0
917.90.660.01.40.0960.995696.013.03.430.589.550red4.8
927.80.540.262.00.0880.998123.048.03.410.749.260red5.2
937.90.180.495.20.0510.995336.0157.03.180.4810.660white6.4
946.60.220.314.70.0450.9970450.0136.03.140.3710.660white6.0
956.00.310.384.80.040.9896841.0101.03.240.5613.160white5.8
966.40.1050.291.10.0350.9914244.0140.03.170.5510.771white6.6
975.10.350.266.80.0340.9918836.0120.03.380.411.560white6.0
987.90.20.491.60.0530.99315.0144.03.160.4710.550white5.8
996.40.290.1815.00.040.9973621.0116.03.140.59.250white4.8
1007.50.350.289.60.0510.996926.0157.03.120.539.260white5.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 Model Tracking and Versioning 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