Loading...

vastorbit.machine_learning.vast.neighbors.KNeighborsRegressor

class vastorbit.machine_learning.vast.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 VAST 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).

Variables:
  • created (Many attributes are)

  • phase. (during the fitting)

  • 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 vastorbit:

import vastorbit as vo

Hint

By assigning an alias to vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit 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 vastorbit are used as intended without interfering with functions from other libraries.

For this example, we will use the winequality dataset.

import vastorbit.datasets as vod

data = vod.load_winequality()
123
fixed_acidity
Decimal(6, 3)
123
volatile_acidity
Decimal(7, 4)
123
citric_acid
Decimal(6, 3)
123
residual_sugar
Decimal(7, 3)
123
chlorides
Double
123
free_sulfur_dioxide
Decimal(7, 2)
123
total_sulfur_dioxide
Decimal(7, 2)
123
density
Double
123
ph
Decimal(6, 3)
123
sulphates
Decimal(6, 3)
123
alcohol
Double
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
16.30.670.4812.60.05257.0222.00.99793.170.529.360white
27.40.40.295.40.04431.0122.00.9943.30.511.181white
37.10.260.312.20.04429.0128.00.99373.340.6410.981white
49.00.310.486.60.04311.073.00.99382.90.3811.650white
56.30.390.246.90.0699.0117.00.99423.150.3510.240white
68.20.220.366.80.03412.090.00.99443.010.3810.581white
77.10.190.283.60.03316.078.00.9932.910.7811.460white
87.30.250.3613.10.0535.0200.00.99863.040.468.971white
97.90.20.341.20.0429.0118.00.99323.140.4110.660white
107.10.260.325.90.03739.097.00.99343.310.411.660white
117.00.20.345.70.03532.083.00.99283.190.4611.560white
126.90.30.334.10.03526.0155.00.99253.250.7912.381white
138.10.290.497.10.04222.0124.00.99443.140.4110.860white
145.80.170.31.40.03755.0130.00.99093.290.3811.360white
155.90.4150.020.80.03822.063.00.99323.360.369.350white
166.60.230.261.30.04516.0128.00.99343.360.610.060white
178.60.550.3515.550.05735.5366.51.00013.040.6311.030white
186.90.350.741.00.04418.0132.00.9923.130.5510.250white
197.60.140.741.60.0427.0103.00.99163.070.410.871white
209.20.280.4911.80.04229.0137.00.9983.10.3410.140white
216.20.180.494.50.04717.090.00.99193.270.3711.660white
225.30.1650.241.10.05125.0105.00.99253.320.479.150white
239.80.250.7410.00.05636.0225.00.99773.060.4310.040white
248.10.290.497.10.04222.0124.00.99443.140.4110.860white
256.80.220.490.90.05226.0128.00.9913.250.3511.460white
267.20.220.491.00.04534.0140.00.993.050.3412.760white
277.40.250.491.10.04235.0156.00.99173.130.5511.350white
288.20.180.491.10.03328.081.00.99233.00.6810.471white
296.10.220.491.50.05118.087.00.99283.30.469.650white
307.00.390.241.00.0488.0119.00.99233.00.3110.140white
316.10.220.491.50.05118.087.00.99283.30.469.650white
326.50.360.492.90.0316.094.00.99023.10.4912.171white
337.10.290.491.20.03132.099.00.98933.070.3312.260white
347.40.250.491.10.04235.0156.00.99173.130.5511.350white
356.90.230.2414.20.05319.094.00.99823.170.59.650white
368.50.560.7417.850.05151.0243.01.00052.990.79.250white
378.20.180.491.10.03328.081.00.99233.00.6810.471white
386.30.230.497.10.0567.0210.00.99513.230.349.550white
396.10.250.497.60.05267.0226.00.99563.160.478.950white
407.20.260.7413.60.0556.0162.00.9983.030.448.850white
417.20.310.241.40.05717.0117.00.99283.160.3510.550white
428.00.250.491.20.06127.0117.00.99383.080.349.450white
437.00.180.495.30.0434.0125.00.99143.240.412.260white
447.80.430.4913.00.03337.0158.00.99553.140.3511.360white
458.30.20.744.450.04433.0130.00.99243.250.4212.260white
466.30.270.491.20.06335.092.00.99113.380.4212.260white
477.40.160.491.20.05518.0150.00.99173.230.4711.260white
487.40.160.491.20.05518.0150.00.99173.230.4711.260white
496.90.190.496.60.03649.0172.00.99323.20.2711.560white
507.80.430.4913.00.03337.0158.00.99553.140.3511.360white
517.20.40.491.10.04811.0138.00.99293.010.429.350white
527.80.430.4913.00.03337.0158.00.99553.140.3511.360white
537.60.520.4914.00.03437.0156.00.99583.140.3811.871white
548.30.210.4919.80.05450.0231.01.00122.990.549.250white
556.90.340.7411.20.06944.0150.00.99683.00.819.250white
566.30.270.491.20.06335.092.00.99113.380.4212.260white
578.30.20.744.450.04433.0130.00.99243.250.4212.260white
587.10.220.742.70.04442.0144.00.9913.310.4112.260white
597.90.110.494.50.04827.0133.00.99463.240.4210.660white
608.50.170.743.60.0529.0128.00.99283.280.412.460white
616.40.1450.495.40.04854.0164.00.99463.560.4410.860white
627.40.160.491.20.05518.0150.00.99173.230.4711.260white
638.30.190.491.20.05111.0137.00.99183.060.4611.060white
648.00.440.499.10.03146.0151.00.99263.160.2712.781white
657.00.20.740.80.04419.0163.00.99313.460.5310.250white
666.90.190.496.60.03649.0172.00.99323.20.2711.560white
677.10.250.493.00.0330.096.00.99033.130.3912.371white
686.50.240.241.60.04615.060.00.99283.190.399.850white
697.20.40.491.10.04811.0138.00.99293.010.429.350white
707.60.520.4914.00.03437.0156.00.99583.140.3811.871white
717.80.430.4913.00.03337.0158.00.99553.140.3511.360white
727.80.210.491.350.0526.048.00.99113.150.2811.450white
737.00.20.495.90.03839.0128.00.99383.210.4810.860white
746.90.250.243.60.05713.085.00.99422.990.489.540white
757.20.080.491.30.0518.0148.00.99453.460.4410.260white
767.10.850.498.70.02840.0184.00.99623.220.3610.750white
777.60.510.241.20.0410.0104.00.9923.050.2910.860white
787.90.220.244.60.04439.0159.00.99272.990.2811.560white
797.70.160.492.00.05620.0124.00.99483.320.4910.760white
807.20.080.491.30.0518.0148.00.99453.460.4410.260white
816.60.250.241.70.04826.0124.00.99423.370.610.160white
826.70.160.492.40.04657.0187.00.99523.620.8110.460white
836.90.250.243.60.05713.085.00.99422.990.489.540white
847.50.320.244.60.0538.0134.00.99583.140.59.130white
857.40.280.491.50.03420.0126.00.99182.980.3910.660white
866.20.150.490.90.03317.051.00.99323.30.79.460white
876.70.250.7419.40.05444.0169.01.00043.510.459.860white
886.50.260.7413.30.04468.0224.00.99723.180.549.560white
897.90.160.7417.850.03752.0187.00.99982.990.419.350white
905.60.1850.491.10.0328.0117.00.99183.550.4510.360white
917.50.20.491.30.0318.097.00.99183.060.6211.150white
928.00.30.499.40.04647.0188.00.99643.140.4810.050white
938.00.340.499.00.03339.0180.00.99363.130.3812.381white
947.70.350.498.650.03342.0186.00.99313.140.3812.481white
957.60.290.499.60.0345.0197.00.99383.130.3812.371white
966.70.620.241.10.0396.062.00.99343.410.3210.450white
976.80.270.491.20.04435.0126.00.993.130.4812.171white
987.70.270.491.80.04123.086.00.99143.160.4212.560white
996.70.510.242.10.04314.0155.00.99043.220.613.060white
1007.40.190.499.30.0326.0132.00.9942.990.3211.071white
Rows: 1-100 | Columns: 14

Note

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

You can easily divide your dataset into training and testing subsets using the VastFrame.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, vastorbit 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 VastFrame.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 vastorbit.machine_learning.vast import KNeighborsRegressor

Then we can create the model:

model = KNeighborsRegressor()

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 VastFrame 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 vastorbit, 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_variance9.992007221626409e-16
max_error3.8
median_absolute_error0.7999999999999998
mean_absolute_error0.8470948012232363
mean_squared_error1.4249847094801225
root_mean_squared_error1.0702824800752226
r2-0.5191985265390417
r2_adj-0.5262048225876461
aic477.40423564508205
bic513.4764789076636
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")
DfSSMSFp_value
Regression654.176177370030879.02936289500514511.1360544577598263.808709808621569e-12
Residual13011054.88000000000080.8108224442736363
Total1307992.9701834862386
Rows: 1-3 | Columns: 6

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

model.score()

Prediction

Prediction is straight-forward:

model.predict(
    test,
    [
        "fixed_acidity",
        "volatile_acidity",
        "citric_acid",
        "residual_sugar",
        "chlorides",
        "density"
    ],
    "prediction",
)
123
fixed_acidity
Decimal(6, 3)
123
volatile_acidity
Decimal(7, 4)
123
citric_acid
Decimal(6, 3)
123
residual_sugar
Decimal(7, 3)
123
chlorides
Double
123
density
Double
123
free_sulfur_dioxide
Decimal(7, 2)
123
total_sulfur_dioxide
Decimal(7, 2)
123
ph
Decimal(6, 3)
123
sulphates
Decimal(6, 3)
123
alcohol
Double
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
seedrand
Decimal(26, 6)
123
prediction
Double
16.00.260.52.20.0480.992859.0153.03.080.619.850white0.095.8
27.50.230.6811.00.0470.997837.0133.02.990.388.850white0.115.8
37.40.21.662.10.0220.9916534.0113.03.260.5512.260white0.125.8
47.30.210.291.60.0340.991729.0118.03.30.511.081white0.195.8
56.70.640.232.10.080.9953811.0119.03.360.710.950red0.095.8
65.10.420.01.80.0440.9915718.088.03.680.7313.671red0.015.8
77.70.30.321.60.0370.991923.0124.02.930.3311.060white0.145.8
87.70.320.6210.60.0360.997856.0153.03.130.448.960white0.05.8
96.70.1050.3212.40.0510.99834.0106.03.540.459.260white0.035.8
107.60.210.441.90.0360.991310.0119.03.010.712.860white0.035.8
118.90.330.321.50.0470.995411.0200.03.190.469.450white0.015.8
126.50.190.321.40.040.992231.0132.03.360.5410.871white0.165.8
137.50.590.221.80.08199999999999990.9949943.060.03.10.429.250red0.195.8
147.00.60.34.50.0680.9991420.0110.03.31.1710.250red0.05.8
157.00.620.11.40.0710.99627.063.03.280.619.250red0.15.8
165.40.740.01.20.04099999999999990.9925816.046.04.010.5912.560red0.195.8
177.50.270.7911.950.040.9983951.0159.02.980.448.750white0.15.8
186.80.180.312.80.0620.9980819.0171.03.00.529.071white0.185.8
195.40.50.135.00.0280.9907912.0107.03.480.8813.571white0.195.8
207.20.190.2711.20.0610.9977246.0149.02.990.599.360white0.015.8
216.80.30.226.20.060.9985841.0190.03.180.519.250white0.05.8
226.50.170.311.50.0410.9909234.0121.03.060.4610.560white0.075.8
236.20.210.2613.10.050.9977259.0150.03.310.469.050white0.135.8
247.90.60.061.60.0690.996415.059.03.30.469.450red0.085.8
257.30.650.01.20.0650.994615.021.03.390.4710.071red0.175.8
265.80.270.214.950.0440.996222.0179.03.370.3710.250white0.175.8
276.50.170.548.50.0820.995964.0163.02.890.398.860white0.145.8
286.90.230.47.50.040.992750.0151.03.110.2711.460white0.135.8
295.80.250.2613.10.0510.997244.0148.03.290.389.350white0.115.8
307.20.430.246.70.0580.99540.0163.03.20.419.950white0.185.8
316.40.240.3214.90.0470.996854.0162.03.280.510.260white0.135.8
326.10.250.497.60.0520.995667.0226.03.160.478.950white0.195.8
337.30.220.499.40.0340.993929.0134.02.990.3211.071white0.075.8
346.40.180.7411.90.0460.997854.0168.03.580.6810.150white0.195.8
356.90.260.491.60.0580.996539.0166.03.650.529.440white0.115.8
367.00.350.36.50.0280.993627.087.03.40.4211.471white0.055.8
377.10.160.251.30.0340.991528.0123.03.270.5511.460white0.175.8
3810.00.910.421.60.0560.996834.0181.03.110.4610.040white0.125.8
397.00.230.421.10.0620.9931835.0100.03.040.49.250white0.055.8
406.40.140.287.90.0570.9942521.082.03.260.3610.060white0.195.8
416.80.30.2620.30.0370.9972745.0150.03.040.3812.360white0.25.8
426.80.290.321.80.0320.9909518.0130.03.050.6211.260white0.25.8
436.00.190.291.20.0460.9903329.092.03.220.5311.360white0.15.8
446.20.360.225.250.0380.9918444.0145.03.220.411.260white0.15.8
4512.00.280.491.90.0740.997610.021.02.980.669.971red0.055.8
466.40.570.021.80.0670.9974.011.03.460.689.550red0.185.8
477.00.450.342.70.08199999999999990.99816.072.03.550.69.550red0.055.8
4811.10.420.472.650.0850.997369.034.03.240.7712.171red0.145.8
498.20.260.342.50.0730.9959416.047.03.40.7811.371red0.195.8
506.80.490.222.30.0710.9943813.024.03.410.8311.360red0.055.8
517.20.60.042.50.0760.9974518.088.03.530.559.550red0.185.8
527.40.6350.12.40.080.9973616.033.03.580.6910.871red0.025.8
537.90.30.278.50.0360.993920.0112.02.960.4611.760white0.135.8
547.00.120.194.90.0550.995327.0127.03.290.419.450white0.165.8
556.80.270.2813.30.0760.997950.0163.03.030.388.660white0.145.8
566.00.260.187.00.0550.9959150.0194.03.210.439.050white0.195.8
576.80.210.5514.60.0530.9980534.0159.02.930.449.250white0.065.8
587.20.20.32.00.0390.991143.0188.03.30.4112.060white0.045.8
597.80.7350.082.40.0920.997410.041.03.240.719.860red0.165.8
607.70.580.11.80.1020.9956528.0109.03.080.499.860red0.175.8
6110.60.280.3915.50.0691.00266.023.03.120.669.250red0.155.8
6210.40.440.736.550.0740.99938.076.03.170.8512.071red0.095.8
636.40.320.274.90.0340.991618.0122.03.360.7112.560white0.175.8
646.70.30.4418.750.0570.9995665.0224.03.110.539.150white0.095.8
656.60.290.291.80.0360.9881938.0102.03.080.4213.771white0.15.8
666.80.260.468.30.0370.9960149.0173.03.170.479.350white0.25.8
677.50.380.294.90.0210.9902638.0113.03.080.4813.071white0.195.8
686.20.160.321.10.0360.9909674.0184.03.220.4111.060white0.125.8
697.70.250.492.50.0470.9925231.0169.03.070.5710.660white0.05.8
706.40.170.279.90.0470.9959626.0101.03.340.59.960white0.075.8
716.60.180.281.70.0410.9920753.0161.03.130.4510.260white0.195.8
727.10.20.279.60.0370.9944419.0105.03.040.3710.571white0.115.8
737.70.30.232.00.0680.9938228.0138.03.110.629.850white0.035.8
746.90.210.626.30.0420.993587.0109.02.960.5910.260white0.015.8
757.30.240.32.50.0420.991131.0104.03.050.5611.371white0.095.8
766.60.180.2617.30.0510.998417.0149.03.00.439.460white0.195.8
776.10.440.284.250.0320.991643.0132.03.260.4711.371white0.025.8
786.50.50.224.10.0360.990235.0131.03.260.5513.071white0.045.8
796.00.240.411.30.0360.9901842.0118.03.040.6411.733333333333360white0.05.8
806.00.290.4110.80.0480.993755.0149.03.090.5911.071white0.195.8
816.40.190.3510.20.0430.9963240.0106.03.160.59.760white0.195.8
827.10.680.02.30.0870.9978317.026.03.450.539.550red0.15.8
836.90.20.376.20.0270.99224.097.03.380.4912.271white0.125.8
846.80.250.244.550.0530.995541.0211.03.370.679.560white0.165.8
857.40.30.221.40.0460.992816.0135.03.080.7710.471white0.135.8
866.30.260.491.50.0520.992434.0134.02.990.619.860white0.075.8
876.10.170.284.50.0330.993346.0150.03.430.4910.960white0.25.8
886.50.210.372.50.0480.991770.0138.03.330.7511.471white0.195.8
896.40.310.2613.20.0460.997557.0205.03.170.419.650white0.15.8
907.90.330.411.50.05599999999999990.993966.035.03.290.7111.060red0.115.8
9110.60.360.572.30.0870.996766.020.03.140.7211.171red0.095.8
926.70.150.295.00.0580.994628.0105.03.520.4410.271white0.075.8
936.00.410.211.90.050.992829.0122.03.420.5210.560white0.165.8
948.00.550.178.20.040.995613.060.03.090.39.540white0.055.8
956.30.60.4411.00.050.997250.0245.03.190.579.340white0.115.8
966.40.30.515.50.0480.994262.0172.03.080.459.160white0.165.8
976.90.20.361.50.0310.993138.0147.03.350.5611.060white0.045.8
989.20.280.463.20.0580.99639.0133.03.140.589.550white0.05.8
997.80.30.2916.850.0540.999823.0135.03.160.389.060white0.185.8
1006.40.30.387.80.0460.995535.0192.03.10.379.050white0.015.8
Rows: 1-100 | Columns: 16

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 VastFrame to the predict() function, but in this case, it’s essential that the column names of the VastFrame match the predictors and response name in the model.

Parameter Modification

In order to see the parameters:

model.get_params()

And to manually change some of the parameters:

model.set_params({'n_neighbors': 3})
__init__(name: str = None, overwrite_model: bool = False, n_neighbors: int = 5, p: int = 2) None

Methods

__init__([name, overwrite_model, n_neighbors, p])

contour([nbins, chart])

Draws the model's contour plot.

deploySQL([X, test_relation, key_columns])

Returns the SQL code needed to deploy the model.

drop()

KNeighborsRegressor models are not stored in the VAST DataBase.

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) to draw a specific graphic.

import_models(path[, schema, kind])

Imports machine learning models.

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

Predicts using the input relation.

regression_report([metrics])

Computes a regression report using multiple metrics to evaluate the model (r2, mse, max error...).

report([metrics])

Computes a regression report using multiple metrics to evaluate the model (r2, mse, max error...).

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 VAST Binary format.

to_python([return_proba, ...])

Returns the Python function needed for in-memory scoring without using built-in VAST functions.

to_sql([X, return_proba, ...])

Returns the SQL code needed to deploy the model without using built-in VAST functions.

Attributes