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
KNeighborsRegressorobject 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
pof thep-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
pof thep-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 fromverticapyare 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()
123fixed_acidity123volatile_acidity123citric_acid123residual_sugar123chlorides123free_sulfur_dioxide123total_sulfur_dioxide123density123pH123sulphates123alcohol123quality123goodAbccolor1 3.9 0.225 0.4 4.2 0.03 29.0 118.0 0.989 3.57 0.36 12.8 8 1 white 2 4.7 0.335 0.14 1.3 0.036 69.0 168.0 0.99212 3.47 0.46 10.5 5 0 white 3 4.7 0.455 0.18 1.9 0.036 33.0 106.0 0.98746 3.21 0.83 14.0 7 1 white 4 4.7 0.785 0.0 3.4 0.036 23.0 134.0 0.98981 3.53 0.92 13.8 6 0 white 5 4.9 0.345 0.34 1.0 0.068 32.0 143.0 0.99138 3.24 0.4 10.1 5 0 white 6 4.9 0.345 0.34 1.0 0.068 32.0 143.0 0.99138 3.24 0.4 10.1 5 0 white 7 4.9 0.42 0.0 2.1 0.048 16.0 42.0 0.99154 3.71 0.74 14.0 7 1 red 8 5.0 0.27 0.4 1.2 0.076 42.0 124.0 0.99204 3.32 0.47 10.1 6 0 white 9 5.0 0.31 0.0 6.4 0.046 43.0 166.0 0.994 3.3 0.63 9.9 6 0 white 10 5.0 0.4 0.5 4.3 0.046 29.0 80.0 0.9902 3.49 0.66 13.6 6 0 red 11 5.0 0.44 0.04 18.6 0.039 38.0 128.0 0.9985 3.37 0.57 10.2 6 0 white 12 5.1 0.11 0.32 1.6 0.028 12.0 90.0 0.99008 3.57 0.52 12.2 6 0 white 13 5.1 0.14 0.25 0.7 0.039 15.0 89.0 0.9919 3.22 0.43 9.2 6 0 white 14 5.1 0.165 0.22 5.7 0.047 42.0 146.0 0.9934 3.18 0.55 9.9 6 0 white 15 5.1 0.33 0.22 1.6 0.027 18.0 89.0 0.9893 3.51 0.38 12.5 7 1 white 16 5.1 0.33 0.22 1.6 0.027 18.0 89.0 0.9893 3.51 0.38 12.5 7 1 white 17 5.1 0.33 0.22 1.6 0.027 18.0 89.0 0.9893 3.51 0.38 12.5 7 1 white 18 5.1 0.39 0.21 1.7 0.027 15.0 72.0 0.9894 3.5 0.45 12.5 6 0 white 19 5.2 0.2 0.27 3.2 0.047 16.0 93.0 0.99235 3.44 0.53 10.1 7 1 white 20 5.2 0.21 0.31 1.7 0.048 17.0 61.0 0.98953 3.24 0.37 12.0 7 1 white 21 5.2 0.22 0.46 6.2 0.066 41.0 187.0 0.99362 3.19 0.42 9.73333333333333 5 0 white 22 5.2 0.31 0.2 2.4 0.027 27.0 117.0 0.98886 3.56 0.45 13.0 7 1 white 23 5.2 0.32 0.25 1.8 0.103 13.0 50.0 0.9957 3.38 0.55 9.2 5 0 red 24 5.2 0.34 0.37 6.2 0.031 42.0 133.0 0.99076 3.25 0.41 12.5 6 0 white 25 5.2 0.36 0.02 1.6 0.031 24.0 104.0 0.9896 3.44 0.35 12.2 6 0 white 26 5.2 0.365 0.08 13.5 0.041 37.0 142.0 0.997 3.46 0.39 9.9 6 0 white 27 5.2 0.48 0.04 1.6 0.054 19.0 106.0 0.9927 3.54 0.62 12.2 7 1 red 28 5.2 0.5 0.18 2.0 0.036 23.0 129.0 0.98949 3.36 0.77 13.4 7 1 white 29 5.3 0.16 0.39 1.0 0.028 40.0 101.0 0.99156 3.57 0.59 10.6 6 0 white 30 5.3 0.16 0.39 1.0 0.028 40.0 101.0 0.99156 3.57 0.59 10.6 6 0 white 31 5.3 0.165 0.24 1.1 0.051 25.0 105.0 0.9925 3.32 0.47 9.1 5 0 white 32 5.3 0.23 0.56 0.9 0.041 46.0 141.0 0.99119 3.16 0.62 9.7 5 0 white 33 5.3 0.3 0.3 1.2 0.029 25.0 93.0 0.98742 3.31 0.4 13.6 7 1 white 34 5.3 0.33 0.3 1.2 0.048 25.0 119.0 0.99045 3.32 0.62 11.3 6 0 white 35 5.3 0.36 0.27 6.3 0.028 40.0 132.0 0.99186 3.37 0.4 11.6 6 0 white 36 5.3 0.36 0.27 6.3 0.028 40.0 132.0 0.99186 3.37 0.4 11.6 6 0 white 37 5.3 0.4 0.25 3.9 0.031 45.0 130.0 0.99072 3.31 0.58 11.75 7 1 white 38 5.3 0.47 0.11 2.2 0.048 16.0 89.0 0.99182 3.54 0.88 13.6 7 1 red 39 5.3 0.47 0.11 2.2 0.048 16.0 89.0 0.99182 3.54 0.88 13.5666666666667 7 1 red 40 5.3 0.715 0.19 1.5 0.161 7.0 62.0 0.99395 3.62 0.61 11.0 5 0 red 41 5.4 0.22 0.29 1.2 0.045 69.0 152.0 0.99178 3.76 0.63 11.0 7 1 white 42 5.4 0.595 0.1 2.8 0.042 26.0 80.0 0.9932 3.36 0.38 9.3 5 0 white 43 5.4 0.74 0.09 1.7 0.089 16.0 26.0 0.99402 3.67 0.56 11.6 6 0 red 44 5.5 0.12 0.33 1.0 0.038 23.0 131.0 0.99164 3.25 0.45 9.8 5 0 white 45 5.5 0.12 0.33 1.0 0.038 23.0 131.0 0.99164 3.25 0.45 9.8 5 0 white 46 5.5 0.14 0.27 4.6 0.029 22.0 104.0 0.9949 3.34 0.44 9.0 5 0 white 47 5.5 0.14 0.27 4.6 0.029 22.0 104.0 0.9949 3.34 0.44 9.0 5 0 white 48 5.5 0.16 0.31 1.2 0.026 31.0 68.0 0.9898 3.33 0.44 11.65 6 0 white 49 5.5 0.16 0.31 1.2 0.026 31.0 68.0 0.9898 3.33 0.44 11.6333333333333 6 0 white 50 5.5 0.18 0.22 5.5 0.037 10.0 86.0 0.99156 3.46 0.44 12.2 5 0 white 51 5.5 0.24 0.45 1.7 0.046 22.0 113.0 0.99224 3.22 0.48 10.0 5 0 white 52 5.5 0.29 0.3 1.1 0.022 20.0 110.0 0.98869 3.34 0.38 12.8 7 1 white 53 5.5 0.31 0.29 3.0 0.027 16.0 102.0 0.99067 3.23 0.56 11.2 6 0 white 54 5.5 0.32 0.45 4.9 0.028 25.0 191.0 0.9922 3.51 0.49 11.5 7 1 white 55 5.5 0.35 0.35 1.1 0.045 14.0 167.0 0.992 3.34 0.68 9.9 6 0 white 56 5.5 0.375 0.38 1.7 0.036 17.0 98.0 0.99142 3.29 0.39 10.5 6 0 white 57 5.6 0.15 0.26 5.55 0.051 51.0 139.0 0.99336 3.47 0.5 11.0 6 0 white 58 5.6 0.15 0.31 5.3 0.038 8.0 79.0 0.9923 3.3 0.39 10.5 6 0 white 59 5.6 0.16 0.27 1.4 0.044 53.0 168.0 0.9918 3.28 0.37 10.1 6 0 white 60 5.6 0.175 0.29 0.8 0.043 20.0 67.0 0.99112 3.28 0.48 9.9 6 0 white 61 5.6 0.185 0.19 7.1 0.048 36.0 110.0 0.99438 3.26 0.41 9.5 6 0 white 62 5.6 0.185 0.19 7.1 0.048 36.0 110.0 0.99438 3.26 0.41 9.5 6 0 white 63 5.6 0.22 0.32 1.2 0.024 29.0 97.0 0.98823 3.2 0.46 13.05 7 1 white 64 5.6 0.26 0.18 1.4 0.034 18.0 135.0 0.99174 3.32 0.35 10.2 6 0 white 65 5.6 0.26 0.26 5.7 0.031 12.0 80.0 0.9923 3.25 0.38 10.8 5 0 white 66 5.6 0.26 0.5 11.4 0.029 25.0 93.0 0.99428 3.23 0.49 10.5 6 0 white 67 5.6 0.28 0.28 4.2 0.044 52.0 158.0 0.992 3.35 0.44 10.7 7 1 white 68 5.6 0.3 0.1 6.4 0.043 34.0 142.0 0.99382 3.14 0.48 9.8 5 0 white 69 5.6 0.35 0.14 5.0 0.046 48.0 198.0 0.9937 3.3 0.71 10.3 5 0 white 70 5.6 0.49 0.13 4.5 0.039 17.0 116.0 0.9907 3.42 0.9 13.7 7 1 white 71 5.6 0.49 0.13 4.5 0.039 17.0 116.0 0.9907 3.42 0.9 13.7 7 1 white 72 5.6 0.66 0.0 2.2 0.087 3.0 11.0 0.99378 3.71 0.63 12.8 7 1 red 73 5.6 0.66 0.0 2.2 0.087 3.0 11.0 0.99378 3.71 0.63 12.8 7 1 red 74 5.7 0.15 0.47 11.4 0.035 49.0 128.0 0.99456 3.03 0.34 10.5 8 1 white 75 5.7 0.18 0.26 2.2 0.023 21.0 95.0 0.9893 3.07 0.54 12.3 6 0 white 76 5.7 0.18 0.36 1.2 0.046 9.0 71.0 0.99199 3.7 0.68 10.9 7 1 white 77 5.7 0.2 0.3 2.5 0.046 38.0 125.0 0.99276 3.34 0.5 9.9 6 0 white 78 5.7 0.21 0.32 0.9 0.038 38.0 121.0 0.99074 3.24 0.46 10.6 6 0 white 79 5.7 0.21 0.37 4.5 0.04 58.0 140.0 0.99332 3.29 0.62 10.6 6 0 white 80 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 81 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 82 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 83 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 84 5.7 0.22 0.2 16.0 0.044 41.0 113.0 0.99862 3.22 0.46 8.9 6 0 white 85 5.7 0.22 0.29 3.5 0.04 27.0 146.0 0.98999 3.17 0.36 12.1 6 0 white 86 5.7 0.23 0.28 9.65 0.025 26.0 121.0 0.9925 3.28 0.38 11.3 6 0 white 87 5.7 0.25 0.26 12.5 0.049 52.5 106.0 0.99691 3.08 0.45 9.4 6 0 white 88 5.7 0.25 0.26 12.5 0.049 52.5 120.0 0.99691 3.08 0.45 9.4 6 0 white 89 5.7 0.25 0.27 11.5 0.04 24.0 120.0 0.99411 3.33 0.31 10.8 6 0 white 90 5.7 0.26 0.24 17.8 0.059 23.0 124.0 0.99773 3.3 0.5 10.1 5 0 white 91 5.7 0.26 0.24 17.8 0.059 23.0 124.0 0.99773 3.3 0.5 10.1 5 0 white 92 5.7 0.26 0.24 17.8 0.059 23.0 124.0 0.99773 3.3 0.5 10.1 5 0 white 93 5.7 0.27 0.32 1.2 0.046 20.0 155.0 0.9934 3.8 0.41 10.2 6 0 white 94 5.7 0.28 0.24 17.5 0.044 60.0 167.0 0.9989 3.31 0.44 9.4 5 0 white 95 5.7 0.32 0.18 1.4 0.029 26.0 104.0 0.9906 3.44 0.37 11.0 6 0 white 96 5.7 0.32 0.38 4.75 0.033 23.0 94.0 0.991 3.42 0.42 11.8 7 1 white 97 5.7 0.36 0.34 4.2 0.026 21.0 77.0 0.9907 3.41 0.45 11.9 6 0 white 98 5.8 0.14 0.15 6.1 0.042 27.0 123.0 0.99362 3.06 0.6 9.9 6 0 white 99 5.8 0.15 0.32 1.2 0.037 14.0 119.0 0.99137 3.19 0.5 10.2 6 0 white 100 5.8 0.17 0.34 1.8 0.045 96.0 170.0 0.99035 3.38 0.9 11.8 8 1 white Rows: 1-100 | Columns: 14Note
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 intotablesortemporary tables. This will help enhance the overall performance of the process.Model Initialization¶
First we import the
KNeighborsRegressormodel:from verticapy.machine_learning.vertica import KNeighborsRegressor
Then we can create the model:
model = KNeighborsRegressor()
Hint
In
verticapy1.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
vDataFrameor the name of the relation stored in the database. The test set is optional and is only used to compute the test metrics. Inverticapy, we don’t work usingXmatrices andyvectors. 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_variance 0.327634969460444 max_error 2.8 median_absolute_error 0.4 mean_absolute_error 0.536566589684373 mean_squared_error 0.490685142417244 root_mean_squared_error 0.700225411853709 r2 0.324226399436746 r2_adj 0.32108813194187 aic -910.663781877801 bic -874.640996362191 Rows: 1-10 | Columns: 2Important
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 Regression 6 343.768160123172 57.294693353861994 115.02384208650274 1.6396704849276214e-116 Residual 1292 643.56 0.4981114551083591 Total 1298 942.505003849115 Rows: 1-3 | Columns: 6You can also use the
KNeighborsRegressor.scorefunction 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", )
123fixed_acidity123volatile_acidity123citric_acid123residual_sugar123chlorides123density123free_sulfur_dioxide123total_sulfur_dioxide123pH123sulphates123alcohol123quality123goodAbccolor123prediction1 7.2 0.21 0.37 1.6 0.049 0.9924 23.0 94.0 3.16 0.48 10.9 7 1 white 5.6 2 5.9 0.37 0.14 6.3 0.036 0.9944 34.0 185.0 3.17 0.63 9.8 5 0 white 5.2 3 5.0 0.3 0.33 3.7 0.03 0.9887 54.0 173.0 3.36 0.3 13.0 7 1 white 7.0 4 5.5 0.12 0.33 1.0 0.038 0.99164 23.0 131.0 3.25 0.45 9.8 5 0 white 5.2 5 5.6 0.41 0.22 7.1 0.05 0.9931 44.0 154.0 3.3 0.4 10.5 5 0 white 5.6 6 7.2 0.3 0.26 1.5 0.041 0.99154 46.0 178.0 3.19 0.56 11.3 6 0 white 5.6 7 5.9 0.2 0.23 1.5 0.037 0.99021 38.0 93.0 3.36 0.49 12.0 6 0 white 6.0 8 6.1 0.3 0.56 2.8 0.044 0.9924 47.0 179.0 3.3 0.57 10.9 7 1 white 6.2 9 6.9 0.28 0.37 9.1 0.037 0.9948 16.0 76.0 3.05 0.54 11.1 5 0 white 5.4 10 6.0 0.1 0.24 1.1 0.041 0.9927 15.0 65.0 3.61 0.61 10.3 7 1 white 6.2 11 6.2 0.25 0.54 7.0 0.046 0.99454 58.0 176.0 3.19 0.7 10.4 5 0 white 5.4 12 6.8 0.36 0.32 1.6 0.039 0.9948 10.0 124.0 3.3 0.67 9.6 5 0 white 5.8 13 6.5 0.33 0.38 2.5 0.047 0.98964 30.0 148.0 3.17 0.43 12.7 6 0 white 6.0 14 6.0 0.41 0.05 1.5 0.063 0.9932 17.0 120.0 3.21 0.56 9.2 6 0 white 5.2 15 6.0 0.29 0.41 10.8 0.048 0.9937 55.0 149.0 3.09 0.59 10.9666666666667 7 1 white 6.4 16 7.1 0.38 0.29 13.6 0.041 0.99461 30.0 137.0 3.02 0.96 12.1 6 0 white 5.8 17 8.4 0.62 0.09 2.2 0.084 0.9964 11.0 108.0 3.15 0.66 9.8 5 0 red 5.2 18 6.7 0.24 0.36 8.4 0.042 0.99473 42.0 123.0 3.34 0.52 10.9 6 0 white 5.8 19 6.2 0.27 0.18 1.5 0.028 0.99228 20.0 111.0 3.41 0.5 10.0 5 0 white 6.8 20 6.2 0.17 0.3 1.1 0.037 0.993 14.0 79.0 3.5 0.54 10.3 6 0 white 6.4 21 6.4 0.46 0.08 4.9 0.046 0.99445 34.0 144.0 3.1 0.56 10.0 5 0 white 5.8 22 6.0 0.24 0.34 1.0 0.036 0.99097 52.0 184.0 3.44 0.44 11.45 6 0 white 6.0 23 5.9 0.62 0.28 3.5 0.039 0.9907 55.0 152.0 3.44 0.44 12.0 6 0 white 6.2 24 8.2 0.37 0.36 1.0 0.034 0.9906 17.0 93.0 3.04 0.32 11.7 8 1 white 6.6 25 9.9 0.49 0.58 3.5 0.094 1.0004 9.0 43.0 3.29 0.58 9.0 5 0 red 6.2 26 5.8 0.22 0.3 1.1 0.047 0.992 36.0 131.0 3.26 0.45 10.4 5 0 white 5.8 27 8.9 0.43 0.45 1.9 0.052 0.9948 6.0 16.0 3.35 0.7 12.5 6 0 red 5.8 28 7.7 0.31 0.36 4.3 0.026 0.99152 15.0 87.0 3.11 0.48 12.0 5 0 white 5.6 29 7.1 0.24 0.41 17.8 0.046 0.9998 39.0 145.0 3.32 0.39 8.7 5 0 white 5.8 30 6.6 0.41 0.27 10.7 0.11 0.99672 20.0 103.0 3.08 0.41 9.0 6 0 white 5.4 31 6.8 0.18 0.24 9.8 0.058 0.9952 64.0 188.0 3.13 0.51 10.6 6 0 white 6.4 32 5.7 0.18 0.36 1.2 0.046 0.99199 9.0 71.0 3.7 0.68 10.9 7 1 white 6.2 33 6.4 0.16 0.44 1.2 0.051 0.99058 39.0 122.0 3.11 0.75 11.3 7 1 white 6.8 34 7.0 0.15 0.28 14.7 0.051 0.99792 29.0 149.0 2.96 0.39 9.0 7 1 white 7.0 35 6.8 0.915 0.29 4.8 0.07 0.99577 15.0 39.0 3.53 0.54 11.1 5 0 red 5.8 36 7.9 0.53 0.24 2.0 0.072 0.996 15.0 105.0 3.27 0.54 9.4 6 0 red 5.2 37 6.7 0.34 0.43 1.6 0.041 0.99014 29.0 114.0 3.23 0.44 12.6 6 0 white 6.0 38 6.4 0.23 0.26 8.1 0.054 0.9954 47.0 181.0 3.12 0.49 9.4 5 0 white 5.0 39 6.2 0.35 0.2 18.1 0.069 0.99908 33.0 158.0 3.15 0.5 8.8 6 0 white 5.4 40 7.2 0.4 0.62 10.8 0.041 0.9976 70.0 189.0 3.08 0.49 8.6 4 0 white 4.8 41 8.3 0.27 0.39 2.4 0.058 0.9955 16.0 107.0 3.28 0.59 10.3 5 0 white 6.4 42 5.9 0.19 0.21 1.7 0.045 0.99341 57.0 135.0 3.32 0.44 9.5 5 0 red 5.8 43 6.6 0.56 0.22 8.9 0.034 0.99675 27.0 133.0 3.2 0.51 9.1 5 0 white 5.0 44 6.9 0.38 0.38 13.1 0.112 0.99792 14.0 94.0 3.02 0.48 9.2 5 0 white 5.6 45 7.7 0.835 0.0 2.6 0.081 0.9975 6.0 14.0 3.3 0.52 9.3 5 0 red 5.4 46 7.1 0.44 0.62 11.8 0.044 0.9975 52.0 152.0 3.12 0.46 8.7 6 0 white 5.6 47 7.7 0.27 0.68 3.5 0.358 0.9972 5.0 10.0 3.25 1.08 9.9 7 1 red 6.6 48 6.4 0.36 0.53 2.2 0.23 0.9934 19.0 35.0 3.37 0.93 12.4 6 0 red 5.8 49 7.0 0.2 0.35 8.8 0.037 0.99388 31.0 103.0 3.13 0.49 11.0 6 0 white 6.0 50 8.0 0.3 0.28 5.7 0.044 0.9948 31.0 124.0 3.16 0.51 10.2 6 0 white 6.0 51 7.5 0.42 0.31 1.6 0.08 0.9978 15.0 42.0 3.31 0.64 9.0 5 0 red 5.6 52 7.0 0.14 0.4 1.7 0.035 0.9911 16.0 85.0 3.19 0.42 11.8 6 0 white 6.2 53 6.8 0.48 0.25 2.0 0.076 0.9953 29.0 61.0 3.34 0.6 10.4 5 0 red 6.2 54 7.6 0.23 0.34 1.6 0.043 0.99305 24.0 129.0 3.12 0.7 10.4 5 0 white 4.8 55 7.9 0.49 0.32 1.9 0.082 0.9968 17.0 144.0 3.2 0.55 9.5 5 0 red 5.4 56 6.9 0.22 0.49 7.0 0.063 0.9957 50.0 168.0 3.54 0.5 10.3 6 0 white 5.8 57 6.4 0.38 0.26 8.2 0.043 0.99234 28.0 98.0 2.99 0.31 11.4 6 0 white 5.2 58 7.6 0.31 0.29 10.5 0.04 0.9966 21.0 145.0 3.04 0.35 9.4 5 0 white 5.4 59 7.0 0.31 0.52 1.7 0.029 0.9918 5.0 61.0 3.07 0.43 10.4 5 0 white 5.4 60 6.4 0.31 0.53 8.8 0.057 0.99642 36.0 221.0 3.17 0.44 9.1 5 0 white 5.0 61 6.7 0.17 0.42 10.4 0.038 0.99628 85.0 182.0 3.04 0.44 8.9 6 0 white 6.0 62 6.1 0.6 0.12 1.8 0.05 0.99268 11.0 76.0 3.42 0.48 10.4 4 0 white 5.2 63 7.4 0.31 0.28 1.6 0.05 0.9929 33.0 137.0 3.31 0.56 10.5 6 0 white 6.2 64 4.7 0.67 0.09 1.0 0.02 0.98722 5.0 9.0 3.3 0.34 13.6 5 0 white 5.2 65 6.7 0.3 0.44 18.75 0.057 0.99956 65.0 224.0 3.11 0.53 9.1 5 0 white 5.0 66 6.9 0.28 0.41 1.7 0.05 0.993 10.0 136.0 3.16 0.71 11.4 6 0 white 6.8 67 6.8 0.25 0.28 5.0 0.035 0.99048 42.0 126.0 3.12 0.38 12.6 7 1 white 6.0 68 7.5 0.38 0.29 12.7 0.05 0.9986 25.0 209.0 3.25 0.59 9.3 6 0 white 5.0 69 7.8 0.3 0.29 16.85 0.054 0.9998 23.0 135.0 3.16 0.38 9.0 6 0 white 5.6 70 5.6 0.12 0.33 2.9 0.044 0.98896 21.0 73.0 3.17 0.32 12.9 8 1 white 6.6 71 7.6 0.23 0.29 8.6 0.053 0.9963 65.0 146.0 3.11 0.32 9.8 6 0 white 5.2 72 5.9 0.36 0.04 5.7 0.046 0.9934 21.0 87.0 3.22 0.51 10.2 5 0 white 5.0 73 6.9 0.41 0.31 2.0 0.079 0.99668 21.0 51.0 3.47 0.55 9.5 6 0 red 5.8 74 7.2 0.5 0.18 2.1 0.071 0.99761 12.0 31.0 3.52 0.72 9.6 6 0 red 5.8 75 6.2 0.21 0.24 1.2 0.051 0.99036 31.0 95.0 3.24 0.57 11.3 6 0 white 5.6 76 8.8 0.19 0.3 5.0 0.028 0.99242 34.0 120.0 2.94 0.47 11.2 5 0 white 5.8 77 8.5 0.2 0.4 1.1 0.046 0.99194 31.0 106.0 3.0 0.35 10.5 4 0 white 5.4 78 9.0 0.8 0.12 2.4 0.083 0.99836 8.0 28.0 3.33 0.65 10.4 6 0 red 5.4 79 6.6 0.31 0.28 1.4 0.035 0.98836 28.0 107.0 3.0 0.4 13.2 6 0 white 5.6 80 10.4 0.52 0.45 2.0 0.08 0.99774 6.0 13.0 3.22 0.76 11.4 6 0 red 5.8 81 6.7 0.24 0.3 3.85 0.042 0.99189 105.0 179.0 3.04 0.59 11.3 8 1 white 7.6 82 6.3 0.23 0.3 1.8 0.033 0.9906 16.0 91.0 3.28 0.4 11.8 6 0 white 5.6 83 11.5 0.45 0.5 3.0 0.078 1.0003 19.0 47.0 3.26 1.11 11.0 6 0 red 5.8 84 6.2 0.56 0.09 1.7 0.053 0.99402 24.0 32.0 3.54 0.6 11.3 5 0 red 5.2 85 7.0 0.53 0.02 1.0 0.036 0.993 39.0 107.0 3.2 0.32 9.0 5 0 white 5.2 86 7.0 0.15 0.28 14.7 0.051 0.99792 29.0 149.0 2.96 0.39 9.0 7 1 white 7.0 87 6.1 0.27 0.25 1.8 0.041 0.9929 9.0 109.0 3.08 0.54 9.0 5 0 white 5.2 88 5.1 0.14 0.25 0.7 0.039 0.9919 15.0 89.0 3.22 0.43 9.2 6 0 white 5.6 89 7.2 1.0 0.0 3.0 0.102 0.99586 7.0 16.0 3.43 0.46 10.0 5 0 red 4.8 90 6.5 0.43 0.28 11.25 0.032 0.9922 31.0 87.0 3.02 0.38 12.4 6 0 white 6.4 91 6.6 0.25 0.3 1.6 0.046 0.993 32.0 134.0 3.42 0.51 10.1 7 1 white 6.0 92 6.9 0.38 0.29 13.65 0.048 0.99784 52.0 189.0 3.0 0.6 9.5 6 0 white 6.0 93 8.6 0.685 0.1 1.6 0.092 0.99745 3.0 12.0 3.31 0.65 9.55 6 0 red 5.4 94 7.5 0.17 0.37 1.5 0.06 0.9936 18.0 75.0 3.54 0.88 10.7 5 0 white 5.4 95 6.1 0.28 0.27 8.0 0.048 0.99498 41.0 162.0 3.21 0.51 9.9 5 0 white 5.2 96 7.3 0.51 0.29 11.3 0.034 0.99683 61.0 224.0 3.14 0.56 9.5 6 0 white 5.2 97 9.4 0.16 0.23 1.6 0.042 0.9942 14.0 67.0 3.07 0.32 9.5 5 0 white 4.8 98 8.0 0.28 0.42 7.1 0.045 0.9959 41.0 169.0 3.17 0.43 10.6 5 0 white 5.6 99 8.0 0.33 0.53 2.5 0.091 0.9976 18.0 80.0 3.37 0.8 9.6 6 0 red 6.2 100 5.3 0.32 0.12 6.6 0.043 0.9937 22.0 141.0 3.36 0.6 10.4 6 0 white 6.8 Rows: 1-100 | Columns: 15Note
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
vDataFrameto thepredict()function, but in this case, it’s essential that the column names of thevDataFramematch 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()KNeighborsRegressormodels 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.
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.
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