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
pof thep-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
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
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 fromvastorbitare 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()
123fixed_acidityDecimal(6, 3)123volatile_acidityDecimal(7, 4)123citric_acidDecimal(6, 3)123residual_sugarDecimal(7, 3)123chloridesDouble123free_sulfur_dioxideDecimal(7, 2)123total_sulfur_dioxideDecimal(7, 2)123densityDouble123phDecimal(6, 3)123sulphatesDecimal(6, 3)123alcoholDouble123qualityInteger123goodIntegerAbccolorVarchar(20)1 6.3 0.67 0.48 12.6 0.052 57.0 222.0 0.9979 3.17 0.52 9.3 6 0 white 2 7.4 0.4 0.29 5.4 0.044 31.0 122.0 0.994 3.3 0.5 11.1 8 1 white 3 7.1 0.26 0.31 2.2 0.044 29.0 128.0 0.9937 3.34 0.64 10.9 8 1 white 4 9.0 0.31 0.48 6.6 0.043 11.0 73.0 0.9938 2.9 0.38 11.6 5 0 white 5 6.3 0.39 0.24 6.9 0.069 9.0 117.0 0.9942 3.15 0.35 10.2 4 0 white 6 8.2 0.22 0.36 6.8 0.034 12.0 90.0 0.9944 3.01 0.38 10.5 8 1 white 7 7.1 0.19 0.28 3.6 0.033 16.0 78.0 0.993 2.91 0.78 11.4 6 0 white 8 7.3 0.25 0.36 13.1 0.05 35.0 200.0 0.9986 3.04 0.46 8.9 7 1 white 9 7.9 0.2 0.34 1.2 0.04 29.0 118.0 0.9932 3.14 0.41 10.6 6 0 white 10 7.1 0.26 0.32 5.9 0.037 39.0 97.0 0.9934 3.31 0.4 11.6 6 0 white 11 7.0 0.2 0.34 5.7 0.035 32.0 83.0 0.9928 3.19 0.46 11.5 6 0 white 12 6.9 0.3 0.33 4.1 0.035 26.0 155.0 0.9925 3.25 0.79 12.3 8 1 white 13 8.1 0.29 0.49 7.1 0.042 22.0 124.0 0.9944 3.14 0.41 10.8 6 0 white 14 5.8 0.17 0.3 1.4 0.037 55.0 130.0 0.9909 3.29 0.38 11.3 6 0 white 15 5.9 0.415 0.02 0.8 0.038 22.0 63.0 0.9932 3.36 0.36 9.3 5 0 white 16 6.6 0.23 0.26 1.3 0.045 16.0 128.0 0.9934 3.36 0.6 10.0 6 0 white 17 8.6 0.55 0.35 15.55 0.057 35.5 366.5 1.0001 3.04 0.63 11.0 3 0 white 18 6.9 0.35 0.74 1.0 0.044 18.0 132.0 0.992 3.13 0.55 10.2 5 0 white 19 7.6 0.14 0.74 1.6 0.04 27.0 103.0 0.9916 3.07 0.4 10.8 7 1 white 20 9.2 0.28 0.49 11.8 0.042 29.0 137.0 0.998 3.1 0.34 10.1 4 0 white 21 6.2 0.18 0.49 4.5 0.047 17.0 90.0 0.9919 3.27 0.37 11.6 6 0 white 22 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 23 9.8 0.25 0.74 10.0 0.056 36.0 225.0 0.9977 3.06 0.43 10.0 4 0 white 24 8.1 0.29 0.49 7.1 0.042 22.0 124.0 0.9944 3.14 0.41 10.8 6 0 white 25 6.8 0.22 0.49 0.9 0.052 26.0 128.0 0.991 3.25 0.35 11.4 6 0 white 26 7.2 0.22 0.49 1.0 0.045 34.0 140.0 0.99 3.05 0.34 12.7 6 0 white 27 7.4 0.25 0.49 1.1 0.042 35.0 156.0 0.9917 3.13 0.55 11.3 5 0 white 28 8.2 0.18 0.49 1.1 0.033 28.0 81.0 0.9923 3.0 0.68 10.4 7 1 white 29 6.1 0.22 0.49 1.5 0.051 18.0 87.0 0.9928 3.3 0.46 9.6 5 0 white 30 7.0 0.39 0.24 1.0 0.048 8.0 119.0 0.9923 3.0 0.31 10.1 4 0 white 31 6.1 0.22 0.49 1.5 0.051 18.0 87.0 0.9928 3.3 0.46 9.6 5 0 white 32 6.5 0.36 0.49 2.9 0.03 16.0 94.0 0.9902 3.1 0.49 12.1 7 1 white 33 7.1 0.29 0.49 1.2 0.031 32.0 99.0 0.9893 3.07 0.33 12.2 6 0 white 34 7.4 0.25 0.49 1.1 0.042 35.0 156.0 0.9917 3.13 0.55 11.3 5 0 white 35 6.9 0.23 0.24 14.2 0.053 19.0 94.0 0.9982 3.17 0.5 9.6 5 0 white 36 8.5 0.56 0.74 17.85 0.051 51.0 243.0 1.0005 2.99 0.7 9.2 5 0 white 37 8.2 0.18 0.49 1.1 0.033 28.0 81.0 0.9923 3.0 0.68 10.4 7 1 white 38 6.3 0.23 0.49 7.1 0.05 67.0 210.0 0.9951 3.23 0.34 9.5 5 0 white 39 6.1 0.25 0.49 7.6 0.052 67.0 226.0 0.9956 3.16 0.47 8.9 5 0 white 40 7.2 0.26 0.74 13.6 0.05 56.0 162.0 0.998 3.03 0.44 8.8 5 0 white 41 7.2 0.31 0.24 1.4 0.057 17.0 117.0 0.9928 3.16 0.35 10.5 5 0 white 42 8.0 0.25 0.49 1.2 0.061 27.0 117.0 0.9938 3.08 0.34 9.4 5 0 white 43 7.0 0.18 0.49 5.3 0.04 34.0 125.0 0.9914 3.24 0.4 12.2 6 0 white 44 7.8 0.43 0.49 13.0 0.033 37.0 158.0 0.9955 3.14 0.35 11.3 6 0 white 45 8.3 0.2 0.74 4.45 0.044 33.0 130.0 0.9924 3.25 0.42 12.2 6 0 white 46 6.3 0.27 0.49 1.2 0.063 35.0 92.0 0.9911 3.38 0.42 12.2 6 0 white 47 7.4 0.16 0.49 1.2 0.055 18.0 150.0 0.9917 3.23 0.47 11.2 6 0 white 48 7.4 0.16 0.49 1.2 0.055 18.0 150.0 0.9917 3.23 0.47 11.2 6 0 white 49 6.9 0.19 0.49 6.6 0.036 49.0 172.0 0.9932 3.2 0.27 11.5 6 0 white 50 7.8 0.43 0.49 13.0 0.033 37.0 158.0 0.9955 3.14 0.35 11.3 6 0 white 51 7.2 0.4 0.49 1.1 0.048 11.0 138.0 0.9929 3.01 0.42 9.3 5 0 white 52 7.8 0.43 0.49 13.0 0.033 37.0 158.0 0.9955 3.14 0.35 11.3 6 0 white 53 7.6 0.52 0.49 14.0 0.034 37.0 156.0 0.9958 3.14 0.38 11.8 7 1 white 54 8.3 0.21 0.49 19.8 0.054 50.0 231.0 1.0012 2.99 0.54 9.2 5 0 white 55 6.9 0.34 0.74 11.2 0.069 44.0 150.0 0.9968 3.0 0.81 9.2 5 0 white 56 6.3 0.27 0.49 1.2 0.063 35.0 92.0 0.9911 3.38 0.42 12.2 6 0 white 57 8.3 0.2 0.74 4.45 0.044 33.0 130.0 0.9924 3.25 0.42 12.2 6 0 white 58 7.1 0.22 0.74 2.7 0.044 42.0 144.0 0.991 3.31 0.41 12.2 6 0 white 59 7.9 0.11 0.49 4.5 0.048 27.0 133.0 0.9946 3.24 0.42 10.6 6 0 white 60 8.5 0.17 0.74 3.6 0.05 29.0 128.0 0.9928 3.28 0.4 12.4 6 0 white 61 6.4 0.145 0.49 5.4 0.048 54.0 164.0 0.9946 3.56 0.44 10.8 6 0 white 62 7.4 0.16 0.49 1.2 0.055 18.0 150.0 0.9917 3.23 0.47 11.2 6 0 white 63 8.3 0.19 0.49 1.2 0.051 11.0 137.0 0.9918 3.06 0.46 11.0 6 0 white 64 8.0 0.44 0.49 9.1 0.031 46.0 151.0 0.9926 3.16 0.27 12.7 8 1 white 65 7.0 0.2 0.74 0.8 0.044 19.0 163.0 0.9931 3.46 0.53 10.2 5 0 white 66 6.9 0.19 0.49 6.6 0.036 49.0 172.0 0.9932 3.2 0.27 11.5 6 0 white 67 7.1 0.25 0.49 3.0 0.03 30.0 96.0 0.9903 3.13 0.39 12.3 7 1 white 68 6.5 0.24 0.24 1.6 0.046 15.0 60.0 0.9928 3.19 0.39 9.8 5 0 white 69 7.2 0.4 0.49 1.1 0.048 11.0 138.0 0.9929 3.01 0.42 9.3 5 0 white 70 7.6 0.52 0.49 14.0 0.034 37.0 156.0 0.9958 3.14 0.38 11.8 7 1 white 71 7.8 0.43 0.49 13.0 0.033 37.0 158.0 0.9955 3.14 0.35 11.3 6 0 white 72 7.8 0.21 0.49 1.35 0.052 6.0 48.0 0.9911 3.15 0.28 11.4 5 0 white 73 7.0 0.2 0.49 5.9 0.038 39.0 128.0 0.9938 3.21 0.48 10.8 6 0 white 74 6.9 0.25 0.24 3.6 0.057 13.0 85.0 0.9942 2.99 0.48 9.5 4 0 white 75 7.2 0.08 0.49 1.3 0.05 18.0 148.0 0.9945 3.46 0.44 10.2 6 0 white 76 7.1 0.85 0.49 8.7 0.028 40.0 184.0 0.9962 3.22 0.36 10.7 5 0 white 77 7.6 0.51 0.24 1.2 0.04 10.0 104.0 0.992 3.05 0.29 10.8 6 0 white 78 7.9 0.22 0.24 4.6 0.044 39.0 159.0 0.9927 2.99 0.28 11.5 6 0 white 79 7.7 0.16 0.49 2.0 0.056 20.0 124.0 0.9948 3.32 0.49 10.7 6 0 white 80 7.2 0.08 0.49 1.3 0.05 18.0 148.0 0.9945 3.46 0.44 10.2 6 0 white 81 6.6 0.25 0.24 1.7 0.048 26.0 124.0 0.9942 3.37 0.6 10.1 6 0 white 82 6.7 0.16 0.49 2.4 0.046 57.0 187.0 0.9952 3.62 0.81 10.4 6 0 white 83 6.9 0.25 0.24 3.6 0.057 13.0 85.0 0.9942 2.99 0.48 9.5 4 0 white 84 7.5 0.32 0.24 4.6 0.053 8.0 134.0 0.9958 3.14 0.5 9.1 3 0 white 85 7.4 0.28 0.49 1.5 0.034 20.0 126.0 0.9918 2.98 0.39 10.6 6 0 white 86 6.2 0.15 0.49 0.9 0.033 17.0 51.0 0.9932 3.3 0.7 9.4 6 0 white 87 6.7 0.25 0.74 19.4 0.054 44.0 169.0 1.0004 3.51 0.45 9.8 6 0 white 88 6.5 0.26 0.74 13.3 0.044 68.0 224.0 0.9972 3.18 0.54 9.5 6 0 white 89 7.9 0.16 0.74 17.85 0.037 52.0 187.0 0.9998 2.99 0.41 9.3 5 0 white 90 5.6 0.185 0.49 1.1 0.03 28.0 117.0 0.9918 3.55 0.45 10.3 6 0 white 91 7.5 0.2 0.49 1.3 0.031 8.0 97.0 0.9918 3.06 0.62 11.1 5 0 white 92 8.0 0.3 0.49 9.4 0.046 47.0 188.0 0.9964 3.14 0.48 10.0 5 0 white 93 8.0 0.34 0.49 9.0 0.033 39.0 180.0 0.9936 3.13 0.38 12.3 8 1 white 94 7.7 0.35 0.49 8.65 0.033 42.0 186.0 0.9931 3.14 0.38 12.4 8 1 white 95 7.6 0.29 0.49 9.6 0.03 45.0 197.0 0.9938 3.13 0.38 12.3 7 1 white 96 6.7 0.62 0.24 1.1 0.039 6.0 62.0 0.9934 3.41 0.32 10.4 5 0 white 97 6.8 0.27 0.49 1.2 0.044 35.0 126.0 0.99 3.13 0.48 12.1 7 1 white 98 7.7 0.27 0.49 1.8 0.041 23.0 86.0 0.9914 3.16 0.42 12.5 6 0 white 99 6.7 0.51 0.24 2.1 0.043 14.0 155.0 0.9904 3.22 0.6 13.0 6 0 white 100 7.4 0.19 0.49 9.3 0.03 26.0 132.0 0.994 2.99 0.32 11.0 7 1 white Rows: 1-100 | Columns: 14Note
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 intotablesortemporary 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 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, vastorbit’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 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
KNeighborsClassifiermodel: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
VastFrameor the name of the relation stored in the database. The test set is optional and is only used to compute the test metrics. Invastorbit, 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 0 0 0.0 0.0 [null] prc_auc 0.5 0.5 0.6658841940532081 0.5552947313510693 0.5550351316733648 [null] accuracy 0.6885758998435054 0.6431924882629108 0.3317683881064163 0.5545122587376108 0.554005304650018 0.5545122587376109 log_loss [null] [null] 138.47941333921926 138.47941333921926 45.94309174947493 [null] precision 0.0 0.0 0.3317683881064163 0.11058946270213876 0.11007026334672967 0.3317683881064163 recall 0.0 0.0 1.0 0.3333333333333333 0.3317683881064163 0.3317683881064163 f1_score 0.0 0.0 0.49823736780258526 0.16607912260086174 0.16529940841024737 0.3317683881064163 mcc 0.0 0.0 0.0 0.0 0.0 -0.002347417840375587 informedness 0.0 0.0 0.0 0.0 0.0 -0.0023474178403756207 markedness -0.3114241001564946 -0.35680751173708924 -0.6682316118935837 -0.4454877412623892 -0.44599469534998204 -0.0023474178403756207 csi 0.0 0.0 0.3317683881064163 0.11058946270213876 0.11007026334672967 0.19887429643527205 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 0 0 0.0 0.0 [null] prc_auc 0.5 0.5 0.6658841940532081 0.5552947313510693 0.6658841940532081 [null] accuracy nan nan 0.3317683881064163 0.3317683881064163 0.33176838810641635 0.3317683881064163 log_loss [null] [null] 138.4794133392193 138.4794133392193 138.4794133392193 [null] precision 0.0 0.0 0.3317683881064163 0.11058946270213876 0.33176838810641635 0.3317683881064163 recall 0.0 0.0 1.0 0.3333333333333333 1.0 1.0 f1_score 0.0 0.0 0.49823736780258526 0.16607912260086174 0.4982373678025852 0.49823736780258526 mcc 0.0 0.0 0.0 0.0 0.0 0.0 informedness -1.0 -1.0 0.0 -0.6666666666666666 0.0 0.0 markedness -1.0 -1.0 -0.6682316118935837 -0.8894105372978612 -0.6682316118935837 -0.6682316118935837 csi 0.0 0.0 0.3317683881064163 0.11058946270213876 0.33176838810641635 0.3317683881064163 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")
Note
For multi-class scoring,
vastorbitallows 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_acidityDecimal(6, 3)123volatile_acidityDecimal(7, 4)123densityDouble123pHDecimal(6, 3)123citric_acidDecimal(6, 3)123residual_sugarDecimal(7, 3)123chloridesDouble123free_sulfur_dioxideDecimal(7, 2)123total_sulfur_dioxideDecimal(7, 2)123sulphatesDecimal(6, 3)123alcoholDouble123qualityInteger123goodIntegerAbccolorVarchar(20)123seedrandDecimal(26, 6)123predictionInteger1 6.3 0.28 0.9958 3.39 0.24 8.45 0.031 32.0 172.0 0.57 9.7 7 1 white 0.01 7 2 6.6 0.58 0.99374 3.35 0.02 2.0 0.062 37.0 53.0 0.76 11.6 7 1 red 0.13 7 3 5.6 0.235 0.991 3.34 0.29 1.2 0.047 33.0 127.0 0.5 11.0 7 1 white 0.09 7 4 5.9 0.34 0.9894 3.41 0.22 2.4 0.03 19.0 135.0 0.78 13.9 7 1 white 0.02 7 5 6.5 0.29 0.99053 3.27 0.31 1.7 0.035 24.0 79.0 0.69 11.4 7 1 white 0.07 7 6 5.8 0.28 0.98952 3.32 0.3 1.5 0.026 31.0 114.0 0.6 12.5 7 1 white 0.09 7 7 6.8 0.21 0.99214 3.18 0.4 6.3 0.032 40.0 121.0 0.53 12.0 7 1 white 0.06 7 8 7.7 0.27 0.9972 3.25 0.68 3.5 0.358 5.0 10.0 1.08 9.9 7 1 red 0.11 7 9 9.8 0.34 0.9947 3.19 0.39 1.4 0.066 3.0 7.0 0.55 11.4 7 1 red 0.07 7 10 7.0 0.32 0.99165 3.15 0.27 7.1 0.027 37.0 122.0 0.6 12.6 7 1 white 0.1 7 11 7.4 0.16 0.99198 3.04 0.33 1.2 0.042 47.0 121.0 0.68 10.5 7 1 white 0.18 7 12 6.6 0.29 0.99685 3.02 0.44 9.0 0.053 62.0 178.0 0.45 8.9 5 0 white 0.01 7 13 6.0 0.28 0.9953 3.19 0.49 6.8 0.048 61.0 222.0 0.47 9.3 5 0 white 0.06 7 14 6.8 0.61 0.99525 3.42 0.04 1.5 0.057 5.0 10.0 0.6 9.5 5 0 red 0.01 7 15 7.6 0.42 0.99784 3.15 0.25 3.9 0.104 28.0 90.0 0.57 9.1 5 0 red 0.19 7 16 6.9 0.58 0.99683 3.4 0.01 1.9 0.08 40.0 54.0 0.73 9.7 5 0 red 0.19 7 17 6.5 0.28 1.0002 3.14 0.28 20.4 0.041 40.0 144.0 0.38 8.7 5 0 white 0.19 7 18 6.9 0.4 0.99705 3.4 0.22 5.95 0.081 76.0 303.0 0.57 9.4 5 0 white 0.09 7 19 5.5 0.14 0.9949 3.34 0.27 4.6 0.029 22.0 104.0 0.44 9.0 5 0 white 0.15 7 20 6.9 0.3 0.99827 3.11 0.21 15.7 0.056 49.0 159.0 0.48 9.0 5 0 white 0.04 7 21 6.8 0.45 0.997 3.09 0.3 11.8 0.094 23.0 97.0 0.44 9.6 5 0 white 0.03 7 22 6.0 0.39 0.99114 3.06 0.13 1.2 0.042 60.0 172.0 0.52 10.6 5 0 white 0.04 7 23 6.5 0.41 0.9946 3.14 0.22 4.8 0.052 49.0 142.0 0.62 9.2 5 0 white 0.12 7 24 6.3 0.34 0.99481 3.18 0.52 6.3 0.047 63.0 186.0 0.44 9.6 5 0 white 0.07 7 25 6.4 0.29 0.99478 3.1 0.3 6.5 0.209 62.0 156.0 0.4 9.4 5 0 white 0.15 7 26 6.2 0.25 0.99804 3.14 0.44 15.8 0.057 39.0 167.0 0.51 9.2 5 0 white 0.15 7 27 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 0.12 7 28 6.7 0.51 0.9904 3.22 0.24 2.1 0.043 14.0 155.0 0.6 13.0 6 0 white 0.1 7 29 8.1 0.19 1.0006 3.2 0.58 16.65 0.049 48.0 181.0 0.62 9.1 6 0 white 0.07 7 30 8.0 0.36 0.9982 3.4 0.43 10.1 0.053 29.0 146.0 0.46 9.5 6 0 white 0.09 7 31 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 0.05 7 32 5.8 0.345 0.99494 3.25 0.15 10.8 0.033 26.0 120.0 0.49 10.0 6 0 white 0.15 7 33 6.6 0.28 0.99566 2.93 0.09 10.9 0.051 37.0 131.0 0.62 9.5 6 0 white 0.14 7 34 7.7 0.58 0.99565 3.08 0.1 1.8 0.102 28.0 109.0 0.49 9.8 6 0 red 0.02 7 35 10.6 0.5 0.99708 3.23 0.45 2.6 0.119 34.0 68.0 0.72 10.9 6 0 red 0.03 7 36 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 0.04 7 37 5.8 0.2 0.99115 3.44 0.3 1.5 0.031 21.0 57.0 0.55 11.0 6 0 white 0.04 7 38 7.2 0.38 0.99685 3.42 0.3 1.8 0.073 31.0 70.0 0.59 9.5 6 0 red 0.02 7 39 7.5 0.63 0.99616 3.26 0.27 2.0 0.083 17.0 91.0 0.58 9.8 6 0 red 0.04 7 40 7.5 0.38 0.99605 3.36 0.57 2.3 0.106 5.0 12.0 0.55 11.4 6 0 red 0.04 7 41 7.6 0.29 0.9938 3.13 0.49 9.6 0.03 45.0 197.0 0.38 12.3 7 1 white 0.19 7 42 7.5 0.31 0.99397 3.16 0.24 7.1 0.031 28.0 141.0 0.38 10.6 7 1 white 0.08 7 43 7.1 0.13 0.9932 3.3 0.4 1.2 0.047 54.0 134.0 0.97 9.8 7 1 white 0.12 7 44 7.5 0.34 0.9924 3.16 0.35 6.0 0.034 12.0 126.0 0.39 12.0 7 1 white 0.02 7 45 6.7 0.15 0.9946 3.52 0.29 5.0 0.058 28.0 105.0 0.44 10.2 7 1 white 0.17 7 46 5.4 0.5 0.99079 3.48 0.13 5.0 0.028 12.0 107.0 0.88 13.5 7 1 white 0.06 7 47 6.6 0.26 0.99089 3.22 0.21 2.9 0.026 48.0 126.0 0.38 11.3 7 1 white 0.11 7 48 8.0 0.24 0.99316 3.44 0.26 1.7 0.033 36.0 136.0 0.51 10.4 7 1 white 0.06 7 49 10.2 0.29 0.9976 3.05 0.49 2.6 0.059 5.0 13.0 0.74 10.5 7 1 red 0.07 7 50 5.6 0.19 0.98964 3.46 0.31 2.7 0.027 11.0 100.0 0.4 13.2 7 1 white 0.0 7 51 6.3 0.43 0.99172 3.28 0.32 8.8 0.042 18.0 106.0 0.33 12.9 7 1 white 0.05 7 52 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 0.03 7 53 5.4 0.22 0.99092 3.29 0.35 6.5 0.029 26.0 87.0 0.44 12.5 7 1 white 0.07 7 54 6.0 0.2 0.99051 3.31 0.33 1.1 0.039 45.0 126.0 0.45 11.6 7 1 white 0.13 7 55 7.2 0.2 0.9918 3.21 0.38 1.0 0.037 21.0 74.0 0.37 11.0 5 0 white 0.04 7 56 8.4 0.57 0.9981 3.15 0.44 10.7 0.051 46.0 195.0 0.51 10.4 5 0 white 0.0 7 57 8.5 0.66 0.99733 3.13 0.2 2.1 0.0969999999999999 23.0 113.0 0.48 9.2 5 0 red 0.04 7 58 11.2 0.4 0.99783 3.1 0.5 2.0 0.099 19.0 50.0 0.58 10.4 5 0 red 0.01 7 59 7.3 0.32 0.9985 3.2 0.23 13.7 0.05 49.0 197.0 0.46 8.7 5 0 white 0.02 7 60 7.7 0.25 0.99555 3.1 0.3 7.8 0.038 67.0 196.0 0.5 10.1 5 0 white 0.12 7 61 6.8 0.26 0.9998 3.45 0.29 16.95 0.056 48.0 179.0 0.4 9.6 5 0 white 0.17 7 62 9.6 0.21 0.9926 2.8 0.28 1.2 0.038 12.0 53.0 0.46 10.6 5 0 white 0.19 7 63 8.7 0.24 0.9926 3.08 0.35 0.6 0.042 11.0 71.0 0.38 10.6 5 0 white 0.15 7 64 7.1 0.33 0.99474 2.9 0.18 6.3 0.094 27.0 166.0 0.49 9.5 5 0 white 0.18 7 65 6.4 0.27 0.9955 3.18 0.45 8.3 0.05 52.0 196.0 0.48 9.5 5 0 white 0.03 7 66 6.4 0.32 0.9986 3.26 0.23 16.2 0.055 36.0 176.0 0.54 9.1 5 0 white 0.17 7 67 6.2 0.25 0.99586 3.16 0.42 8.0 0.049 53.0 206.0 0.47 9.1 6 0 white 0.09 7 68 8.5 0.28 0.9981 3.13 0.34 13.8 0.041 32.0 161.0 0.4 9.9 6 0 white 0.1 7 69 6.8 0.38 0.99326 3.11 0.29 9.9 0.037 40.0 146.0 0.37 11.5 6 0 white 0.08 7 70 5.1 0.3 0.98944 3.29 0.3 2.3 0.048 40.0 150.0 0.46 12.2 6 0 white 0.13 7 71 11.6 0.44 0.998 3.21 0.64 2.1 0.059 5.0 15.0 0.67 10.2 6 0 red 0.06 7 72 11.5 0.18 0.9996 3.28 0.51 4.0 0.104 4.0 23.0 0.97 10.1 6 0 red 0.01 7 73 13.4 0.27 1.0002 3.16 0.62 2.6 0.0819999999999999 6.0 21.0 0.67 9.7 6 0 red 0.07 7 74 6.0 0.22 0.99261 2.94 0.23 5.0 0.045 10.0 122.0 0.63 10.0 6 0 white 0.07 7 75 5.7 0.22 0.99862 3.22 0.2 16.0 0.044 41.0 113.0 0.46 8.9 6 0 white 0.01 7 76 7.3 0.19 0.9946 3.32 0.68 1.5 0.05 31.0 156.0 0.64 9.4 6 0 white 0.18 7 77 6.9 0.24 0.9935 3.2 0.34 4.7 0.04 43.0 161.0 0.59 10.6 6 0 white 0.01 7 78 6.0 0.24 0.9938 3.64 0.27 1.9 0.048 40.0 170.0 0.54 10.0 7 1 white 0.13 7 79 7.2 0.2 0.99 3.16 0.34 2.7 0.032 49.0 151.0 0.39 12.7 7 1 white 0.11 7 80 7.3 0.18 0.9918 3.2 0.29 1.2 0.044 12.0 143.0 0.48 11.3 7 1 white 0.17 7 81 5.0 0.27 0.98956 3.45 0.32 4.5 0.032 58.0 178.0 0.31 12.6 7 1 white 0.17 7 82 8.9 0.4 0.995 3.32 0.51 2.6 0.052 13.0 27.0 0.9 13.4 7 1 red 0.01 7 83 8.7 0.48 0.9964 3.33 0.3 2.8 0.066 10.0 28.0 0.67 11.2 7 1 red 0.08 7 84 9.1 0.21 0.99552 3.23 0.37 1.6 0.067 6.0 10.0 0.58 11.1 7 1 red 0.07 7 85 8.9 0.3 0.99458 3.15 0.35 4.6 0.032 32.0 148.0 0.45 11.5 7 1 white 0.04 7 86 8.7 0.3 0.99474 3.12 0.34 4.8 0.018 23.0 127.0 0.49 11.2 7 1 white 0.14 7 87 7.5 0.34 0.98958 3.2 0.28 4.0 0.028 46.0 100.0 0.5 13.2 7 1 white 0.16 7 88 7.4 0.16 0.99825 2.9 0.3 13.7 0.056 33.0 168.0 0.44 8.7 7 1 white 0.09 7 89 5.8 0.415 0.9922 3.29 0.13 1.4 0.04 11.0 64.0 0.52 10.5 5 0 white 0.15 7 90 6.4 0.5 0.9974 3.28 0.16 12.9 0.042 26.0 138.0 0.33 9.0 5 0 white 0.06 7 91 7.0 0.62 0.9978 3.48 0.08 1.8 0.076 8.0 24.0 0.53 9.0 5 0 red 0.0 7 92 9.9 0.5 1.00242 3.16 0.5 13.8 0.205 48.0 82.0 0.75 8.8 5 0 red 0.01 7 93 8.1 0.27 0.9908 2.99 0.41 1.45 0.033 11.0 63.0 0.56 12.0 5 0 white 0.06 7 94 7.5 0.27 0.9939 3.03 0.36 7.0 0.036 45.0 164.0 0.33 11.0 5 0 white 0.17 7 95 8.6 0.18 0.99104 2.99 0.28 0.8 0.032 25.0 78.0 0.38 11.1 5 0 white 0.03 7 96 6.1 0.22 0.9928 3.3 0.49 1.5 0.051 18.0 87.0 0.46 9.6 5 0 white 0.05 7 97 7.8 0.27 0.9934 3.15 0.28 1.8 0.05 21.0 127.0 0.44 9.9 5 0 white 0.11 7 98 7.7 0.26 0.993 3.21 0.32 1.2 0.04 26.0 117.0 0.56 10.8 5 0 white 0.05 7 99 6.9 0.32 0.996 3.23 0.13 7.8 0.042 11.0 117.0 0.37 9.2 5 0 white 0.12 7 100 7.1 0.26 0.996 3.16 0.19 8.2 0.051 53.0 187.0 0.52 9.7 5 0 white 0.18 7 Rows: 1-100 | Columns: 16Note
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
VastFrameto thepredict()function, but in this case, it’s essential that the column names of theVastFramematch 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_acidityDecimal(6, 3)123volatile_acidityDecimal(7, 4)123densityDouble123pHDecimal(6, 3)123citric_acidDecimal(6, 3)123residual_sugarDecimal(7, 3)123chloridesDouble123free_sulfur_dioxideDecimal(7, 2)123total_sulfur_dioxideDecimal(7, 2)123sulphatesDecimal(6, 3)123alcoholDouble123qualityInteger123goodIntegerAbccolorVarchar(20)123seedrandDecimal(26, 6)123predictionInteger123prediction_5Double123prediction_6Double123prediction_7Double1 7.8 0.28 0.9901 3.0 0.34 1.6 0.028 32.0 118.0 0.38 12.1 7 1 white 0.13 7 0.0 0.0 1.0 2 8.0 0.57 0.9917 3.29 0.39 3.9 0.034 22.0 122.0 0.67 12.8 7 1 white 0.0 7 0.0 0.0 1.0 3 7.3 0.65 0.9946 3.39 0.0 1.2 0.065 15.0 21.0 0.47 10.0 7 1 red 0.05 7 0.0 0.0 1.0 4 7.9 0.65 0.9963 3.34 0.01 2.5 0.078 17.0 38.0 0.74 11.7 7 1 red 0.19 7 0.0 0.0 1.0 5 10.5 0.42 0.997 3.24 0.66 2.95 0.1159999999999999 12.0 29.0 0.75 11.7 7 1 red 0.15 7 0.0 0.0 1.0 6 10.2 0.44 0.99745 3.29 0.58 4.1 0.092 11.0 24.0 0.99 12.0 7 1 red 0.09 7 0.0 0.0 1.0 7 5.3 0.3 0.98742 3.31 0.3 1.2 0.029 25.0 93.0 0.4 13.6 7 1 white 0.08 7 0.0 0.0 1.0 8 6.8 0.18 0.99406 3.11 0.28 9.8 0.039 29.0 113.0 0.45 10.9 7 1 white 0.11 7 0.0 0.0 1.0 9 7.1 0.33 0.9912 3.08 0.3 3.3 0.034 30.0 102.0 0.31 12.3 7 1 white 0.0 7 0.0 0.0 1.0 10 7.4 0.24 0.9936 3.18 0.26 1.6 0.058 53.0 150.0 0.5 9.9 7 1 white 0.01 7 0.0 0.0 1.0 11 6.0 0.38 0.98872 3.18 0.26 3.5 0.035 38.0 111.0 0.47 13.6 7 1 white 0.16 7 0.0 0.0 1.0 12 5.3 0.47 0.99182 3.54 0.11 2.2 0.048 16.0 89.0 0.88 13.6 7 1 red 0.12 7 0.0 0.0 1.0 13 11.9 0.38 0.9996 3.24 0.51 2.0 0.121 7.0 20.0 0.76 10.4 6 0 red 0.12 7 0.0 0.0 1.0 14 9.8 0.25 0.9982 3.42 0.49 2.7 0.088 15.0 33.0 0.9 10.0 6 0 red 0.01 7 0.0 0.0 1.0 15 5.8 0.13 0.9956 3.32 0.22 12.7 0.058 24.0 183.0 0.42 11.7 6 0 white 0.01 7 0.0 0.0 1.0 16 7.0 0.26 0.9986 2.94 0.46 15.55 0.037 61.0 171.0 0.35 8.8 6 0 white 0.04 7 0.0 0.0 1.0 17 6.6 0.25 0.9942 3.37 0.24 1.7 0.048 26.0 124.0 0.6 10.1 6 0 white 0.16 7 0.0 0.0 1.0 18 7.6 0.23 0.9967 3.08 0.49 10.0 0.036 45.0 182.0 0.58 9.6 6 0 white 0.08 7 0.0 0.0 1.0 19 6.4 0.37 0.9982 3.14 0.49 13.3 0.045 53.0 243.0 0.48 8.5 6 0 white 0.12 7 0.0 0.0 1.0 20 7.3 0.21 0.997 3.4 0.3 10.9 0.037 18.0 112.0 0.5 9.6 6 0 white 0.01 7 0.0 0.0 1.0 21 7.3 0.19 0.9964 3.36 0.24 6.3 0.054 34.0 231.0 0.54 10.0 6 0 white 0.18 7 0.0 0.0 1.0 22 6.8 0.14 0.99164 3.27 0.18 1.4 0.047 30.0 90.0 0.54 11.2 6 0 white 0.17 7 0.0 0.0 1.0 23 7.0 0.24 0.99636 3.47 0.24 9.0 0.03 42.0 219.0 0.46 10.2 6 0 white 0.14 7 0.0 0.0 1.0 24 6.7 0.2 0.99104 3.11 0.3 1.4 0.025 17.0 76.0 0.44 11.0 6 0 white 0.09 7 0.0 0.0 1.0 25 6.5 0.2 0.99941 2.94 0.5 18.1 0.054 50.0 221.0 0.64 8.8 6 0 white 0.03 7 0.0 0.0 1.0 26 6.4 0.16 0.9908 3.18 0.42 1.0 0.036 29.0 113.0 0.52 11.0 6 0 white 0.05 7 0.0 0.0 1.0 27 6.6 0.18 0.99207 3.13 0.28 1.7 0.041 53.0 161.0 0.45 10.2 6 0 white 0.07 7 0.0 0.0 1.0 28 6.4 0.32 0.99 3.06 0.31 1.9 0.037 34.0 126.0 0.45 11.8 6 0 white 0.01 7 0.0 0.0 1.0 29 7.1 0.6 0.99514 3.4 0.01 2.3 0.079 24.0 37.0 0.61 10.9 6 0 red 0.03 7 0.0 0.0 1.0 30 6.2 0.57 0.99448 3.44 0.1 2.1 0.048 4.0 11.0 0.76 10.8 6 0 red 0.13 7 0.0 0.0 1.0 31 6.0 0.19 0.9972 3.3 0.26 12.4 0.048 50.0 147.0 0.36 8.9 6 0 white 0.09 7 0.0 0.0 1.0 32 5.7 0.36 0.9941 3.29 0.21 6.7 0.038 51.0 166.0 0.63 10.0 6 0 white 0.18 7 0.0 0.0 1.0 33 6.8 0.39 0.99775 3.07 0.35 11.6 0.044 57.0 220.0 0.53 9.3 5 0 white 0.0 7 0.0 0.0 1.0 34 10.0 0.73 0.9966 3.15 0.43 2.3 0.059 15.0 31.0 0.57 11.0 5 0 red 0.03 7 0.0 0.0 1.0 35 9.0 0.46 0.9983 3.1 0.23 2.8 0.092 28.0 104.0 0.56 9.2 5 0 red 0.07 7 0.0 0.0 1.0 36 7.5 0.42 0.99628 3.24 0.32 2.7 0.067 7.0 25.0 0.44 10.4 5 0 red 0.04 7 0.0 0.0 1.0 37 6.1 0.46 0.99537 3.35 0.32 6.2 0.053 10.0 94.0 0.47 10.1 5 0 white 0.11 7 0.0 0.0 1.0 38 7.4 0.74 0.99502 3.12 0.07 1.7 0.086 15.0 48.0 0.48 10.0 5 0 red 0.15 7 0.0 0.0 1.0 39 7.3 0.28 0.9994 3.28 0.42 14.4 0.04 49.0 173.0 0.82 9.0 7 1 white 0.12 7 0.0 0.0 1.0 40 6.9 0.28 0.9918 3.35 0.3 1.6 0.047 46.0 132.0 0.38 11.1 7 1 white 0.13 7 0.0 0.0 1.0 41 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 0.17 7 0.0 0.0 1.0 42 6.0 0.31 0.98952 3.32 0.27 2.3 0.042 19.0 120.0 0.41 12.7 7 1 white 0.09 7 0.0 0.0 1.0 43 7.0 0.26 0.99787 3.47 0.26 10.8 0.039 37.0 184.0 0.58 10.3 7 1 white 0.11 7 0.0 0.0 1.0 44 6.8 0.18 0.99808 3.0 0.3 12.8 0.062 19.0 171.0 0.52 9.0 7 1 white 0.07 7 0.0 0.0 1.0 45 6.7 0.3 0.99159 3.31 0.29 2.8 0.025 37.0 107.0 0.63 11.3 7 1 white 0.0 7 0.0 0.0 1.0 46 6.7 0.28 0.99064 3.26 0.28 2.4 0.012 36.0 100.0 0.39 11.7 7 1 white 0.08 7 0.0 0.0 1.0 47 5.8 0.22 0.98936 3.14 0.29 0.9 0.034 34.0 89.0 0.36 11.1 7 1 white 0.06 7 0.0 0.0 1.0 48 5.9 0.33 0.9911 3.22 0.32 8.1 0.038 9.0 34.0 0.36 12.7 7 1 white 0.14 7 0.0 0.0 1.0 49 6.4 0.23 0.99216 3.18 0.35 4.6 0.039 43.0 147.0 0.4 11.0 7 1 white 0.19 7 0.0 0.0 1.0 50 6.1 0.32 0.9898 3.29 0.24 1.5 0.036 38.0 124.0 0.42 12.4 7 1 white 0.02 7 0.0 0.0 1.0 51 12.9 0.5 1.00012 3.09 0.55 2.8 0.072 7.0 24.0 0.68 10.9 6 0 red 0.07 7 0.0 0.0 1.0 52 10.4 0.26 0.99724 3.33 0.48 1.9 0.066 6.0 10.0 0.87 10.9 6 0 red 0.0 7 0.0 0.0 1.0 53 6.5 0.29 0.99339 3.24 0.3 9.15 0.051 25.0 166.0 0.56 11.35 6 0 white 0.04 7 0.0 0.0 1.0 54 6.7 0.2 0.9987 3.16 0.42 14.0 0.038 83.0 160.0 0.5 9.4 6 0 white 0.08 7 0.0 0.0 1.0 55 6.8 0.18 0.992 3.3 0.37 1.5 0.027 37.0 93.0 0.45 10.8 6 0 white 0.03 7 0.0 0.0 1.0 56 6.5 0.22 0.99297 3.05 0.19 1.1 0.064 36.0 191.0 0.5 9.5 6 0 white 0.05 7 0.0 0.0 1.0 57 7.2 0.24 0.99076 3.13 0.36 2.0 0.029 21.0 63.0 0.63 12.5 6 0 white 0.04 7 0.0 0.0 1.0 58 6.2 0.24 0.9952 3.19 0.28 12.2 0.049 54.0 133.0 0.37 10.7 6 0 white 0.13 7 0.0 0.0 1.0 59 7.3 0.26 0.99693 3.1 0.33 11.8 0.057 48.0 127.0 0.55 10.0 6 0 white 0.02 7 0.0 0.0 1.0 60 7.8 0.3 0.99222 3.06 0.36 4.6 0.024 20.0 198.0 0.66 11.9 6 0 white 0.07 7 0.0 0.0 1.0 61 6.3 0.2 0.9917 3.46 0.4 1.5 0.037 35.0 107.0 0.5 11.4 6 0 white 0.15 7 0.0 0.0 1.0 62 7.3 0.51 0.9944 3.01 0.26 3.3 0.09 7.0 135.0 0.52 8.8 5 0 white 0.17 7 0.0 0.0 1.0 63 6.7 0.58 0.9959 3.28 0.08 1.8 0.0969999999999999 15.0 65.0 0.54 9.2 5 0 red 0.08 7 0.0 0.0 1.0 64 8.1 0.67 0.9968 3.17 0.55 1.8 0.1169999999999999 32.0 141.0 0.62 9.4 5 0 red 0.01 7 0.0 0.0 1.0 65 11.9 0.38 1.0004 3.16 0.49 2.7 0.098 12.0 42.0 0.61 10.3 5 0 red 0.17 7 0.0 0.0 1.0 66 7.1 0.31 0.9965 2.94 0.3 2.2 0.053 36.0 127.0 1.62 9.5 5 0 red 0.14 7 0.0 0.0 1.0 67 7.1 0.52 0.99745 3.5 0.03 2.6 0.076 21.0 92.0 0.6 9.8 5 0 red 0.04 7 0.0 0.0 1.0 68 6.4 0.18 0.9978 3.58 0.74 11.9 0.046 54.0 168.0 0.68 10.1 5 0 white 0.04 7 0.0 0.0 1.0 69 7.1 0.3 0.9942 3.4 0.49 1.6 0.045 31.0 100.0 0.59 10.2 5 0 white 0.1 7 0.0 0.0 1.0 70 6.4 0.22 0.9948 3.4 0.32 7.9 0.029 34.0 124.0 0.39 10.2 5 0 white 0.06 7 0.0 0.0 1.0 71 6.6 0.45 0.9954 3.12 0.43 7.2 0.064 31.0 186.0 0.44 9.4 5 0 white 0.13 7 0.0 0.0 1.0 72 7.4 0.35 0.99304 3.14 0.24 6.0 0.042 28.0 123.0 0.44 11.3 5 0 white 0.05 7 0.0 0.0 1.0 73 7.3 0.32 0.99588 3.43 0.23 2.3 0.066 35.0 70.0 0.62 10.1 5 0 red 0.08 7 0.0 0.0 1.0 74 7.1 0.31 0.99663 3.14 0.25 11.2 0.048 32.0 136.0 0.4 9.5 5 0 white 0.15 7 0.0 0.0 1.0 75 7.3 0.18 0.9918 3.2 0.29 1.2 0.044 12.0 143.0 0.48 11.3 7 1 white 0.02 7 0.0 0.0 1.0 76 7.5 0.52 0.9968 3.38 0.16 1.9 0.085 12.0 35.0 0.62 9.5 7 1 red 0.03 7 0.0 0.0 1.0 77 8.7 0.3 0.99474 3.12 0.34 4.8 0.018 23.0 127.0 0.49 11.2 7 1 white 0.01 7 0.0 0.0 1.0 78 7.4 0.16 0.9984 2.9 0.27 15.5 0.05 25.0 135.0 0.43 8.7 7 1 white 0.03 7 0.0 0.0 1.0 79 6.9 0.33 0.9924 3.25 0.26 5.0 0.027 46.0 143.0 0.43 11.2 7 1 white 0.18 7 0.0 0.0 1.0 80 6.3 0.37 0.9921 3.29 0.28 6.3 0.034 45.0 152.0 0.46 11.6 7 1 white 0.06 7 0.0 0.0 1.0 81 6.0 0.24 0.99014 3.13 0.33 2.5 0.026 31.0 85.0 0.5 11.3 7 1 white 0.07 7 0.0 0.0 1.0 82 5.8 0.17 0.99202 3.43 0.36 1.3 0.036 11.0 70.0 0.68 10.4 7 1 white 0.06 7 0.0 0.0 1.0 83 8.2 0.42 0.9911 3.0 0.29 4.1 0.03 31.0 100.0 0.32 12.8 7 1 white 0.1 7 0.0 0.0 1.0 84 7.0 0.2 0.9922 3.25 0.34 2.1 0.049 12.0 136.0 0.46 11.6 7 1 white 0.04 7 0.0 0.0 1.0 85 6.1 0.36 0.9917 3.4 0.27 2.1 0.035 16.0 100.0 0.71 11.5 7 1 white 0.13 7 0.0 0.0 1.0 86 12.7 0.59 1.0 3.0 0.45 2.3 0.0819999999999999 11.0 22.0 0.7 9.3 6 0 red 0.06 7 0.0 0.0 1.0 87 5.6 0.295 0.99154 3.47 0.26 1.1 0.035 40.0 102.0 0.56 10.6 6 0 white 0.18 7 0.0 0.0 1.0 88 6.4 0.26 0.9956 3.39 0.21 7.1 0.04 35.0 162.0 0.58 9.9 6 0 white 0.07 7 0.0 0.0 1.0 89 6.0 0.26 0.9896 3.38 0.29 1.0 0.032 27.0 96.0 0.44 12.3 6 0 white 0.01 7 0.0 0.0 1.0 90 7.2 0.2 0.997 3.4 0.31 10.0 0.054 49.0 165.0 0.42 9.9 6 0 white 0.06 7 0.0 0.0 1.0 91 7.5 0.25 0.99563 3.12 0.32 8.2 0.024 53.0 209.0 0.46 10.8 6 0 white 0.02 7 0.0 0.0 1.0 92 6.3 0.2 0.99668 3.16 0.19 12.3 0.048 54.0 145.0 0.42 9.3 6 0 white 0.08 7 0.0 0.0 1.0 93 6.7 0.39 0.9948 3.46 0.31 2.7 0.054 27.0 202.0 0.57 10.5 6 0 white 0.11 7 0.0 0.0 1.0 94 7.0 0.31 0.9954 3.13 0.26 7.4 0.069 28.0 160.0 0.46 9.8 6 0 white 0.19 7 0.0 0.0 1.0 95 6.1 0.4 0.993 3.22 0.31 0.9 0.048 23.0 170.0 0.77 9.5 6 0 white 0.17 7 0.0 0.0 1.0 96 6.3 0.34 0.9977 3.23 0.28 14.7 0.047 49.0 198.0 0.46 9.5 5 0 white 0.14 7 0.0 0.0 1.0 97 6.2 0.31 0.9941 3.15 0.21 6.3 0.041 50.0 218.0 0.6 10.0 5 0 white 0.09 7 0.0 0.0 1.0 98 6.1 0.27 0.99985 3.4 0.3 16.7 0.039 49.0 172.0 0.45 9.4 5 0 white 0.05 7 0.0 0.0 1.0 99 5.8 0.415 0.9922 3.29 0.13 1.4 0.04 11.0 64.0 0.52 10.5 5 0 white 0.14 7 0.0 0.0 1.0 100 6.8 0.24 1.0 3.3 0.31 18.3 0.046 40.0 142.0 0.41 8.7 5 0 white 0.07 7 0.0 0.0 1.0 Rows: 1-100 | Columns: 19Note
Probabilities are added to the
VastFrame, and vastorbit 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)
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)
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()
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()KNeighborsClassifiermodels 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.
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.
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