Loading...

vastorbit.machine_learning.vast.neighbors.KNeighborsClassifier

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

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

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

Balancing the Dataset

In vastorbit, 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, vastorbit’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 vastorbit.machine_learning.vast.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 vastorbit.machine_learning.vast import KNeighborsClassifier

Then we can create the model:

model = KNeighborsClassifier(
   n_neighbors = 10,
   p = 2,
)

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

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()
567avg_macroavg_weightedavg_micro
auc0000.00.0[null]
prc_auc0.50.50.66588419405320810.55529473135106930.5550351316733648[null]
accuracy0.68857589984350540.64319248826291080.33176838810641630.55451225873761080.5540053046500180.5545122587376109
log_loss[null][null]138.47941333921926138.4794133392192645.94309174947493[null]
precision0.00.00.33176838810641630.110589462702138760.110070263346729670.3317683881064163
recall0.00.01.00.33333333333333330.33176838810641630.3317683881064163
f1_score0.00.00.498237367802585260.166079122600861740.165299408410247370.3317683881064163
mcc0.00.00.00.00.0-0.002347417840375587
informedness0.00.00.00.00.0-0.0023474178403756207
markedness-0.3114241001564946-0.35680751173708924-0.6682316118935837-0.4454877412623892-0.44599469534998204-0.0023474178403756207
csi0.00.00.33176838810641630.110589462702138760.110070263346729670.19887429643527205
Rows: 1-11 | Columns: 7

Important

Most metrics are computed using a single SQL query, but some of them might require multiple SQL queries. Selecting only the necessary metrics in the report can help optimize performance. E.g. model.report(metrics = ["auc", "accuracy"]).

For classification models, we can easily modify the cutoff to observe the effect on different metrics:

model.report(cutoff = 0.2)
567avg_macroavg_weightedavg_micro
auc0000.00.0[null]
prc_auc0.50.50.66588419405320810.55529473135106930.6658841940532081[null]
accuracynannan0.33176838810641630.33176838810641630.331768388106416350.3317683881064163
log_loss[null][null]138.4794133392193138.4794133392193138.4794133392193[null]
precision0.00.00.33176838810641630.110589462702138760.331768388106416350.3317683881064163
recall0.00.01.00.33333333333333331.01.0
f1_score0.00.00.498237367802585260.166079122600861740.49823736780258520.49823736780258526
mcc0.00.00.00.00.00.0
informedness-1.0-1.00.0-0.66666666666666660.00.0
markedness-1.0-1.0-0.6682316118935837-0.8894105372978612-0.6682316118935837-0.6682316118935837
csi0.00.00.33176838810641630.110589462702138760.331768388106416350.3317683881064163
Rows: 1-11 | Columns: 7

You can also use the KNeighborsClassifier.score function to compute any classification metric. The default metric is the accuracy:

model.score(metric = "f1", average = "macro")

Note

For multi-class scoring, vastorbit 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
Decimal(6, 3)
123
volatile_acidity
Decimal(7, 4)
123
density
Double
123
pH
Decimal(6, 3)
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
sulphates
Decimal(6, 3)
123
alcohol
Double
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
seedrand
Decimal(26, 6)
123
prediction
Integer
16.30.280.99583.390.248.450.03132.0172.00.579.771white0.017
26.60.580.993743.350.022.00.06237.053.00.7611.671red0.137
35.60.2350.9913.340.291.20.04733.0127.00.511.071white0.097
45.90.340.98943.410.222.40.0319.0135.00.7813.971white0.027
56.50.290.990533.270.311.70.03524.079.00.6911.471white0.077
65.80.280.989523.320.31.50.02631.0114.00.612.571white0.097
76.80.210.992143.180.46.30.03240.0121.00.5312.071white0.067
87.70.270.99723.250.683.50.3585.010.01.089.971red0.117
99.80.340.99473.190.391.40.0663.07.00.5511.471red0.077
107.00.320.991653.150.277.10.02737.0122.00.612.671white0.17
117.40.160.991983.040.331.20.04247.0121.00.6810.571white0.187
126.60.290.996853.020.449.00.05362.0178.00.458.950white0.017
136.00.280.99533.190.496.80.04861.0222.00.479.350white0.067
146.80.610.995253.420.041.50.0575.010.00.69.550red0.017
157.60.420.997843.150.253.90.10428.090.00.579.150red0.197
166.90.580.996833.40.011.90.0840.054.00.739.750red0.197
176.50.281.00023.140.2820.40.04140.0144.00.388.750white0.197
186.90.40.997053.40.225.950.08176.0303.00.579.450white0.097
195.50.140.99493.340.274.60.02922.0104.00.449.050white0.157
206.90.30.998273.110.2115.70.05649.0159.00.489.050white0.047
216.80.450.9973.090.311.80.09423.097.00.449.650white0.037
226.00.390.991143.060.131.20.04260.0172.00.5210.650white0.047
236.50.410.99463.140.224.80.05249.0142.00.629.250white0.127
246.30.340.994813.180.526.30.04763.0186.00.449.650white0.077
256.40.290.994783.10.36.50.20962.0156.00.49.450white0.157
266.20.250.998043.140.4415.80.05739.0167.00.519.250white0.157
276.40.220.989583.180.341.40.02356.0115.00.711.760white0.127
286.70.510.99043.220.242.10.04314.0155.00.613.060white0.17
298.10.191.00063.20.5816.650.04948.0181.00.629.160white0.077
308.00.360.99823.40.4310.10.05329.0146.00.469.560white0.097
316.70.450.991223.120.35.30.03627.0165.00.4612.260white0.057
325.80.3450.994943.250.1510.80.03326.0120.00.4910.060white0.157
336.60.280.995662.930.0910.90.05137.0131.00.629.560white0.147
347.70.580.995653.080.11.80.10228.0109.00.499.860red0.027
3510.60.50.997083.230.452.60.11934.068.00.7210.960red0.037
365.40.150.988783.040.322.50.03710.051.00.5812.660white0.047
375.80.20.991153.440.31.50.03121.057.00.5511.060white0.047
387.20.380.996853.420.31.80.07331.070.00.599.560red0.027
397.50.630.996163.260.272.00.08317.091.00.589.860red0.047
407.50.380.996053.360.572.30.1065.012.00.5511.460red0.047
417.60.290.99383.130.499.60.0345.0197.00.3812.371white0.197
427.50.310.993973.160.247.10.03128.0141.00.3810.671white0.087
437.10.130.99323.30.41.20.04754.0134.00.979.871white0.127
447.50.340.99243.160.356.00.03412.0126.00.3912.071white0.027
456.70.150.99463.520.295.00.05828.0105.00.4410.271white0.177
465.40.50.990793.480.135.00.02812.0107.00.8813.571white0.067
476.60.260.990893.220.212.90.02648.0126.00.3811.371white0.117
488.00.240.993163.440.261.70.03336.0136.00.5110.471white0.067
4910.20.290.99763.050.492.60.0595.013.00.7410.571red0.077
505.60.190.989643.460.312.70.02711.0100.00.413.271white0.07
516.30.430.991723.280.328.80.04218.0106.00.3312.971white0.057
526.70.190.991732.90.323.70.04126.076.00.5710.571white0.037
535.40.220.990923.290.356.50.02926.087.00.4412.571white0.077
546.00.20.990513.310.331.10.03945.0126.00.4511.671white0.137
557.20.20.99183.210.381.00.03721.074.00.3711.050white0.047
568.40.570.99813.150.4410.70.05146.0195.00.5110.450white0.07
578.50.660.997333.130.22.10.096999999999999923.0113.00.489.250red0.047
5811.20.40.997833.10.52.00.09919.050.00.5810.450red0.017
597.30.320.99853.20.2313.70.0549.0197.00.468.750white0.027
607.70.250.995553.10.37.80.03867.0196.00.510.150white0.127
616.80.260.99983.450.2916.950.05648.0179.00.49.650white0.177
629.60.210.99262.80.281.20.03812.053.00.4610.650white0.197
638.70.240.99263.080.350.60.04211.071.00.3810.650white0.157
647.10.330.994742.90.186.30.09427.0166.00.499.550white0.187
656.40.270.99553.180.458.30.0552.0196.00.489.550white0.037
666.40.320.99863.260.2316.20.05536.0176.00.549.150white0.177
676.20.250.995863.160.428.00.04953.0206.00.479.160white0.097
688.50.280.99813.130.3413.80.04132.0161.00.49.960white0.17
696.80.380.993263.110.299.90.03740.0146.00.3711.560white0.087
705.10.30.989443.290.32.30.04840.0150.00.4612.260white0.137
7111.60.440.9983.210.642.10.0595.015.00.6710.260red0.067
7211.50.180.99963.280.514.00.1044.023.00.9710.160red0.017
7313.40.271.00023.160.622.60.08199999999999996.021.00.679.760red0.077
746.00.220.992612.940.235.00.04510.0122.00.6310.060white0.077
755.70.220.998623.220.216.00.04441.0113.00.468.960white0.017
767.30.190.99463.320.681.50.0531.0156.00.649.460white0.187
776.90.240.99353.20.344.70.0443.0161.00.5910.660white0.017
786.00.240.99383.640.271.90.04840.0170.00.5410.071white0.137
797.20.20.993.160.342.70.03249.0151.00.3912.771white0.117
807.30.180.99183.20.291.20.04412.0143.00.4811.371white0.177
815.00.270.989563.450.324.50.03258.0178.00.3112.671white0.177
828.90.40.9953.320.512.60.05213.027.00.913.471red0.017
838.70.480.99643.330.32.80.06610.028.00.6711.271red0.087
849.10.210.995523.230.371.60.0676.010.00.5811.171red0.077
858.90.30.994583.150.354.60.03232.0148.00.4511.571white0.047
868.70.30.994743.120.344.80.01823.0127.00.4911.271white0.147
877.50.340.989583.20.284.00.02846.0100.00.513.271white0.167
887.40.160.998252.90.313.70.05633.0168.00.448.771white0.097
895.80.4150.99223.290.131.40.0411.064.00.5210.550white0.157
906.40.50.99743.280.1612.90.04226.0138.00.339.050white0.067
917.00.620.99783.480.081.80.0768.024.00.539.050red0.07
929.90.51.002423.160.513.80.20548.082.00.758.850red0.017
938.10.270.99082.990.411.450.03311.063.00.5612.050white0.067
947.50.270.99393.030.367.00.03645.0164.00.3311.050white0.177
958.60.180.991042.990.280.80.03225.078.00.3811.150white0.037
966.10.220.99283.30.491.50.05118.087.00.469.650white0.057
977.80.270.99343.150.281.80.0521.0127.00.449.950white0.117
987.70.260.9933.210.321.20.0426.0117.00.5610.850white0.057
996.90.320.9963.230.137.80.04211.0117.00.379.250white0.127
1007.10.260.9963.160.198.20.05153.0187.00.529.750white0.187
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.

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
Decimal(6, 3)
123
volatile_acidity
Decimal(7, 4)
123
density
Double
123
pH
Decimal(6, 3)
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
sulphates
Decimal(6, 3)
123
alcohol
Double
123
quality
Integer
123
good
Integer
Abc
color
Varchar(20)
123
seedrand
Decimal(26, 6)
123
prediction
Integer
123
prediction_5
Double
123
prediction_6
Double
123
prediction_7
Double
17.80.280.99013.00.341.60.02832.0118.00.3812.171white0.1370.00.01.0
28.00.570.99173.290.393.90.03422.0122.00.6712.871white0.070.00.01.0
37.30.650.99463.390.01.20.06515.021.00.4710.071red0.0570.00.01.0
47.90.650.99633.340.012.50.07817.038.00.7411.771red0.1970.00.01.0
510.50.420.9973.240.662.950.115999999999999912.029.00.7511.771red0.1570.00.01.0
610.20.440.997453.290.584.10.09211.024.00.9912.071red0.0970.00.01.0
75.30.30.987423.310.31.20.02925.093.00.413.671white0.0870.00.01.0
86.80.180.994063.110.289.80.03929.0113.00.4510.971white0.1170.00.01.0
97.10.330.99123.080.33.30.03430.0102.00.3112.371white0.070.00.01.0
107.40.240.99363.180.261.60.05853.0150.00.59.971white0.0170.00.01.0
116.00.380.988723.180.263.50.03538.0111.00.4713.671white0.1670.00.01.0
125.30.470.991823.540.112.20.04816.089.00.8813.671red0.1270.00.01.0
1311.90.380.99963.240.512.00.1217.020.00.7610.460red0.1270.00.01.0
149.80.250.99823.420.492.70.08815.033.00.910.060red0.0170.00.01.0
155.80.130.99563.320.2212.70.05824.0183.00.4211.760white0.0170.00.01.0
167.00.260.99862.940.4615.550.03761.0171.00.358.860white0.0470.00.01.0
176.60.250.99423.370.241.70.04826.0124.00.610.160white0.1670.00.01.0
187.60.230.99673.080.4910.00.03645.0182.00.589.660white0.0870.00.01.0
196.40.370.99823.140.4913.30.04553.0243.00.488.560white0.1270.00.01.0
207.30.210.9973.40.310.90.03718.0112.00.59.660white0.0170.00.01.0
217.30.190.99643.360.246.30.05434.0231.00.5410.060white0.1870.00.01.0
226.80.140.991643.270.181.40.04730.090.00.5411.260white0.1770.00.01.0
237.00.240.996363.470.249.00.0342.0219.00.4610.260white0.1470.00.01.0
246.70.20.991043.110.31.40.02517.076.00.4411.060white0.0970.00.01.0
256.50.20.999412.940.518.10.05450.0221.00.648.860white0.0370.00.01.0
266.40.160.99083.180.421.00.03629.0113.00.5211.060white0.0570.00.01.0
276.60.180.992073.130.281.70.04153.0161.00.4510.260white0.0770.00.01.0
286.40.320.993.060.311.90.03734.0126.00.4511.860white0.0170.00.01.0
297.10.60.995143.40.012.30.07924.037.00.6110.960red0.0370.00.01.0
306.20.570.994483.440.12.10.0484.011.00.7610.860red0.1370.00.01.0
316.00.190.99723.30.2612.40.04850.0147.00.368.960white0.0970.00.01.0
325.70.360.99413.290.216.70.03851.0166.00.6310.060white0.1870.00.01.0
336.80.390.997753.070.3511.60.04457.0220.00.539.350white0.070.00.01.0
3410.00.730.99663.150.432.30.05915.031.00.5711.050red0.0370.00.01.0
359.00.460.99833.10.232.80.09228.0104.00.569.250red0.0770.00.01.0
367.50.420.996283.240.322.70.0677.025.00.4410.450red0.0470.00.01.0
376.10.460.995373.350.326.20.05310.094.00.4710.150white0.1170.00.01.0
387.40.740.995023.120.071.70.08615.048.00.4810.050red0.1570.00.01.0
397.30.280.99943.280.4214.40.0449.0173.00.829.071white0.1270.00.01.0
406.90.280.99183.350.31.60.04746.0132.00.3811.171white0.1370.00.01.0
416.60.320.991983.40.264.60.03126.0120.00.7312.571white0.1770.00.01.0
426.00.310.989523.320.272.30.04219.0120.00.4112.771white0.0970.00.01.0
437.00.260.997873.470.2610.80.03937.0184.00.5810.371white0.1170.00.01.0
446.80.180.998083.00.312.80.06219.0171.00.529.071white0.0770.00.01.0
456.70.30.991593.310.292.80.02537.0107.00.6311.371white0.070.00.01.0
466.70.280.990643.260.282.40.01236.0100.00.3911.771white0.0870.00.01.0
475.80.220.989363.140.290.90.03434.089.00.3611.171white0.0670.00.01.0
485.90.330.99113.220.328.10.0389.034.00.3612.771white0.1470.00.01.0
496.40.230.992163.180.354.60.03943.0147.00.411.071white0.1970.00.01.0
506.10.320.98983.290.241.50.03638.0124.00.4212.471white0.0270.00.01.0
5112.90.51.000123.090.552.80.0727.024.00.6810.960red0.0770.00.01.0
5210.40.260.997243.330.481.90.0666.010.00.8710.960red0.070.00.01.0
536.50.290.993393.240.39.150.05125.0166.00.5611.3560white0.0470.00.01.0
546.70.20.99873.160.4214.00.03883.0160.00.59.460white0.0870.00.01.0
556.80.180.9923.30.371.50.02737.093.00.4510.860white0.0370.00.01.0
566.50.220.992973.050.191.10.06436.0191.00.59.560white0.0570.00.01.0
577.20.240.990763.130.362.00.02921.063.00.6312.560white0.0470.00.01.0
586.20.240.99523.190.2812.20.04954.0133.00.3710.760white0.1370.00.01.0
597.30.260.996933.10.3311.80.05748.0127.00.5510.060white0.0270.00.01.0
607.80.30.992223.060.364.60.02420.0198.00.6611.960white0.0770.00.01.0
616.30.20.99173.460.41.50.03735.0107.00.511.460white0.1570.00.01.0
627.30.510.99443.010.263.30.097.0135.00.528.850white0.1770.00.01.0
636.70.580.99593.280.081.80.096999999999999915.065.00.549.250red0.0870.00.01.0
648.10.670.99683.170.551.80.116999999999999932.0141.00.629.450red0.0170.00.01.0
6511.90.381.00043.160.492.70.09812.042.00.6110.350red0.1770.00.01.0
667.10.310.99652.940.32.20.05336.0127.01.629.550red0.1470.00.01.0
677.10.520.997453.50.032.60.07621.092.00.69.850red0.0470.00.01.0
686.40.180.99783.580.7411.90.04654.0168.00.6810.150white0.0470.00.01.0
697.10.30.99423.40.491.60.04531.0100.00.5910.250white0.170.00.01.0
706.40.220.99483.40.327.90.02934.0124.00.3910.250white0.0670.00.01.0
716.60.450.99543.120.437.20.06431.0186.00.449.450white0.1370.00.01.0
727.40.350.993043.140.246.00.04228.0123.00.4411.350white0.0570.00.01.0
737.30.320.995883.430.232.30.06635.070.00.6210.150red0.0870.00.01.0
747.10.310.996633.140.2511.20.04832.0136.00.49.550white0.1570.00.01.0
757.30.180.99183.20.291.20.04412.0143.00.4811.371white0.0270.00.01.0
767.50.520.99683.380.161.90.08512.035.00.629.571red0.0370.00.01.0
778.70.30.994743.120.344.80.01823.0127.00.4911.271white0.0170.00.01.0
787.40.160.99842.90.2715.50.0525.0135.00.438.771white0.0370.00.01.0
796.90.330.99243.250.265.00.02746.0143.00.4311.271white0.1870.00.01.0
806.30.370.99213.290.286.30.03445.0152.00.4611.671white0.0670.00.01.0
816.00.240.990143.130.332.50.02631.085.00.511.371white0.0770.00.01.0
825.80.170.992023.430.361.30.03611.070.00.6810.471white0.0670.00.01.0
838.20.420.99113.00.294.10.0331.0100.00.3212.871white0.170.00.01.0
847.00.20.99223.250.342.10.04912.0136.00.4611.671white0.0470.00.01.0
856.10.360.99173.40.272.10.03516.0100.00.7111.571white0.1370.00.01.0
8612.70.591.03.00.452.30.081999999999999911.022.00.79.360red0.0670.00.01.0
875.60.2950.991543.470.261.10.03540.0102.00.5610.660white0.1870.00.01.0
886.40.260.99563.390.217.10.0435.0162.00.589.960white0.0770.00.01.0
896.00.260.98963.380.291.00.03227.096.00.4412.360white0.0170.00.01.0
907.20.20.9973.40.3110.00.05449.0165.00.429.960white0.0670.00.01.0
917.50.250.995633.120.328.20.02453.0209.00.4610.860white0.0270.00.01.0
926.30.20.996683.160.1912.30.04854.0145.00.429.360white0.0870.00.01.0
936.70.390.99483.460.312.70.05427.0202.00.5710.560white0.1170.00.01.0
947.00.310.99543.130.267.40.06928.0160.00.469.860white0.1970.00.01.0
956.10.40.9933.220.310.90.04823.0170.00.779.560white0.1770.00.01.0
966.30.340.99773.230.2814.70.04749.0198.00.469.550white0.1470.00.01.0
976.20.310.99413.150.216.30.04150.0218.00.610.050white0.0970.00.01.0
986.10.270.999853.40.316.70.03949.0172.00.459.450white0.0570.00.01.0
995.80.4150.99223.290.131.40.0411.064.00.5210.550white0.1470.00.01.0
1006.80.241.03.30.3118.30.04640.0142.00.418.750white0.0770.00.01.0
Rows: 1-100 | Columns: 19

Note

Probabilities are added to the VastFrame, and vastorbit 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)

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)

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

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

Methods

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

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.

drop()

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

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.

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