verticapy.machine_learning.vertica.neighbors.KNeighborsClassifier¶
- class verticapy.machine_learning.vertica.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 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.- 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
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.
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
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.Balancing the Dataset¶
In VerticaPy, balancing a dataset to address class imbalances is made straightforward through the
balance()function within thepreprocessingmodule. 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, VerticaPy’sbalance()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 verticapy.machine_learning.vertica.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
KNeighborsClassifiermodel:from verticapy.machine_learning.vertica import KNeighborsClassifier
Then we can create the model:
model = KNeighborsClassifier( n_neighbors = 10, p = 2, )
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.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
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.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()
5 6 7 avg_macro avg_weighted avg_micro auc 0.7218114837398377 0.6722378487848784 0.7179235537190086 0.7039909620812416 0.7036328990494615 [null] prc_auc 0.6055638110298862 0.4962590789879965 0.6509673760933948 0.5842634220370925 0.583835438950684 [null] accuracy 0.723338485316847 0.6723338485316847 0.6831530139103554 0.6929417825862957 0.6923310296720806 0.6929417825862957 log_loss 0.247372354179121 0.263088251395399 0.243743081751822 0.25140122910878065 0.25148218022792374 [null] precision 0.5736842105263158 0.5186915887850467 0.5308641975308642 0.541079998947409 0.5404248911413412 0.5394126738794436 recall 0.5265700483091788 0.5045454545454545 0.5863636363636363 0.5391597130727566 0.5394126738794436 0.5394126738794436 f1_score 0.54911838790932 0.5115207373271888 0.5572354211663066 0.5392915154676051 0.5390940667168443 0.5394126738794436 mcc 0.3507605430140015 0.26513735306409725 0.31242486060023666 0.30944091889277847 0.3086106946059533 0.3091190108191654 informedness 0.3424791392182698 0.2633276559506066 0.3193847136470087 0.308397169605295 0.3077123695976155 0.3091190108191655 markedness 0.35924219739721286 0.26695948716841844 0.3056166727783891 0.31060611911467345 0.3096288872481 0.3091190108191655 csi 0.3784722222222222 0.34365325077399383 0.38622754491017963 0.36945100596879854 0.3692697450548967 0.3693121693121693 Rows: 1-11 | Columns: 7Important
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
cutoffto observe the effect on different metrics:model.report(cutoff = 0.2)
5 6 7 avg_macro avg_weighted avg_micro auc 0.7182268468745199 0.6583389868837625 0.7184113865932049 0.6983257401171624 0.6977992963308579 [null] prc_auc 0.6003502432077987 0.4991573952497418 0.6211264008896221 0.5735446797823877 0.5728036677514173 [null] accuracy 0.58603066439523 0.5288461538461539 0.55810147299509 0.5576594304121579 0.5569527713354673 0.5570801317233809 log_loss 0.245790024975802 0.263088251395399 0.249176152265673 0.25268480954562467 0.2528618316457757 [null] precision 0.4477211796246649 0.4177777777777778 0.4397163120567376 0.43507175648639346 0.4347497730870425 0.43418940609951845 recall 0.8186274509803921 0.8545454545454545 0.8493150684931506 0.8408293246729991 0.8413685847589425 0.8413685847589425 f1_score 0.5788561525129983 0.5611940298507463 0.5794392523364486 0.5731631449000644 0.5730117230816455 0.5727898358920064 mcc 0.27778031238447665 0.2195092751581671 0.2542676925091321 0.2505190933505919 0.24983491278496098 0.25016134804760454 informedness 0.28076844314749394 0.20603060306030585 0.2447232317584569 0.24384075932208557 0.24292050206914173 0.24340420816861164 markedness 0.27482398336298264 0.2338697318007663 0.2641843971631206 0.25762603744228985 0.2571878951492076 0.25710607276618513 csi 0.4073170731707317 0.3900414937759336 0.40789473684210525 0.4017511012629235 0.40160304654114415 0.4013353115727003 Rows: 1-11 | Columns: 7You can also use the
KNeighborsClassifier.scorefunction to compute any classification metric. The default metric is the accuracy:model.score(metric = "f1", average = "macro") Out[4]: 0.3530332983456925
Note
For multi-class scoring,
verticapyallows the flexibility to use three averaging techniques:micro,macroandweighted. 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", )
123fixed_acidity123volatile_acidity123density123pH123citric_acid123residual_sugar123chlorides123free_sulfur_dioxide123total_sulfur_dioxide123sulphates123alcohol123quality123goodAbccolor123prediction1 5.0 0.24 0.98774 3.32 0.34 1.1 0.034 49.0 158.0 0.32 13.1 7 1 white 7 2 5.1 0.11 0.99008 3.57 0.32 1.6 0.028 12.0 90.0 0.52 12.2 6 0 white 7 3 5.1 0.585 0.99264 3.56 0.0 1.7 0.044 14.0 86.0 0.94 12.9 7 1 red 7 4 5.2 0.48 0.9927 3.54 0.04 1.6 0.054 19.0 106.0 0.62 12.2 7 1 red 7 5 5.3 0.23 0.99119 3.16 0.56 0.9 0.041 46.0 141.0 0.62 9.7 5 0 white 7 6 5.6 0.18 0.98984 3.51 0.58 1.25 0.034 29.0 129.0 0.6 12.0 7 1 white 6 7 5.6 0.24 0.98981 3.04 0.34 2.0 0.041 14.0 73.0 0.45 11.6 7 1 white 6 8 5.6 0.35 0.9937 3.3 0.14 5.0 0.046 48.0 198.0 0.71 10.3 5 0 white 5 9 5.7 0.24 0.99391 3.11 0.47 6.3 0.069 35.0 182.0 0.46 9.75 5 0 white 7 10 5.7 0.46 0.9932 3.13 0.46 1.4 0.04 31.0 169.0 0.47 8.8 5 0 white 5 11 5.8 0.31 0.98906 3.25 0.32 4.5 0.024 28.0 94.0 0.52 13.7 7 1 white 7 12 5.8 0.31 0.9916 3.18 0.33 1.2 0.036 23.0 99.0 0.6 10.5 6 0 white 5 13 5.8 0.36 0.99212 3.1 0.5 1.0 0.127 63.0 178.0 0.45 9.7 5 0 white 5 14 5.8 0.42 0.989 3.32 0.3 2.2 0.035 26.0 129.0 0.47 12.9 6 0 white 7 15 5.9 0.23 0.99139 3.31 0.24 3.8 0.038 61.0 152.0 0.5 11.3 7 1 white 5 16 5.9 0.24 0.99225 3.39 0.3 2.0 0.033 28.0 92.0 0.69 10.9 7 1 white 7 17 6.0 0.13 0.9948 3.59 0.28 5.7 0.038 56.0 189.5 0.43 10.6 7 1 white 6 18 6.0 0.22 0.98862 3.22 0.28 1.1 0.034 47.0 90.0 0.38 12.6 6 0 white 6 19 6.0 0.23 0.99571 3.05 0.15 9.7 0.048 101.0 207.0 0.3 9.1 5 0 white 6 20 6.0 0.24 0.9946 3.52 0.32 6.3 0.03 34.0 129.0 0.41 10.4 5 0 white 7 21 6.0 0.29 0.99658 3.11 0.21 15.55 0.043 20.0 142.0 0.54 10.1 6 0 white 7 22 6.1 0.21 0.99184 3.22 0.19 1.4 0.046 51.0 131.0 0.39 10.5 5 0 white 5 23 6.1 0.22 0.98966 3.03 0.4 1.85 0.031 25.0 111.0 0.3 11.8 7 1 white 6 24 6.1 0.25 0.9963 3.14 0.18 10.5 0.049 41.0 124.0 0.35 10.5 5 0 white 5 25 6.1 0.32 0.99633 3.42 0.25 2.3 0.071 23.0 58.0 0.97 10.6 5 0 red 5 26 6.2 0.21 0.9933 3.49 0.27 1.7 0.038 41.0 150.0 0.71 10.5 7 1 white 7 27 6.2 0.32 0.9949 3.18 0.16 7.0 0.045 30.0 136.0 0.47 9.6 6 0 white 5 28 6.2 0.35 0.99005 3.1 0.29 3.9 0.041 22.0 79.0 0.59 12.0666666666667 6 0 white 5 29 6.2 0.36 0.98936 3.2 0.32 4.0 0.036 44.0 92.0 0.5 13.3 7 1 white 7 30 6.2 0.6 0.9949 3.45 0.08 2.0 0.09 32.0 44.0 0.58 10.5 5 0 red 6 31 6.3 0.22 0.99204 3.24 0.3 2.0 0.05 23.0 120.0 0.47 10.4 6 0 white 6 32 6.3 0.27 0.9926 3.45 0.38 0.9 0.051 7.0 140.0 0.5 10.5 7 1 white 6 33 6.3 0.27 0.99691 3.18 0.46 11.1 0.053 44.0 177.0 0.67 9.4 5 0 white 5 34 6.3 0.41 0.99274 3.16 0.16 0.9 0.032 25.0 98.0 0.42 9.5 5 0 white 5 35 6.4 0.15 0.99112 3.17 0.4 1.3 0.053 61.0 146.0 0.68 11.0 6 0 white 7 36 6.4 0.17 0.9916 3.46 0.27 1.5 0.037 20.0 98.0 0.42 11.0 7 1 white 6 37 6.4 0.21 0.99278 3.15 0.28 5.9 0.047 29.0 101.0 0.4 11.0 6 0 white 7 38 6.4 0.24 0.9942 3.01 0.49 5.8 0.053 25.0 120.0 0.98 10.5 6 0 white 5 39 6.4 0.28 0.99354 3.1 0.56 1.7 0.156 49.0 106.0 0.37 9.2 6 0 white 5 40 6.4 0.45 0.9905 2.97 0.07 1.1 0.03 10.0 131.0 0.28 10.8 5 0 white 5 41 6.4 0.57 0.99519 3.47 0.12 2.3 0.12 25.0 36.0 0.71 11.3 7 1 red 6 42 6.5 0.16 0.992 3.18 0.33 4.8 0.043 45.0 114.0 0.44 11.2 6 0 white 7 43 6.5 0.23 0.99 3.19 0.39 1.9 0.036 41.0 98.0 0.43 11.9 7 1 white 6 44 6.5 0.25 0.99776 3.2 0.27 17.4 0.064 29.0 140.0 0.49 10.1 6 0 white 6 45 6.5 0.28 0.99111 3.28 0.34 3.6 0.04 29.0 121.0 0.48 12.1 7 1 white 7 46 6.5 0.28 0.9912 3.32 0.29 2.7 0.038 26.0 107.0 0.41 11.6 7 1 white 7 47 6.5 0.32 0.9943 3.03 0.23 8.5 0.051 20.0 138.0 0.42 10.7 5 0 white 5 48 6.5 0.35 0.99692 3.18 0.31 10.2 0.069 58.0 170.0 0.49 9.4 5 0 white 5 49 6.5 0.36 0.9902 3.1 0.49 2.9 0.03 16.0 94.0 0.49 12.1 7 1 white 5 50 6.6 0.2 0.99496 3.11 0.27 10.9 0.038 29.0 130.0 0.44 10.5 7 1 white 6 51 6.6 0.21 0.99294 2.96 0.5 8.7 0.036 41.0 191.0 0.56 11.0 6 0 white 6 52 6.6 0.22 0.99009 3.15 0.37 1.6 0.04 31.0 101.0 0.66 12.0 5 0 white 7 53 6.6 0.22 0.99019 3.17 0.35 1.4 0.05 23.0 83.0 0.48 12.0 7 1 white 7 54 6.6 0.22 0.99906 3.08 0.23 17.3 0.047 37.0 118.0 0.46 8.8 6 0 white 6 55 6.6 0.23 0.99084 3.29 0.32 1.7 0.024 26.0 102.0 0.6 11.8 6 0 white 6 56 6.6 0.23 0.99756 3.33 0.29 14.45 0.057 29.0 144.0 0.54 10.2 6 0 white 6 57 6.6 0.24 0.99381 3.13 0.3 11.3 0.026 11.0 77.0 0.55 12.8 7 1 white 6 58 6.6 0.29 0.9913 3.15 0.39 6.75 0.031 22.0 98.0 0.8 12.9 7 1 white 5 59 6.6 0.3 0.9956 3.18 0.45 8.0 0.038 54.0 200.0 0.48 9.5 5 0 white 5 60 6.6 0.32 0.99198 3.4 0.26 4.6 0.031 26.0 120.0 0.73 12.5 7 1 white 7 61 6.6 0.44 0.99444 3.42 0.09 2.2 0.063 9.0 18.0 0.69 11.3 6 0 red 6 62 6.6 0.64 0.99036 3.11 0.28 4.4 0.032 19.0 78.0 0.62 12.9 6 0 white 5 63 6.7 0.16 0.99666 2.88 0.32 12.5 0.035 18.0 156.0 0.36 9.0 6 0 white 6 64 6.7 0.21 0.9953 3.12 0.42 9.1 0.049 31.0 150.0 0.74 9.9 7 1 white 7 65 6.7 0.28 0.99064 3.26 0.28 2.4 0.012 36.0 100.0 0.39 11.7 7 1 red 7 66 6.7 0.37 0.9953 3.16 0.41 6.3 0.061 22.0 149.0 0.47 9.6 6 0 white 5 67 6.7 0.45 0.99122 3.12 0.3 5.3 0.036 27.0 165.0 0.46 12.2 6 0 white 6 68 6.7 0.48 0.9889 3.15 0.32 1.4 0.021 22.0 121.0 0.53 12.7 7 1 white 5 69 6.8 0.16 0.9923 3.49 0.4 2.3 0.037 18.0 102.0 0.42 11.4 7 1 white 6 70 6.8 0.16 0.99518 3.2 0.29 10.4 0.046 59.0 143.0 0.4 10.8 6 0 white 6 71 6.8 0.18 0.99498 3.42 0.32 7.2 0.047 17.0 109.0 0.44 10.4 6 0 white 6 72 6.8 0.21 1.0 3.27 0.36 18.1 0.046 32.0 133.0 0.48 8.8 5 0 white 5 73 6.8 0.22 0.9918 3.24 0.31 6.3 0.035 33.0 170.0 0.66 12.6 6 0 white 6 74 6.8 0.23 0.9988 3.18 0.39 16.1 0.053 71.0 194.0 0.64 10.2 6 0 white 6 75 6.8 0.24 1.00055 3.0 0.49 19.3 0.057 55.0 247.0 0.56 8.7 5 0 white 6 76 6.8 0.25 0.99402 3.53 0.24 1.6 0.045 39.0 164.0 0.58 10.8 5 0 white 6 77 6.8 0.32 0.99061 3.33 0.3 3.3 0.029 15.0 80.0 0.63 12.6 7 1 white 7 78 6.8 0.33 0.99226 3.06 0.31 7.4 0.045 34.0 143.0 0.55 12.2 6 0 white 5 79 6.8 0.36 0.9928 3.36 0.32 1.8 0.067 4.0 8.0 0.55 12.8 7 1 red 7 80 6.8 0.39 0.99212 3.18 0.34 7.4 0.02 38.0 133.0 0.44 12.0 7 1 white 5 81 6.9 0.19 0.99315 3.21 0.35 1.7 0.036 33.0 101.0 0.54 10.8 7 1 white 6 82 6.9 0.25 0.9948 3.16 0.26 5.2 0.024 36.0 135.0 0.72 10.7 7 1 white 6 83 6.9 0.28 0.98882 2.98 0.24 2.1 0.034 49.0 121.0 0.43 13.2 7 1 white 5 84 6.9 0.34 0.99165 3.36 0.3 4.7 0.029 34.0 148.0 0.49 12.3 7 1 white 7 85 7.0 0.2 0.9928 3.19 0.34 5.7 0.035 32.0 83.0 0.46 11.5 6 0 white 6 86 7.0 0.23 0.9912 3.16 0.35 1.4 0.036 31.0 113.0 0.48 10.8 7 1 white 7 87 7.0 0.23 0.99372 3.18 0.28 2.7 0.053 16.0 92.0 0.56 9.3 5 0 white 5 88 7.0 0.24 0.98988 3.16 0.35 1.0 0.032 42.0 104.0 0.37 11.7 7 1 white 6 89 7.0 0.24 0.99 3.06 0.34 1.4 0.031 27.0 107.0 0.39 11.9 6 0 white 7 90 7.0 0.34 0.9937 3.01 0.1 3.5 0.044 17.0 63.0 0.39 9.2 5 0 white 5 91 7.0 0.49 0.9974 3.34 0.49 5.6 0.06 26.0 121.0 0.76 10.5 5 0 red 5 92 7.0 0.55 0.9959 3.36 0.13 2.2 0.075 15.0 35.0 0.59 9.7 6 0 red 5 93 7.1 0.22 0.9899 3.15 0.33 2.8 0.033 48.0 153.0 0.38 12.7 7 1 white 7 94 7.1 0.34 0.99 3.3 0.49 1.5 0.027 26.0 126.0 0.33 12.2 7 1 white 7 95 7.1 0.35 0.9897 3.26 0.27 3.1 0.034 28.0 134.0 0.38 13.1 7 1 white 7 96 7.1 0.365 0.9941 3.15 0.14 1.2 0.055 24.0 84.0 0.43 8.9 5 0 white 5 97 7.1 0.46 0.99624 3.36 0.14 2.8 0.076 15.0 37.0 0.49 10.7 5 0 red 5 98 7.2 0.18 0.9919 3.14 0.41 1.2 0.048 41.0 97.0 0.45 10.4 5 0 white 7 99 7.2 0.23 0.99166 3.18 0.39 1.5 0.053 26.0 106.0 0.47 11.1 6 0 white 6 100 7.2 0.24 0.9958 3.17 0.19 7.7 0.045 53.0 176.0 0.38 9.5 5 0 white 6 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.Probabilities¶
It is also easy to get the model’s probabilities:
model.predict_proba( test, [ "fixed_acidity", "volatile_acidity", "density", "pH", ], "prediction", )
123fixed_acidity123volatile_acidity123density123pH123citric_acid123residual_sugar123chlorides123free_sulfur_dioxide123total_sulfur_dioxide123sulphates123alcohol123quality123goodAbccolor123prediction123prediction_5123prediction_6123prediction_71 4.2 0.17 0.98999 3.65 0.36 1.8 0.029 93.0 161.0 0.89 12.0 7 1 white 7 0.4 0.2 0.4 2 4.7 0.145 0.9908 3.76 0.29 1.0 0.042 35.0 90.0 0.49 11.3 6 0 white 7 0.1 0.3 0.6 3 5.0 0.255 0.99238 3.75 0.22 2.7 0.043 46.0 153.0 0.76 11.3 6 0 white 7 0.0 0.3 0.7 4 5.2 0.48 0.9927 3.54 0.04 1.6 0.054 19.0 106.0 0.62 12.2 7 1 red 7 0.0 0.1 0.9 5 5.4 0.15 0.98878 3.04 0.32 2.5 0.037 10.0 51.0 0.58 12.6 6 0 white 6 0.4 0.3 0.3 6 5.6 0.18 0.9954 3.49 0.3 10.2 0.028 28.0 131.0 0.42 10.8 7 1 white 7 0.1 0.5 0.4 7 5.6 0.49 0.9907 3.42 0.13 4.5 0.039 17.0 116.0 0.9 13.7 7 1 white 7 0.4 0.3 0.3 8 5.6 0.54 0.9942 3.72 0.04 1.7 0.049 5.0 13.0 0.58 11.4 5 0 red 5 0.4 0.2 0.4 9 5.8 0.19 0.9935 3.34 0.49 4.9 0.04 44.0 118.0 0.38 9.5 7 1 white 7 0.2 0.2 0.6 10 5.8 0.19 0.99362 3.77 0.24 1.3 0.044 38.0 128.0 0.6 10.6 5 0 white 5 0.5 0.0 0.5 11 5.8 0.31 0.98906 3.25 0.32 4.5 0.024 28.0 94.0 0.52 13.7 7 1 white 7 0.0 0.4 0.6 12 5.8 0.35 0.9912 3.35 0.29 3.2 0.034 41.0 151.0 0.58 11.6333333333333 7 1 white 7 0.1 0.2 0.7 13 5.8 0.36 0.99212 3.1 0.5 1.0 0.127 63.0 178.0 0.45 9.7 5 0 white 5 0.4 0.2 0.4 14 5.9 0.19 0.9897 3.09 0.37 0.8 0.027 3.0 21.0 0.31 10.8 5 0 white 7 0.2 0.2 0.6 15 5.9 0.19 0.99341 3.32 0.21 1.7 0.045 57.0 135.0 0.44 9.5 5 0 white 7 0.4 0.1 0.5 16 5.9 0.2 0.99426 3.31 0.28 12.8 0.038 29.0 132.0 0.57 11.8 7 1 white 5 0.5 0.1 0.4 17 5.9 0.32 0.98964 3.22 0.28 4.7 0.039 34.0 94.0 0.57 13.1 7 1 white 5 0.6 0.2 0.2 18 5.9 0.34 0.98892 3.4 0.31 2.0 0.03 38.0 142.0 0.41 12.9 7 1 white 7 0.1 0.3 0.6 19 5.9 0.42 0.99184 3.25 0.36 2.4 0.034 19.0 77.0 0.48 10.9 5 0 white 5 0.6 0.2 0.2 20 6.0 0.13 0.9948 3.59 0.28 5.7 0.038 56.0 189.5 0.43 10.6 7 1 white 6 0.1 0.4 0.5 21 6.0 0.22 0.98862 3.22 0.28 1.1 0.034 47.0 90.0 0.38 12.6 6 0 white 6 0.2 0.5 0.3 22 6.0 0.25 0.99398 3.16 0.4 5.7 0.052 56.0 152.0 0.88 10.5 6 0 white 7 0.3 0.3 0.4 23 6.0 0.34 0.9951 3.25 0.24 5.4 0.06 23.0 126.0 0.44 9.0 7 1 white 7 0.2 0.4 0.4 24 6.1 0.32 0.99633 3.42 0.25 2.3 0.071 23.0 58.0 0.97 10.6 5 0 red 7 0.2 0.2 0.6 25 6.1 0.35 0.9934 3.23 0.07 1.4 0.069 22.0 108.0 0.52 9.2 5 0 white 5 0.6 0.2 0.2 26 6.2 0.15 0.993 3.38 0.46 1.6 0.039 38.0 123.0 0.51 9.7 6 0 white 7 0.1 0.4 0.5 27 6.2 0.7 0.99622 3.54 0.15 5.1 0.076 13.0 27.0 0.6 11.9 6 0 red 6 0.6 0.4 0.0 28 6.3 0.22 0.98998 3.14 0.28 2.4 0.042 38.0 102.0 0.37 11.6 7 1 white 6 0.3 0.6 0.1 29 6.3 0.25 0.9968 3.18 0.44 11.6 0.041 48.0 195.0 0.52 9.5 5 0 white 6 0.4 0.3 0.3 30 6.3 0.27 0.9936 3.28 0.23 2.9 0.047 13.0 100.0 0.43 9.8 5 0 white 5 0.3 0.6 0.1 31 6.3 0.3 0.9959 3.44 0.48 1.8 0.069 18.0 61.0 0.78 10.3 6 0 red 7 0.0 0.4 0.6 32 6.4 0.22 0.98958 3.18 0.34 1.4 0.023 56.0 115.0 0.7 11.7 6 0 white 6 0.1 0.5 0.4 33 6.5 0.08 0.991 3.34 0.33 1.9 0.028 23.0 93.0 0.7 12.0 7 1 white 7 0.1 0.1 0.8 34 6.5 0.24 0.98928 3.25 0.28 1.1 0.034 26.0 83.0 0.33 12.3 6 0 white 7 0.2 0.3 0.5 35 6.5 0.25 0.99776 3.2 0.27 17.4 0.064 29.0 140.0 0.49 10.1 6 0 white 6 0.3 0.4 0.3 36 6.5 0.25 0.998 3.58 0.35 12.0 0.055 47.0 179.0 0.47 10.0 5 0 white 7 0.3 0.3 0.4 37 6.5 0.28 0.99074 3.17 0.25 4.8 0.029 54.0 128.0 0.44 12.2 7 1 white 6 0.3 0.6 0.1 38 6.5 0.35 0.99692 3.18 0.31 10.2 0.069 58.0 170.0 0.49 9.4 5 0 white 5 0.4 0.3 0.3 39 6.6 0.2 0.99496 3.11 0.27 10.9 0.038 29.0 130.0 0.44 10.5 7 1 white 6 0.0 0.5 0.5 40 6.6 0.22 0.99906 3.08 0.23 17.3 0.047 37.0 118.0 0.46 8.8 6 0 white 6 0.2 0.6 0.2 41 6.6 0.23 0.99084 3.29 0.32 1.7 0.024 26.0 102.0 0.6 11.8 6 0 white 6 0.3 0.5 0.2 42 6.6 0.24 0.99381 3.13 0.3 11.3 0.026 11.0 77.0 0.55 12.8 7 1 white 6 0.3 0.4 0.3 43 6.6 0.26 0.9981 3.11 0.56 15.4 0.053 32.0 141.0 0.49 9.3 5 0 white 5 0.4 0.4 0.2 44 6.6 0.56 0.99397 3.42 0.14 2.4 0.064 13.0 29.0 0.62 11.7 7 1 red 6 0.4 0.4 0.2 45 6.6 0.64 0.99036 3.11 0.28 4.4 0.032 19.0 78.0 0.62 12.9 6 0 white 5 0.6 0.4 0.0 46 6.7 0.19 0.98912 3.31 0.39 1.0 0.032 14.0 71.0 0.38 13.0 7 1 white 6 0.0 0.6 0.4 47 6.7 0.19 0.99173 2.9 0.32 3.7 0.041 26.0 76.0 0.57 10.5 7 1 white 6 0.0 0.6 0.4 48 6.7 0.21 0.98949 3.24 0.34 1.5 0.035 45.0 123.0 0.36 12.6 7 1 white 7 0.0 0.5 0.5 49 6.7 0.24 0.99802 3.42 0.26 12.6 0.053 44.0 182.0 0.42 9.7 5 0 white 7 0.3 0.3 0.4 50 6.7 0.3 0.9974 3.04 0.5 12.1 0.045 38.0 127.0 0.53 8.9 6 0 white 6 0.2 0.4 0.4 51 6.7 0.31 0.98867 3.09 0.3 2.4 0.038 30.0 83.0 0.36 12.8 7 1 white 6 0.3 0.5 0.2 52 6.7 0.34 0.9982 3.19 0.3 15.6 0.054 51.0 196.0 0.49 9.3 5 0 white 5 0.6 0.2 0.2 53 6.7 0.35 0.99188 3.13 0.32 9.0 0.032 29.0 113.0 0.65 12.9 7 1 white 7 0.2 0.4 0.4 54 6.8 0.17 0.993 3.0 0.17 5.1 0.049 26.0 82.0 0.38 9.8 6 0 white 7 0.0 0.3 0.7 55 6.8 0.21 1.0 3.27 0.36 18.1 0.046 32.0 133.0 0.48 8.8 5 0 white 6 0.4 0.4 0.2 56 6.8 0.22 0.9918 3.24 0.31 6.3 0.035 33.0 170.0 0.66 12.6 6 0 white 6 0.4 0.6 0.0 57 6.8 0.22 0.9984 3.44 0.3 13.6 0.055 50.0 180.0 0.39 9.8 5 0 white 6 0.1 0.6 0.3 58 6.8 0.23 0.9988 3.18 0.39 16.1 0.053 71.0 194.0 0.64 10.2 6 0 white 6 0.4 0.5 0.1 59 6.8 0.3 0.9912 3.09 0.35 2.8 0.038 10.0 164.0 0.53 12.0 6 0 white 6 0.5 0.4 0.1 60 6.8 0.36 0.9928 3.36 0.32 1.8 0.067 4.0 8.0 0.55 12.8 7 1 red 7 0.2 0.1 0.7 61 6.8 0.4 0.9904 3.12 0.29 2.8 0.044 27.0 97.0 0.42 11.2 6 0 white 7 0.4 0.2 0.4 62 6.8 0.59 0.9962 3.41 0.06 6.0 0.06 11.0 18.0 0.59 10.8 7 1 red 6 0.3 0.5 0.2 63 6.8 0.69 0.9997 3.46 0.0 5.6 0.124 21.0 58.0 0.72 10.2 5 0 red 6 0.2 0.6 0.2 64 6.9 0.18 0.9934 3.27 0.36 1.3 0.036 40.0 117.0 0.95 9.5 7 1 white 7 0.1 0.3 0.6 65 6.9 0.2 0.992 3.38 0.37 6.2 0.027 24.0 97.0 0.49 12.2 7 1 white 5 0.4 0.4 0.2 66 6.9 0.25 0.9948 3.16 0.26 5.2 0.024 36.0 135.0 0.72 10.7 7 1 white 6 0.3 0.6 0.1 67 6.9 0.33 0.99226 3.11 0.31 7.7 0.04 29.0 135.0 0.57 12.3 5 0 white 5 0.6 0.3 0.1 68 6.9 0.38 0.9932 3.38 0.32 8.5 0.044 36.0 152.0 0.35 12.0 7 1 white 7 0.2 0.3 0.5 69 6.9 0.43 0.99594 3.17 0.28 9.4 0.056 29.0 183.0 0.43 9.4 5 0 white 5 0.5 0.2 0.3 70 6.9 0.52 0.99685 3.46 0.25 2.6 0.081 10.0 37.0 0.5 11.0 5 0 red 5 0.4 0.3 0.3 71 7.0 0.14 0.9956 3.22 0.32 9.0 0.039 54.0 141.0 0.43 9.4 6 0 white 6 0.2 0.5 0.3 72 7.0 0.16 0.9936 3.08 0.73 1.0 0.138 58.0 150.0 0.3 9.2 5 0 white 7 0.1 0.5 0.4 73 7.0 0.16 0.998 2.91 0.25 14.3 0.044 27.0 149.0 0.46 9.2 6 0 white 7 0.1 0.3 0.6 74 7.0 0.24 0.99436 3.2 0.3 6.7 0.039 37.0 125.0 0.39 9.9 5 0 white 7 0.4 0.4 0.2 75 7.0 0.36 0.99558 3.4 0.21 2.3 0.086 20.0 65.0 0.54 10.1 6 0 red 7 0.3 0.2 0.5 76 7.0 0.49 0.9974 3.34 0.49 5.6 0.06 26.0 121.0 0.76 10.5 5 0 red 5 0.5 0.5 0.0 77 7.1 0.17 0.9958 3.35 0.38 7.4 0.052 49.0 182.0 0.52 9.6 6 0 white 6 0.2 0.4 0.4 78 7.1 0.26 0.9986 3.07 0.34 14.4 0.067 35.0 189.0 0.53 9.1 7 1 white 7 0.2 0.1 0.7 79 7.1 0.28 0.99069 3.15 0.31 1.5 0.053 20.0 98.0 0.5 11.4 5 0 white 5 0.6 0.2 0.2 80 7.1 0.35 0.9897 3.26 0.27 3.1 0.034 28.0 134.0 0.38 13.1 7 1 white 7 0.2 0.2 0.6 81 7.1 0.44 0.9896 3.07 0.37 2.7 0.041 35.0 128.0 0.43 13.5 7 1 white 7 0.4 0.1 0.5 82 7.1 0.75 0.99242 3.39 0.01 2.2 0.059 11.0 18.0 0.4 12.8 6 0 red 5 0.6 0.3 0.1 83 7.2 0.16 0.98958 3.12 0.29 1.0 0.031 40.0 123.0 0.4 12.1 7 1 white 5 0.3 0.2 0.5 84 7.2 0.16 0.9922 3.27 0.32 0.8 0.04 50.0 121.0 0.33 10.0 6 0 white 5 0.3 0.3 0.4 85 7.2 0.18 0.9925 3.32 0.31 1.1 0.045 20.0 73.0 0.4 10.8 7 1 white 5 0.3 0.5 0.2 86 7.2 0.22 0.99196 3.25 0.24 1.4 0.041 17.0 159.0 0.53 11.2 6 0 white 5 0.6 0.1 0.3 87 7.2 0.23 0.9979 2.98 0.39 14.2 0.058 49.0 192.0 0.48 9.0 7 1 white 6 0.0 0.4 0.6 88 7.2 0.24 0.99411 3.36 0.29 3.0 0.036 17.0 117.0 0.68 10.1 6 0 white 6 0.2 0.5 0.3 89 7.2 0.24 0.9958 3.17 0.19 7.7 0.045 53.0 176.0 0.38 9.5 5 0 white 6 0.3 0.5 0.2 90 7.2 0.25 0.9999 2.97 0.39 18.95 0.038 42.0 155.0 0.47 9.0 6 0 white 7 0.1 0.3 0.6 91 7.2 0.29 0.9977 3.08 0.4 13.6 0.045 66.0 231.0 0.59 9.6 6 0 white 7 0.3 0.4 0.3 92 7.2 0.32 0.99568 3.2 0.4 8.7 0.038 45.0 154.0 0.47 10.4 6 0 white 5 0.6 0.2 0.2 93 7.2 0.37 0.9903 3.3 0.15 2.0 0.029 27.0 87.0 0.59 12.6 7 1 white 7 0.1 0.1 0.8 94 7.2 0.61 0.99641 3.25 0.08 4.0 0.082 26.0 108.0 0.51 9.4 5 0 red 6 0.5 0.4 0.1 95 7.3 0.25 0.9911 3.29 0.41 1.8 0.037 52.0 165.0 0.39 12.2 7 1 white 7 0.2 0.3 0.5 96 7.3 0.25 0.9986 3.04 0.36 13.1 0.05 35.0 200.0 0.46 8.9 7 1 white 7 0.0 0.4 0.6 97 7.3 0.26 1.0 3.06 0.33 17.85 0.049 41.5 195.0 0.44 9.1 7 1 white 7 0.0 0.5 0.5 98 7.3 0.4 0.9969 3.41 0.3 1.7 0.08 33.0 79.0 0.65 9.5 6 0 red 5 0.7 0.1 0.2 99 7.3 0.45 0.9978 3.33 0.36 5.9 0.074 12.0 87.0 0.83 10.5 5 0 red 5 0.5 0.2 0.3 100 7.4 0.13 0.995 3.36 0.39 4.7 0.042 36.0 137.0 0.56 10.3 7 1 white 7 0.1 0.3 0.6 Rows: 1-100 | Columns: 18Note
Probabilities are added to the
vDataFrame, and VerticaPy uses the corresponding probability function in SQL behind the scenes. You can use thepos_labelparameter 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) Out[5]: array([[108, 49, 50], [ 47, 113, 60], [ 34, 52, 134]])
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_labeland 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) Out[6]: array([[375, 9], [178, 27]])
Note
In classification, the
cutoffis 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 thenbins, 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() Out[7]: {'n_neighbors': 10, 'p': 2}
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¶
Must be overridden in the child class
Methods
__init__([name, overwrite_model, n_neighbors, p])Must be overridden in the child class
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.
does_model_exists(name[, raise_error, ...])Checks whether the model is stored in the Vertica database.
drop()KNeighborsClassifiermodels 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.
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.
register(registered_name[, raise_error])Registers the model and adds it to in-DB Model versioning environment with a status of 'under_review'.
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.
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