vastorbit.machine_learning.vast.cluster.NearestCentroid¶
- class vastorbit.machine_learning.vast.cluster.NearestCentroid(name: str = None, overwrite_model: bool = False, p: int = 2)¶
Creates a
NearestCentroidobject using the k-nearest centroid algorithm. This object uses pure SQL to compute the distances and final score.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:
p (int, optional) – The
pcorresponding to the one of thep-distances (distance metric used to compute the model).- Variables:
created (Many attributes are)
phase. (during the fitting)
clusters_ (numpy.array) – Cluster centers.
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 iris dataset.
import vastorbit.datasets as vod data = vod.load_iris()
123sepallengthcmDecimal(5, 2)123sepalwidthcmDecimal(5, 2)123petallengthcmDecimal(5, 2)123petalwidthcmDecimal(5, 2)AbcspeciesVarchar(30)1 5.1 3.5 1.4 0.2 Iris-setosa 2 4.9 3.0 1.4 0.2 Iris-setosa 3 4.7 3.2 1.3 0.2 Iris-setosa 4 4.6 3.1 1.5 0.2 Iris-setosa 5 5.0 3.6 1.4 0.2 Iris-setosa 6 5.4 3.9 1.7 0.4 Iris-setosa 7 4.6 3.4 1.4 0.3 Iris-setosa 8 5.0 3.4 1.5 0.2 Iris-setosa 9 4.4 2.9 1.4 0.2 Iris-setosa 10 4.9 3.1 1.5 0.1 Iris-setosa 11 5.4 3.7 1.5 0.2 Iris-setosa 12 4.8 3.4 1.6 0.2 Iris-setosa 13 4.8 3.0 1.4 0.1 Iris-setosa 14 4.3 3.0 1.1 0.1 Iris-setosa 15 5.8 4.0 1.2 0.2 Iris-setosa 16 5.7 4.4 1.5 0.4 Iris-setosa 17 5.4 3.9 1.3 0.4 Iris-setosa 18 5.1 3.5 1.4 0.3 Iris-setosa 19 5.7 3.8 1.7 0.3 Iris-setosa 20 5.1 3.8 1.5 0.3 Iris-setosa 21 5.4 3.4 1.7 0.2 Iris-setosa 22 5.1 3.7 1.5 0.4 Iris-setosa 23 4.6 3.6 1.0 0.2 Iris-setosa 24 5.1 3.3 1.7 0.5 Iris-setosa 25 4.8 3.4 1.9 0.2 Iris-setosa 26 5.0 3.0 1.6 0.2 Iris-setosa 27 5.0 3.4 1.6 0.4 Iris-setosa 28 5.2 3.5 1.5 0.2 Iris-setosa 29 5.2 3.4 1.4 0.2 Iris-setosa 30 4.7 3.2 1.6 0.2 Iris-setosa 31 4.8 3.1 1.6 0.2 Iris-setosa 32 5.4 3.4 1.5 0.4 Iris-setosa 33 5.2 4.1 1.5 0.1 Iris-setosa 34 5.5 4.2 1.4 0.2 Iris-setosa 35 4.9 3.1 1.5 0.1 Iris-setosa 36 5.0 3.2 1.2 0.2 Iris-setosa 37 5.5 3.5 1.3 0.2 Iris-setosa 38 4.9 3.1 1.5 0.1 Iris-setosa 39 4.4 3.0 1.3 0.2 Iris-setosa 40 5.1 3.4 1.5 0.2 Iris-setosa 41 5.0 3.5 1.3 0.3 Iris-setosa 42 4.5 2.3 1.3 0.3 Iris-setosa 43 4.4 3.2 1.3 0.2 Iris-setosa 44 5.0 3.5 1.6 0.6 Iris-setosa 45 5.1 3.8 1.9 0.4 Iris-setosa 46 4.8 3.0 1.4 0.3 Iris-setosa 47 5.1 3.8 1.6 0.2 Iris-setosa 48 4.6 3.2 1.4 0.2 Iris-setosa 49 5.3 3.7 1.5 0.2 Iris-setosa 50 5.0 3.3 1.4 0.2 Iris-setosa 51 7.0 3.2 4.7 1.4 Iris-versicolor 52 6.4 3.2 4.5 1.5 Iris-versicolor 53 6.9 3.1 4.9 1.5 Iris-versicolor 54 5.5 2.3 4.0 1.3 Iris-versicolor 55 6.5 2.8 4.6 1.5 Iris-versicolor 56 5.7 2.8 4.5 1.3 Iris-versicolor 57 6.3 3.3 4.7 1.6 Iris-versicolor 58 4.9 2.4 3.3 1.0 Iris-versicolor 59 6.6 2.9 4.6 1.3 Iris-versicolor 60 5.2 2.7 3.9 1.4 Iris-versicolor 61 5.0 2.0 3.5 1.0 Iris-versicolor 62 5.9 3.0 4.2 1.5 Iris-versicolor 63 6.0 2.2 4.0 1.0 Iris-versicolor 64 6.1 2.9 4.7 1.4 Iris-versicolor 65 5.6 2.9 3.6 1.3 Iris-versicolor 66 6.7 3.1 4.4 1.4 Iris-versicolor 67 5.6 3.0 4.5 1.5 Iris-versicolor 68 5.8 2.7 4.1 1.0 Iris-versicolor 69 6.2 2.2 4.5 1.5 Iris-versicolor 70 5.6 2.5 3.9 1.1 Iris-versicolor 71 5.9 3.2 4.8 1.8 Iris-versicolor 72 6.1 2.8 4.0 1.3 Iris-versicolor 73 6.3 2.5 4.9 1.5 Iris-versicolor 74 6.1 2.8 4.7 1.2 Iris-versicolor 75 6.4 2.9 4.3 1.3 Iris-versicolor 76 6.6 3.0 4.4 1.4 Iris-versicolor 77 6.8 2.8 4.8 1.4 Iris-versicolor 78 6.7 3.0 5.0 1.7 Iris-versicolor 79 6.0 2.9 4.5 1.5 Iris-versicolor 80 5.7 2.6 3.5 1.0 Iris-versicolor 81 5.5 2.4 3.8 1.1 Iris-versicolor 82 5.5 2.4 3.7 1.0 Iris-versicolor 83 5.8 2.7 3.9 1.2 Iris-versicolor 84 6.0 2.7 5.1 1.6 Iris-versicolor 85 5.4 3.0 4.5 1.5 Iris-versicolor 86 6.0 3.4 4.5 1.6 Iris-versicolor 87 6.7 3.1 4.7 1.5 Iris-versicolor 88 6.3 2.3 4.4 1.3 Iris-versicolor 89 5.6 3.0 4.1 1.3 Iris-versicolor 90 5.5 2.5 4.0 1.3 Iris-versicolor 91 5.5 2.6 4.4 1.2 Iris-versicolor 92 6.1 3.0 4.6 1.4 Iris-versicolor 93 5.8 2.6 4.0 1.2 Iris-versicolor 94 5.0 2.3 3.3 1.0 Iris-versicolor 95 5.6 2.7 4.2 1.3 Iris-versicolor 96 5.7 3.0 4.2 1.2 Iris-versicolor 97 5.7 2.9 4.2 1.3 Iris-versicolor 98 6.2 2.9 4.3 1.3 Iris-versicolor 99 5.1 2.5 3.0 1.1 Iris-versicolor 100 5.7 2.8 4.1 1.3 Iris-versicolor Rows: 1-100 | Columns: 5Note
vastorbit offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the vastorbit environment.
You can easily divide your dataset into training and testing subsets using the
VastFrame.train_test_split()method. This is a crucial step when preparing your data for machine learning, as it allows you to evaluate the performance of your models accurately.data = vod.load_iris() 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
NearestCentroidmodel:from vastorbit.machine_learning.vast import NearestCentroid
Then we can create the model:
model = NearestCentroid(p = 2)
Model Training¶
We can now fit the model:
model.fit( train, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "Species", 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()
Iris-setosa Iris-versicolor Iris-virginica avg_macro avg_weighted avg_micro auc 1.0 0.9464285714285714 0.9429824561403508 0.9631370091896407 0.9658319185059422 [null] prc_auc 0.22287390029325505 0.5276160620988208 0.283559070857458 0.34468301108317795 0.31517768027420334 [null] accuracy 1.0 0.8709677419354839 0.8709677419354839 0.913978494623656 0.9209157127991675 0.9139784946236559 log_loss 0.24228991388266358 0.3052768153002631 0.3180582180134203 0.28854164906544905 0.2858424287049951 [null] precision 1.0 0.6363636363636364 1.0 0.8787878787878788 0.9178885630498533 0.8709677419354839 recall 1.0 1.0 0.6666666666666666 0.8888888888888888 0.8709677419354839 0.8709677419354839 f1_score 1.0 0.7777777777777778 0.8 0.8592592592592592 0.8724014336917563 0.8709677419354839 mcc 1.0 0.7282190812544191 0.7421082385212817 0.8234424399252336 0.8388010461624616 0.8064516129032258 informedness 1.0 0.8333333333333335 0.6666666666666665 0.8333333333333334 0.8333333333333334 0.8064516129032258 markedness 1.0 0.6363636363636362 0.8260869565217392 0.8208168642951251 0.8505673849292362 0.8064516129032258 csi 1.0 0.6363636363636364 0.6666666666666666 0.7676767676767676 0.7888563049853372 0.7714285714285715 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)
Iris-setosa Iris-versicolor Iris-virginica avg_macro avg_weighted avg_micro auc 1.0 0.9702380952380952 0.9912280701754387 0.987155388471178 0.9898839841539333 [null] prc_auc 0.19354838709677424 0.12380952380952381 0.25852854582693274 0.19196215224441027 0.20295451166939196 [null] accuracy 0.9032258064516129 0.5806451612903226 0.8709677419354839 0.7849462365591399 0.8178980228928199 0.7849462365591398 log_loss 0.1895134731328137 0.3052768153002631 0.34538958598674424 0.28005995813994033 0.27599272311408185 [null] precision 0.8 0.35 0.75 0.6333333333333333 0.6790322580645162 0.6078431372549019 recall 1.0 1.0 1.0 1.0 1.0 1.0 f1_score 0.888888888888889 0.5185185185185185 0.8571428571428571 0.7548500881834216 0.7929680832906639 0.7560975609756097 mcc 0.8207826816681233 0.4005204946899305 0.7694837640638655 0.6635956468073064 0.7060271229552704 0.6416889479197478 informedness 0.8421052631578947 0.45833333333333326 0.7894736842105263 0.6966374269005847 0.7350735710243349 0.6774193548387095 markedness 0.8 0.3500000000000001 0.75 0.6333333333333334 0.6790322580645163 0.607843137254902 csi 0.8 0.35 0.75 0.6333333333333333 0.6790322580645162 0.6078431372549019 Rows: 1-11 | Columns: 7You can also use the
score()function 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, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "prediction", )
123sepallengthcmDecimal(5, 2)123sepalwidthcmDecimal(5, 2)123petallengthcmDecimal(5, 2)123petalwidthcmDecimal(5, 2)AbcspeciesVarchar(30)123seedrandDecimal(26, 6)AbcpredictionVarchar(15)1 4.9 3.0 1.4 0.2 Iris-setosa 0.12 Iris-setosa 2 4.8 3.4 1.6 0.2 Iris-setosa 0.17 Iris-setosa 3 4.8 3.0 1.4 0.1 Iris-setosa 0.1 Iris-setosa 4 5.8 4.0 1.2 0.2 Iris-setosa 0.13 Iris-setosa 5 5.1 3.7 1.5 0.4 Iris-setosa 0.08 Iris-setosa 6 5.1 3.3 1.7 0.5 Iris-setosa 0.18 Iris-setosa 7 5.0 3.4 1.6 0.4 Iris-setosa 0.03 Iris-setosa 8 5.2 3.5 1.5 0.2 Iris-setosa 0.08 Iris-setosa 9 4.8 3.1 1.6 0.2 Iris-setosa 0.18 Iris-setosa 10 5.5 4.2 1.4 0.2 Iris-setosa 0.05 Iris-setosa 11 5.0 3.5 1.3 0.3 Iris-setosa 0.14 Iris-setosa 12 5.1 3.8 1.6 0.2 Iris-setosa 0.04 Iris-setosa 13 6.3 3.3 4.7 1.6 Iris-versicolor 0.16 Iris-versicolor 14 6.7 3.1 4.4 1.4 Iris-versicolor 0.17 Iris-versicolor 15 6.6 3.0 4.4 1.4 Iris-versicolor 0.18 Iris-versicolor 16 5.5 2.4 3.8 1.1 Iris-versicolor 0.09 Iris-versicolor 17 6.7 3.1 4.7 1.5 Iris-versicolor 0.05 Iris-versicolor 18 5.5 2.6 4.4 1.2 Iris-versicolor 0.0 Iris-versicolor 19 6.2 2.9 4.3 1.3 Iris-versicolor 0.13 Iris-versicolor 20 5.8 2.7 5.1 1.9 Iris-virginica 0.16 Iris-versicolor 21 6.4 2.7 5.3 1.9 Iris-virginica 0.02 Iris-virginica 22 6.4 3.2 5.3 2.3 Iris-virginica 0.1 Iris-virginica 23 7.2 3.2 6.0 1.8 Iris-virginica 0.0 Iris-virginica 24 6.4 2.8 5.6 2.2 Iris-virginica 0.03 Iris-virginica 25 6.3 2.8 5.1 1.5 Iris-virginica 0.13 Iris-versicolor 26 6.0 3.0 4.8 1.8 Iris-virginica 0.02 Iris-versicolor 27 6.9 3.1 5.4 2.1 Iris-virginica 0.17 Iris-virginica 28 5.8 2.7 5.1 1.9 Iris-virginica 0.0 Iris-versicolor 29 6.7 3.0 5.2 2.3 Iris-virginica 0.15 Iris-virginica 30 6.3 2.5 5.0 1.9 Iris-virginica 0.09 Iris-virginica 31 6.5 3.0 5.2 2.0 Iris-virginica 0.05 Iris-virginica Rows: 1-31 | Columns: 7Note
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, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "prediction", )
123sepallengthcmDecimal(5, 2)123sepalwidthcmDecimal(5, 2)123petallengthcmDecimal(5, 2)123petalwidthcmDecimal(5, 2)AbcspeciesVarchar(30)123seedrandDecimal(26, 6)AbcpredictionVarchar(15)123prediction_irissetosaDouble123prediction_irisversicolorDouble123prediction_irisvirginicaDouble1 4.7 3.2 1.3 0.2 Iris-setosa 0.12 Iris-setosa 0.7613965675103003 0.14467417537820826 0.09392925711149135 2 4.8 3.0 1.4 0.1 Iris-setosa 0.17 Iris-setosa 0.7289722137664401 0.16533541855596667 0.10569236767759328 3 4.3 3.0 1.1 0.1 Iris-setosa 0.1 Iris-setosa 0.6752524881790439 0.19461369042330223 0.1301338213976539 4 5.7 4.4 1.5 0.4 Iris-setosa 0.13 Iris-setosa 0.6089627224911727 0.23249497163084687 0.15854230587798052 5 4.6 3.6 1.0 0.2 Iris-setosa 0.08 Iris-setosa 0.7229031081342717 0.16513769314254176 0.11195919872318653 6 4.8 3.4 1.9 0.2 Iris-setosa 0.18 Iris-setosa 0.7841931720572493 0.1335219167016401 0.08228491124111063 7 5.2 3.5 1.5 0.2 Iris-setosa 0.03 Iris-setosa 0.7779514218510921 0.13560899375540197 0.08643958439350587 8 5.2 3.4 1.4 0.2 Iris-setosa 0.08 Iris-setosa 0.7692382262328845 0.14068609892939601 0.09007567483771937 9 5.4 3.4 1.5 0.4 Iris-setosa 0.18 Iris-setosa 0.762573577753049 0.14584302956585052 0.09158339268110065 10 4.9 3.1 1.5 0.1 Iris-setosa 0.05 Iris-setosa 0.7497918349332112 0.15323691823555283 0.09697124683123601 11 4.5 2.3 1.3 0.3 Iris-setosa 0.14 Iris-setosa 0.61420078529435 0.23443297669537155 0.1513662380102784 12 4.6 3.2 1.4 0.2 Iris-setosa 0.04 Iris-setosa 0.7622151438946718 0.1444553014084304 0.09332955469689763 13 4.9 2.4 3.3 1.0 Iris-versicolor 0.16 Iris-versicolor 0.3363928374654682 0.4598994425739441 0.20370771996058756 14 5.6 3.0 4.5 1.5 Iris-versicolor 0.17 Iris-versicolor 0.11121925249352126 0.6894319067695716 0.1993488407369071 15 6.8 2.8 4.8 1.4 Iris-versicolor 0.18 Iris-versicolor 0.13309413891180494 0.4714626106153817 0.39544325047281337 16 5.5 2.4 3.7 1.0 Iris-versicolor 0.09 Iris-versicolor 0.2074585599396806 0.6072144463294987 0.18532699373082068 17 6.3 2.3 4.4 1.3 Iris-versicolor 0.05 Iris-versicolor 0.12254420944806177 0.6511526206131306 0.22630316993880756 18 6.1 3.0 4.6 1.4 Iris-versicolor 0.0 Iris-versicolor 0.10060338450808148 0.6723535209825083 0.22704309450941013 19 5.1 2.5 3.0 1.1 Iris-versicolor 0.13 Iris-versicolor 0.37772235451658315 0.4297518538640195 0.19252579161939745 20 7.1 3.0 5.9 2.1 Iris-virginica 0.16 Iris-virginica 0.08442952944076036 0.19136973094153362 0.724200739617706 21 6.8 3.0 5.5 2.1 Iris-virginica 0.02 Iris-virginica 0.07290457573655844 0.18867039483007145 0.7384250294333701 22 6.5 3.0 5.5 1.8 Iris-virginica 0.1 Iris-virginica 0.07047814984499497 0.20373521631072047 0.7257866338442845 23 6.2 2.8 4.8 1.8 Iris-virginica 0.0 Iris-versicolor 0.11474219215115426 0.5220450582939605 0.3632127495548853 24 6.3 2.8 5.1 1.5 Iris-virginica 0.03 Iris-versicolor 0.11223298418101837 0.4465472305614894 0.4412197852574921 25 6.1 2.6 5.6 1.4 Iris-virginica 0.13 Iris-virginica 0.11588732189298584 0.3567102445774058 0.5274024335296085 26 6.9 3.1 5.4 2.1 Iris-virginica 0.02 Iris-virginica 0.08833860526972465 0.22576719522478783 0.6858941995054876 27 6.7 3.1 5.6 2.4 Iris-virginica 0.17 Iris-virginica 0.07506125595718344 0.1806363597989686 0.744302384243848 28 6.8 3.2 5.9 2.3 Iris-virginica 0.0 Iris-virginica 0.06428702577483228 0.14571972018792767 0.7899932540372401 29 6.3 2.5 5.0 1.9 Iris-virginica 0.15 Iris-virginica 0.11882646653270593 0.43853734608973305 0.44263618737756105 30 6.5 3.0 5.2 2.0 Iris-virginica 0.09 Iris-virginica 0.09561454284890682 0.2934311063592053 0.6109543507918879 31 6.2 3.4 5.4 2.3 Iris-virginica 0.05 Iris-virginica 0.10197954887586298 0.26047818323807675 0.6375422678860603 Rows: 1-31 | Columns: 10Note
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.
model.confusion_matrix()
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.Specific confusion matrix:
model.confusion_matrix(pos_label = "Iris-setosa", 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 = "Iris-setosa")
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 = "Iris-setosa")
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({'p': 3})
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¶
To Memmodel
model.to_memmodel()
Note
MemModelobjects serve as in-memory representations of machine learning models. They can be used for both in-database and in-memory prediction tasks. These objects can be pickled in the same way that you would pickle ascikit-learnmodel.The following methods for exporting the model use
MemModel, and it is recommended to useMemModeldirectly.To SQL
You can get the SQL code by:
model.to_sql()
To Python
To obtain the prediction function in Python syntax, use the following code:
X = [[5, 2, 3, 1]] model.to_python()(X)
Hint
The
to_python()method is used to retrieve predictions, probabilities, or cluster distances. For specific details on how to use this method for different model types, refer to the relevant documentation for each model.- __init__(name: str = None, overwrite_model: bool = False, p: int = 2) None¶
Methods
__init__([name, overwrite_model, 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, pos_label, cutoff, allSQL])Returns the SQL code needed to deploy the model.
drop()NearestCentroidmodels 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.
Converts the model to an InMemory object that can be used for different types of predictions.
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