verticapy.machine_learning.vertica.cluster.NearestCentroid¶
- class verticapy.machine_learning.vertica.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 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¶
- p: int, optional
The
pcorresponding to the one of thep-distances (distance metric used to compute the model).
Attributes¶
Many attributes are created during the fitting phase.
- 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
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 iris dataset.
import verticapy.datasets as vpd data = vpd.load_iris()
123SepalLengthCm123SepalWidthCm123PetalLengthCm123PetalWidthCmAbcSpecies1 4.6 3.6 1.0 0.2 Iris-setosa 2 4.7 3.2 1.3 0.2 Iris-setosa 3 4.7 3.2 1.6 0.2 Iris-setosa 4 4.8 3.0 1.4 0.1 Iris-setosa 5 4.8 3.1 1.6 0.2 Iris-setosa 6 4.8 3.4 1.9 0.2 Iris-setosa 7 4.9 3.0 1.4 0.2 Iris-setosa 8 4.9 3.1 1.5 0.1 Iris-setosa 9 4.9 3.1 1.5 0.1 Iris-setosa 10 4.9 3.1 1.5 0.1 Iris-setosa 11 5.0 2.3 3.3 1.0 Iris-versicolor 12 5.0 3.4 1.5 0.2 Iris-setosa 13 5.1 3.5 1.4 0.2 Iris-setosa 14 5.4 3.0 4.5 1.5 Iris-versicolor 15 5.4 3.4 1.5 0.4 Iris-setosa 16 5.4 3.9 1.3 0.4 Iris-setosa 17 5.5 2.4 3.7 1.0 Iris-versicolor 18 5.5 2.4 3.8 1.1 Iris-versicolor 19 5.6 2.7 4.2 1.3 Iris-versicolor 20 5.7 3.0 4.2 1.2 Iris-versicolor 21 5.7 4.4 1.5 0.4 Iris-setosa 22 5.8 2.8 5.1 2.4 Iris-virginica 23 5.9 3.2 4.8 1.8 Iris-versicolor 24 6.1 3.0 4.6 1.4 Iris-versicolor 25 6.1 3.0 4.9 1.8 Iris-virginica 26 6.3 2.5 4.9 1.5 Iris-versicolor 27 6.3 3.3 4.7 1.6 Iris-versicolor 28 6.3 3.3 6.0 2.5 Iris-virginica 29 6.4 2.9 4.3 1.3 Iris-versicolor 30 6.5 3.0 5.5 1.8 Iris-virginica 31 6.5 3.0 5.8 2.2 Iris-virginica 32 6.7 3.0 5.0 1.7 Iris-versicolor 33 6.8 2.8 4.8 1.4 Iris-versicolor 34 6.8 3.2 5.9 2.3 Iris-virginica 35 7.0 3.2 4.7 1.4 Iris-versicolor 36 7.1 3.0 5.9 2.1 Iris-virginica 37 7.7 3.8 6.7 2.2 Iris-virginica 38 4.4 2.9 1.4 0.2 Iris-setosa 39 4.5 2.3 1.3 0.3 Iris-setosa 40 4.8 3.4 1.6 0.2 Iris-setosa 41 5.0 2.0 3.5 1.0 Iris-versicolor 42 5.1 3.3 1.7 0.5 Iris-setosa 43 5.1 3.4 1.5 0.2 Iris-setosa 44 5.2 2.7 3.9 1.4 Iris-versicolor 45 5.2 3.5 1.5 0.2 Iris-setosa 46 5.2 4.1 1.5 0.1 Iris-setosa 47 5.4 3.9 1.7 0.4 Iris-setosa 48 5.5 3.5 1.3 0.2 Iris-setosa 49 5.6 3.0 4.1 1.3 Iris-versicolor 50 5.8 2.7 3.9 1.2 Iris-versicolor 51 5.8 2.7 5.1 1.9 Iris-virginica 52 5.8 2.7 5.1 1.9 Iris-virginica 53 5.9 3.0 4.2 1.5 Iris-versicolor 54 5.9 3.0 5.1 1.8 Iris-virginica 55 6.0 2.7 5.1 1.6 Iris-versicolor 56 6.0 2.9 4.5 1.5 Iris-versicolor 57 6.1 2.8 4.7 1.2 Iris-versicolor 58 6.2 2.8 4.8 1.8 Iris-virginica 59 6.2 2.9 4.3 1.3 Iris-versicolor 60 6.3 2.3 4.4 1.3 Iris-versicolor 61 6.3 2.7 4.9 1.8 Iris-virginica 62 6.4 3.2 5.3 2.3 Iris-virginica 63 6.5 2.8 4.6 1.5 Iris-versicolor 64 6.5 3.0 5.2 2.0 Iris-virginica 65 6.5 3.2 5.1 2.0 Iris-virginica 66 6.6 2.9 4.6 1.3 Iris-versicolor 67 6.6 3.0 4.4 1.4 Iris-versicolor 68 6.7 3.1 4.4 1.4 Iris-versicolor 69 6.7 3.1 4.7 1.5 Iris-versicolor 70 6.9 3.1 4.9 1.5 Iris-versicolor 71 6.9 3.1 5.4 2.1 Iris-virginica 72 6.9 3.2 5.7 2.3 Iris-virginica 73 7.2 3.0 5.8 1.6 Iris-virginica 74 7.2 3.2 6.0 1.8 Iris-virginica 75 7.3 2.9 6.3 1.8 Iris-virginica 76 7.7 2.6 6.9 2.3 Iris-virginica 77 3.3 4.5 5.6 7.8 Iris-setosa 78 3.3 4.5 5.6 7.8 Iris-setosa 79 3.3 4.5 5.6 7.8 Iris-setosa 80 3.3 4.5 5.6 7.8 Iris-setosa 81 3.3 4.5 5.6 7.8 Iris-setosa 82 3.3 4.5 5.6 7.8 Iris-setosa 83 3.3 4.5 5.6 7.8 Iris-setosa 84 3.3 4.5 5.6 7.8 Iris-setosa 85 3.3 4.5 5.6 7.8 Iris-setosa 86 3.3 4.5 5.6 7.8 Iris-setosa 87 3.3 4.5 5.6 7.8 Iris-setosa 88 3.3 4.5 5.6 7.8 Iris-setosa 89 3.3 4.5 5.6 7.8 Iris-setosa 90 3.3 4.5 5.6 7.8 Iris-setosa 91 3.3 4.5 5.6 7.8 Iris-setosa 92 3.3 4.5 5.6 7.8 Iris-setosa 93 3.3 4.5 5.6 7.8 Iris-setosa 94 3.3 4.5 5.6 7.8 Iris-setosa 95 3.3 4.5 5.6 7.8 Iris-setosa 96 3.3 4.5 5.6 7.8 Iris-setosa 97 3.3 4.5 5.6 7.8 Iris-setosa 98 3.3 4.5 5.6 7.8 Iris-setosa 99 3.3 4.5 5.6 7.8 Iris-setosa 100 3.3 4.5 5.6 7.8 Iris-setosa Rows: 1-100 | Columns: 5Note
VerticaPy offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the VerticaPy environment.
You can easily divide your dataset into training and testing subsets using the
vDataFrame.train_test_split()method. This is a crucial step when preparing your data for machine learning, as it allows you to evaluate the performance of your models accurately.data = vpd.load_iris() 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
NearestCentroidmodel:from verticapy.machine_learning.vertica 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
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()
Iris-setosa Iris-versicolor Iris-virginica avg_macro avg_weighted avg_micro auc 1.0 0.9958419958419954 0.9125874125874126 0.9694764694764694 0.9796881496881495 [null] prc_auc 1.0 0.9881304592843054 0.8892942583732055 0.9591415725525035 0.9725586562560247 [null] accuracy 0.66 0.54 0.88 0.6933333333333334 0.6772 0.6933333333333334 log_loss 0.270765904753855 0.198062783689329 0.172246971981091 0.21369188680809167 0.23018892806707011 [null] precision 1.0 0.3611111111111111 1.0 0.7870370370370371 0.8338888888888889 0.54 recall 0.34615384615384615 1.0 0.45454545454545453 0.6002331002331003 0.54 0.54 f1_score 0.5142857142857142 0.5306122448979591 0.625 0.5566326530612244 0.5428877551020408 0.54 mcc 0.4501406909523198 0.3696439322329485 0.6276459144608478 0.48247684588203876 0.4682626828571594 0.31 informedness 0.34615384615384626 0.3783783783783785 0.4545454545454546 0.39302589302589314 0.3783783783783785 0.31000000000000005 markedness 0.5853658536585367 0.36111111111111116 0.8666666666666667 0.6043812104787715 0.5889457994579946 0.31000000000000005 csi 0.34615384615384615 0.3611111111111111 0.45454545454545453 0.38727013727013726 0.3738888888888889 0.3698630136986301 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.9958419958419954 0.9125874125874126 0.9694764694764694 0.9796881496881495 [null] prc_auc 1.0 0.9881304592843054 0.8892942583732055 0.9591415725525035 0.9725586562560247 [null] accuracy 0.8 0.26 0.46 0.5066666666666667 0.5848 0.5066666666666667 log_loss 0.270765904753855 0.198062783689329 0.172246971981091 0.21369188680809167 0.23018892806707011 [null] precision 0.7222222222222222 0.26 0.2777777777777778 0.42 0.5042666666666666 0.4016393442622951 recall 1.0 1.0 0.9090909090909091 0.9696969696969697 0.98 0.98 f1_score 0.8387096774193548 0.41269841269841273 0.4255319148936171 0.5589800016704616 0.6370476408362475 0.5697674418604652 mcc 0.6490734136415512 0.0 0.22366056877342066 0.2909113274716573 0.3867235002237592 0.3024586990598921 informedness 0.5833333333333335 0.0 0.24242424242424243 0.2752525252525253 0.3566666666666668 0.25 markedness 0.7222222222222223 -0.74 0.2063492063492065 0.06285714285714294 0.228552380952381 0.3659250585480094 csi 0.7222222222222222 0.26 0.2702702702702703 0.4174974974974975 0.502615015015015 0.3983739837398374 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") Out[4]: 0.5566326530612244
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, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "prediction", )
123SepalLengthCm123SepalWidthCm123PetalLengthCm123PetalWidthCmAbcSpeciesAbcprediction1 4.7 3.2 1.3 0.2 Iris-setosa Iris-versicolor 2 5.1 3.5 1.4 0.2 Iris-setosa Iris-versicolor 3 5.4 3.4 1.5 0.4 Iris-setosa Iris-versicolor 4 5.4 3.9 1.3 0.4 Iris-setosa Iris-versicolor 5 5.5 2.4 3.7 1.0 Iris-versicolor Iris-versicolor 6 5.7 4.4 1.5 0.4 Iris-setosa Iris-versicolor 7 6.1 3.0 4.6 1.4 Iris-versicolor Iris-versicolor 8 6.1 3.0 4.9 1.8 Iris-virginica Iris-versicolor 9 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 10 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 11 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 12 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 13 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 14 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 15 4.3 3.0 1.1 0.1 Iris-setosa Iris-versicolor 16 4.6 3.1 1.5 0.2 Iris-setosa Iris-versicolor 17 4.9 2.4 3.3 1.0 Iris-versicolor Iris-versicolor 18 5.0 3.5 1.3 0.3 Iris-setosa Iris-versicolor 19 5.1 3.5 1.4 0.3 Iris-setosa Iris-versicolor 20 5.2 3.4 1.4 0.2 Iris-setosa Iris-versicolor 21 5.5 2.3 4.0 1.3 Iris-versicolor Iris-versicolor 22 5.7 2.9 4.2 1.3 Iris-versicolor Iris-versicolor 23 5.7 3.8 1.7 0.3 Iris-setosa Iris-versicolor 24 5.8 4.0 1.2 0.2 Iris-setosa Iris-versicolor 25 6.0 3.4 4.5 1.6 Iris-versicolor Iris-versicolor 26 6.1 2.6 5.6 1.4 Iris-virginica Iris-versicolor 27 6.7 3.0 5.2 2.3 Iris-virginica Iris-versicolor 28 6.7 3.3 5.7 2.5 Iris-virginica Iris-versicolor 29 7.4 2.8 6.1 1.9 Iris-virginica Iris-versicolor 30 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 31 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 32 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 33 5.0 2.0 3.5 1.0 Iris-versicolor Iris-versicolor 34 5.1 3.4 1.5 0.2 Iris-setosa Iris-versicolor 35 5.2 3.5 1.5 0.2 Iris-setosa Iris-versicolor 36 5.2 4.1 1.5 0.1 Iris-setosa Iris-versicolor 37 5.8 2.7 3.9 1.2 Iris-versicolor Iris-versicolor 38 6.6 2.9 4.6 1.3 Iris-versicolor Iris-versicolor 39 6.7 3.1 4.7 1.5 Iris-versicolor Iris-versicolor 40 7.3 2.9 6.3 1.8 Iris-virginica Iris-versicolor 41 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 42 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 43 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 44 4.4 3.0 1.3 0.2 Iris-setosa Iris-versicolor 45 4.8 3.0 1.4 0.3 Iris-setosa Iris-versicolor 46 5.7 2.8 4.5 1.3 Iris-versicolor Iris-versicolor 47 5.8 2.6 4.0 1.2 Iris-versicolor Iris-versicolor 48 6.1 2.9 4.7 1.4 Iris-versicolor Iris-versicolor 49 7.2 3.6 6.1 2.5 Iris-virginica Iris-virginica 50 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica Rows: 1-50 | Columns: 6Note
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, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "prediction", )
123SepalLengthCm123SepalWidthCm123PetalLengthCm123PetalWidthCmAbcSpeciesAbcprediction123prediction_irissetosa123prediction_irisversicolor123prediction_irisvirginica1 4.7 3.2 1.3 0.2 Iris-setosa Iris-versicolor 0.312944316184786 0.450696303968841 0.236359379846373 2 5.1 3.5 1.4 0.2 Iris-setosa Iris-versicolor 0.305242901098255 0.459768556988884 0.234988541912861 3 5.4 3.4 1.5 0.4 Iris-setosa Iris-versicolor 0.2984100856685 0.472660832493979 0.228929081837521 4 5.4 3.9 1.3 0.4 Iris-setosa Iris-versicolor 0.311427922313439 0.453229108469278 0.235342969217283 5 5.5 2.4 3.7 1.0 Iris-versicolor Iris-versicolor 0.157861495016323 0.693044540205306 0.149093964778371 6 5.7 4.4 1.5 0.4 Iris-setosa Iris-versicolor 0.308722817953092 0.451392903909253 0.239884278137655 7 6.1 3.0 4.6 1.4 Iris-versicolor Iris-versicolor 0.0833408821360125 0.817645154019402 0.0990139638445857 8 6.1 3.0 4.9 1.8 Iris-virginica Iris-versicolor 0.146211385749299 0.672393618777159 0.181394995473542 9 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 10 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 11 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 12 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 13 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 14 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 15 4.3 3.0 1.1 0.1 Iris-setosa Iris-versicolor 0.321813075145489 0.436801773039241 0.241385151815271 16 4.6 3.1 1.5 0.2 Iris-setosa Iris-versicolor 0.309130257313096 0.45565457845727 0.235215164229634 17 4.9 2.4 3.3 1.0 Iris-versicolor Iris-versicolor 0.230312351951158 0.577683693215611 0.192003954833231 18 5.0 3.5 1.3 0.3 Iris-setosa Iris-versicolor 0.311975939342601 0.453300380468846 0.234723680188553 19 5.1 3.5 1.4 0.3 Iris-setosa Iris-versicolor 0.30716797640093 0.45944387824759 0.233388145351481 20 5.2 3.4 1.4 0.2 Iris-setosa Iris-versicolor 0.301931975093698 0.464181437317443 0.233886587588858 21 5.5 2.3 4.0 1.3 Iris-versicolor Iris-versicolor 0.140386682144863 0.724404078816897 0.13520923903824 22 5.7 2.9 4.2 1.3 Iris-versicolor Iris-versicolor 0.0734901628045896 0.850917398992328 0.0755924382030824 23 5.7 3.8 1.7 0.3 Iris-setosa Iris-versicolor 0.290425356175494 0.477755448833498 0.231819194991008 24 5.8 4.0 1.2 0.2 Iris-setosa Iris-versicolor 0.304811700431295 0.455109689286964 0.24007861028174 25 6.0 3.4 4.5 1.6 Iris-versicolor Iris-versicolor 0.138320561478235 0.709275376940486 0.152404061581278 26 6.1 2.6 5.6 1.4 Iris-virginica Iris-versicolor 0.167832857924205 0.547035879511815 0.28513126256398 27 6.7 3.0 5.2 2.3 Iris-virginica Iris-versicolor 0.206688550041038 0.522417069321738 0.270894380637223 28 6.7 3.3 5.7 2.5 Iris-virginica Iris-versicolor 0.223104399810004 0.427363900812719 0.349531699377277 29 7.4 2.8 6.1 1.9 Iris-virginica Iris-versicolor 0.203892477324845 0.427613142536413 0.368494380138742 30 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 31 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 32 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.468457956157774 0.252196848676983 0.279345195165243 33 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.209628008273572 0.228564527104335 0.561807464622093 34 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.209628008273572 0.228564527104335 0.561807464622093 35 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.209628008273572 0.228564527104335 0.561807464622093 36 4.4 3.0 1.3 0.2 Iris-setosa Iris-versicolor 0.317568071455339 0.444511659334834 0.237920269209827 37 4.8 3.0 1.4 0.3 Iris-setosa Iris-versicolor 0.308057042352219 0.459343114771024 0.232599842876758 38 5.7 2.8 4.5 1.3 Iris-versicolor Iris-versicolor 0.0777858573452441 0.834467080129732 0.0877470625250235 39 5.8 2.6 4.0 1.2 Iris-versicolor Iris-versicolor 0.087548835162665 0.824691930529365 0.08775923430797 40 6.1 2.9 4.7 1.4 Iris-versicolor Iris-versicolor 0.0899236981460054 0.799732225103934 0.110344076750061 41 7.2 3.6 6.1 2.5 Iris-virginica Iris-virginica 0.223267697340344 0.377742837243672 0.398989465415985 42 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.209628008273572 0.228564527104335 0.561807464622093 43 5.0 2.0 3.5 1.0 Iris-versicolor Iris-versicolor 0.219258539205836 0.58735580968435 0.193385651109814 44 5.1 3.4 1.5 0.2 Iris-setosa Iris-versicolor 0.301137578729782 0.465462807609822 0.233399613660396 45 5.2 3.5 1.5 0.2 Iris-setosa Iris-versicolor 0.300309370192825 0.466024171799892 0.233666458007283 46 5.2 4.1 1.5 0.1 Iris-setosa Iris-versicolor 0.307190703845569 0.451617329438814 0.241191966715617 47 5.8 2.7 3.9 1.2 Iris-versicolor Iris-versicolor 0.096424758316226 0.809366741744776 0.0942084999389981 48 6.6 2.9 4.6 1.3 Iris-versicolor Iris-versicolor 0.119918726394561 0.731958889348189 0.14812238425725 49 6.7 3.1 4.7 1.5 Iris-versicolor Iris-versicolor 0.144897847439601 0.673755957276255 0.181346195284144 50 7.3 2.9 6.3 1.8 Iris-virginica Iris-versicolor 0.19847615009052 0.406886997375678 0.394636852533801 Rows: 1-50 | Columns: 9Note
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.
model.confusion_matrix() Out[5]: array([[ 9, 17, 0], [ 0, 13, 0], [ 0, 6, 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.Specific confusion matrix:
model.confusion_matrix(pos_label = "Iris-setosa", cutoff = 0.6) Out[6]: array([[24, 0], [26, 0]])
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() Out[7]: {'p': 2}
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() Out[9]: 'CASE WHEN "SepalLengthCm" IS NULL OR "SepalWidthCm" IS NULL OR "PetalLengthCm" IS NULL OR "PetalWidthCm" IS NULL THEN NULL WHEN POWER(POWER("SepalLengthCm" - 5.38988764044944, 2) + POWER("SepalWidthCm" - 3.86179775280899, 2) + POWER("PetalLengthCm" - 7.63258426966292, 2) + POWER("PetalWidthCm" - 1.90898876404494, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.03918918918919, 2) + POWER("SepalWidthCm" - 3.99594594594595, 2) + POWER("PetalLengthCm" - 3.77027027027027, 2) + POWER("PetalWidthCm" - 4.42972972972973, 2), 1 / 2) AND POWER(POWER("SepalLengthCm" - 5.38988764044944, 2) + POWER("SepalWidthCm" - 3.86179775280899, 2) + POWER("PetalLengthCm" - 7.63258426966292, 2) + POWER("PetalWidthCm" - 1.90898876404494, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 5.98378378378378, 2) + POWER("SepalWidthCm" - 2.78648648648649, 2) + POWER("PetalLengthCm" - 4.29189189189189, 2) + POWER("PetalWidthCm" - 1.34594594594595, 2), 1 / 2) THEN \'Iris-virginica\' WHEN POWER(POWER("SepalLengthCm" - 5.98378378378378, 2) + POWER("SepalWidthCm" - 2.78648648648649, 2) + POWER("PetalLengthCm" - 4.29189189189189, 2) + POWER("PetalWidthCm" - 1.34594594594595, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.03918918918919, 2) + POWER("SepalWidthCm" - 3.99594594594595, 2) + POWER("PetalLengthCm" - 3.77027027027027, 2) + POWER("PetalWidthCm" - 4.42972972972973, 2), 1 / 2) THEN \'Iris-versicolor\' ELSE \'Iris-setosa\' END'
To Python
To obtain the prediction function in Python syntax, use the following code:
X = [[5, 2, 3, 1]] model.to_python()(X) Out[11]: array(['Iris-versicolor'], dtype=object)
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¶
Must be overridden in the child class
Methods
__init__([name, overwrite_model, 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, pos_label, cutoff, allSQL])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()NearestCentroidmodels 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.
Converts the model to an InMemory object that can be used for different types of predictions.
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