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 3.3 4.5 5.6 7.8 Iris-setosa 2 3.3 4.5 5.6 7.8 Iris-setosa 3 3.3 4.5 5.6 7.8 Iris-setosa 4 3.3 4.5 5.6 7.8 Iris-setosa 5 3.3 4.5 5.6 7.8 Iris-setosa 6 3.3 4.5 5.6 7.8 Iris-setosa 7 3.3 4.5 5.6 7.8 Iris-setosa 8 3.3 4.5 5.6 7.8 Iris-setosa 9 3.3 4.5 5.6 7.8 Iris-setosa 10 3.3 4.5 5.6 7.8 Iris-setosa 11 3.3 4.5 5.6 7.8 Iris-setosa 12 3.3 4.5 5.6 7.8 Iris-setosa 13 3.3 4.5 5.6 7.8 Iris-setosa 14 3.3 4.5 5.6 7.8 Iris-setosa 15 3.3 4.5 5.6 7.8 Iris-setosa 16 3.3 4.5 5.6 7.8 Iris-setosa 17 3.3 4.5 5.6 7.8 Iris-setosa 18 3.3 4.5 5.6 7.8 Iris-setosa 19 3.3 4.5 5.6 7.8 Iris-setosa 20 3.3 4.5 5.6 7.8 Iris-setosa 21 3.3 4.5 5.6 7.8 Iris-setosa 22 3.3 4.5 5.6 7.8 Iris-setosa 23 3.3 4.5 5.6 7.8 Iris-setosa 24 3.3 4.5 5.6 7.8 Iris-setosa 25 3.3 4.5 5.6 7.8 Iris-setosa 26 3.3 4.5 5.6 7.8 Iris-setosa 27 4.3 3.0 1.1 0.1 Iris-setosa 28 4.3 4.7 9.6 1.8 Iris-virginica 29 4.3 4.7 9.6 1.8 Iris-virginica 30 4.3 4.7 9.6 1.8 Iris-virginica 31 4.3 4.7 9.6 1.8 Iris-virginica 32 4.3 4.7 9.6 1.8 Iris-virginica 33 4.3 4.7 9.6 1.8 Iris-virginica 34 4.3 4.7 9.6 1.8 Iris-virginica 35 4.3 4.7 9.6 1.8 Iris-virginica 36 4.3 4.7 9.6 1.8 Iris-virginica 37 4.3 4.7 9.6 1.8 Iris-virginica 38 4.3 4.7 9.6 1.8 Iris-virginica 39 4.3 4.7 9.6 1.8 Iris-virginica 40 4.3 4.7 9.6 1.8 Iris-virginica 41 4.3 4.7 9.6 1.8 Iris-virginica 42 4.3 4.7 9.6 1.8 Iris-virginica 43 4.3 4.7 9.6 1.8 Iris-virginica 44 4.3 4.7 9.6 1.8 Iris-virginica 45 4.3 4.7 9.6 1.8 Iris-virginica 46 4.3 4.7 9.6 1.8 Iris-virginica 47 4.3 4.7 9.6 1.8 Iris-virginica 48 4.3 4.7 9.6 1.8 Iris-virginica 49 4.3 4.7 9.6 1.8 Iris-virginica 50 4.3 4.7 9.6 1.8 Iris-virginica 51 4.3 4.7 9.6 1.8 Iris-virginica 52 4.3 4.7 9.6 1.8 Iris-virginica 53 4.3 4.7 9.6 1.8 Iris-virginica 54 4.4 2.9 1.4 0.2 Iris-setosa 55 4.4 3.0 1.3 0.2 Iris-setosa 56 4.4 3.2 1.3 0.2 Iris-setosa 57 4.5 2.3 1.3 0.3 Iris-setosa 58 4.6 3.1 1.5 0.2 Iris-setosa 59 4.6 3.2 1.4 0.2 Iris-setosa 60 4.6 3.4 1.4 0.3 Iris-setosa 61 4.6 3.6 1.0 0.2 Iris-setosa 62 4.7 3.2 1.3 0.2 Iris-setosa 63 4.7 3.2 1.6 0.2 Iris-setosa 64 4.8 3.0 1.4 0.1 Iris-setosa 65 4.8 3.0 1.4 0.3 Iris-setosa 66 4.8 3.1 1.6 0.2 Iris-setosa 67 4.8 3.4 1.6 0.2 Iris-setosa 68 4.8 3.4 1.9 0.2 Iris-setosa 69 4.9 2.4 3.3 1.0 Iris-versicolor 70 4.9 2.5 4.5 1.7 Iris-virginica 71 4.9 3.0 1.4 0.2 Iris-setosa 72 4.9 3.1 1.5 0.1 Iris-setosa 73 4.9 3.1 1.5 0.1 Iris-setosa 74 4.9 3.1 1.5 0.1 Iris-setosa 75 5.0 2.0 3.5 1.0 Iris-versicolor 76 5.0 2.3 3.3 1.0 Iris-versicolor 77 5.0 3.0 1.6 0.2 Iris-setosa 78 5.0 3.2 1.2 0.2 Iris-setosa 79 5.0 3.3 1.4 0.2 Iris-setosa 80 5.0 3.4 1.5 0.2 Iris-setosa 81 5.0 3.4 1.6 0.4 Iris-setosa 82 5.0 3.5 1.3 0.3 Iris-setosa 83 5.0 3.5 1.6 0.6 Iris-setosa 84 5.0 3.6 1.4 0.2 Iris-setosa 85 5.1 2.5 3.0 1.1 Iris-versicolor 86 5.1 3.3 1.7 0.5 Iris-setosa 87 5.1 3.4 1.5 0.2 Iris-setosa 88 5.1 3.5 1.4 0.2 Iris-setosa 89 5.1 3.5 1.4 0.3 Iris-setosa 90 5.1 3.7 1.5 0.4 Iris-setosa 91 5.1 3.8 1.5 0.3 Iris-setosa 92 5.1 3.8 1.6 0.2 Iris-setosa 93 5.1 3.8 1.9 0.4 Iris-setosa 94 5.2 2.7 3.9 1.4 Iris-versicolor 95 5.2 3.4 1.4 0.2 Iris-setosa 96 5.2 3.5 1.5 0.2 Iris-setosa 97 5.2 4.1 1.5 0.1 Iris-setosa 98 5.3 3.7 1.5 0.2 Iris-setosa 99 5.4 3.0 4.5 1.5 Iris-versicolor 100 5.4 3.4 1.5 0.4 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 1.0 0.9017094017094016 0.9672364672364672 0.9597902097902097 [null] prc_auc 1.0 0.9999999999999998 0.9008978239956499 0.9669659413318832 0.9594582007254931 [null] accuracy 0.7727272727272727 0.5909090909090909 0.8181818181818182 0.7272727272727274 0.7541322314049587 0.7272727272727273 log_loss 0.219410015811111 0.184883532269521 0.224385522885376 0.20955969032200264 0.21438321525343965 [null] precision 1.0 0.3333333333333333 1.0 0.7777777777777777 0.8636363636363636 0.5909090909090909 recall 0.4117647058823529 1.0 0.5555555555555556 0.6557734204793028 0.5909090909090909 0.5909090909090909 f1_score 0.5833333333333334 0.5 0.7142857142857143 0.5992063492063493 0.6198593073593074 0.5909090909090909 mcc 0.5481577761336338 0.40237390808147827 0.651794907395869 0.5341088638703271 0.5607353568211528 0.38636363636363635 informedness 0.4117647058823528 0.48571428571428577 0.5555555555555556 0.4843448490507314 0.4857142857142857 0.38636363636363624 markedness 0.7297297297297298 0.33333333333333326 0.7647058823529411 0.6092563151386681 0.6629570747217806 0.38636363636363624 csi 0.4117647058823529 0.3333333333333333 0.5555555555555556 0.4335511982570806 0.45454545454545453 0.41935483870967744 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 1.0 0.9017094017094016 0.9672364672364672 0.9597902097902097 [null] prc_auc 1.0 0.9999999999999998 0.9008978239956499 0.9669659413318832 0.9594582007254931 [null] accuracy 0.7045454545454546 0.20454545454545456 0.5909090909090909 0.5 0.5557851239669422 0.5 log_loss 0.219410015811111 0.184883532269521 0.224385522885376 0.20955969032200264 0.21438321525343965 [null] precision 0.5666666666666667 0.20454545454545456 0.5 0.42373737373737375 0.46532369146005514 0.4 recall 1.0 1.0 1.0 1.0 1.0 1.0 f1_score 0.7234042553191489 0.339622641509434 0.6666666666666666 0.576564521165083 0.6216926389547827 0.5714285714285715 mcc 0.5420582630066875 0.0 0.3922322702763681 0.3114301777610185 0.36989025763837075 0.31622776601683794 informedness 0.5185185185185186 0.0 0.3076923076923077 0.2754036087369421 0.32621082621082625 0.25 markedness 0.5666666666666667 -0.7954545454545454 0.5 0.09040404040404042 0.26077823691460056 0.3999999999999999 csi 0.5666666666666667 0.20454545454545456 0.5 0.42373737373737375 0.46532369146005514 0.4 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.5992063492063493
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 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 2 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 3 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 4 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 5 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 6 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 7 4.3 3.0 1.1 0.1 Iris-setosa Iris-versicolor 8 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 9 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 10 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 11 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 12 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 13 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 14 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 15 4.4 3.2 1.3 0.2 Iris-setosa Iris-versicolor 16 4.8 3.1 1.6 0.2 Iris-setosa Iris-versicolor 17 4.8 3.4 1.9 0.2 Iris-setosa Iris-versicolor 18 4.9 2.5 4.5 1.7 Iris-virginica Iris-versicolor 19 4.9 3.1 1.5 0.1 Iris-setosa Iris-versicolor 20 4.9 3.1 1.5 0.1 Iris-setosa Iris-versicolor 21 5.0 3.2 1.2 0.2 Iris-setosa Iris-versicolor 22 5.0 3.6 1.4 0.2 Iris-setosa Iris-versicolor 23 5.4 3.4 1.5 0.4 Iris-setosa Iris-versicolor 24 5.4 3.4 1.7 0.2 Iris-setosa Iris-versicolor 25 5.5 2.3 4.0 1.3 Iris-versicolor Iris-versicolor 26 5.6 3.0 4.5 1.5 Iris-versicolor Iris-versicolor 27 5.7 3.0 4.2 1.2 Iris-versicolor Iris-versicolor 28 5.8 2.6 4.0 1.2 Iris-versicolor Iris-versicolor 29 5.8 2.7 4.1 1.0 Iris-versicolor Iris-versicolor 30 5.8 2.7 5.1 1.9 Iris-virginica Iris-versicolor 31 5.8 2.8 5.1 2.4 Iris-virginica Iris-versicolor 32 5.9 3.0 4.2 1.5 Iris-versicolor Iris-versicolor 33 6.3 2.3 4.4 1.3 Iris-versicolor Iris-versicolor 34 6.4 2.7 5.3 1.9 Iris-virginica Iris-versicolor 35 6.4 2.8 5.6 2.2 Iris-virginica Iris-versicolor 36 6.6 3.0 4.4 1.4 Iris-versicolor Iris-versicolor 37 6.7 3.1 4.7 1.5 Iris-versicolor Iris-versicolor 38 6.9 3.1 5.1 2.3 Iris-virginica Iris-versicolor 39 6.9 3.1 5.4 2.1 Iris-virginica Iris-versicolor 40 7.2 3.0 5.8 1.6 Iris-virginica Iris-versicolor 41 7.7 2.8 6.7 2.0 Iris-virginica Iris-virginica 42 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 43 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 44 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica Rows: 1-44 | 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 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.396381814771746 0.285670531542205 0.317947653686049 2 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.396381814771746 0.285670531542205 0.317947653686049 3 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.396381814771746 0.285670531542205 0.317947653686049 4 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.396381814771746 0.285670531542205 0.317947653686049 5 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.396381814771746 0.285670531542205 0.317947653686049 6 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.396381814771746 0.285670531542205 0.317947653686049 7 4.3 3.0 1.1 0.1 Iris-setosa Iris-versicolor 0.379734552842337 0.392357174613991 0.227908272543672 8 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 9 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 10 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 11 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 12 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 13 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 14 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 15 4.4 3.2 1.3 0.2 Iris-setosa Iris-versicolor 0.381239549318794 0.394511434935913 0.224249015745294 16 4.8 3.1 1.6 0.2 Iris-setosa Iris-versicolor 0.363049183789877 0.416327247839306 0.220623568370818 17 4.8 3.4 1.9 0.2 Iris-setosa Iris-versicolor 0.360198343948455 0.420250217088923 0.219551438962622 18 4.9 2.5 4.5 1.7 Iris-virginica Iris-versicolor 0.243327746800057 0.546051124367299 0.210621128832644 19 4.9 3.1 1.5 0.1 Iris-setosa Iris-versicolor 0.359646672601595 0.41712926843067 0.223224058967735 20 4.9 3.1 1.5 0.1 Iris-setosa Iris-versicolor 0.359646672601595 0.41712926843067 0.223224058967735 21 5.0 3.2 1.2 0.2 Iris-setosa Iris-versicolor 0.368852111557597 0.407478131317743 0.22366975712466 22 5.0 3.6 1.4 0.2 Iris-setosa Iris-versicolor 0.371083036951049 0.40549644246018 0.223420520588772 23 5.4 3.4 1.5 0.4 Iris-setosa Iris-versicolor 0.360971742279516 0.4208857574975 0.218142500222984 24 5.4 3.4 1.7 0.2 Iris-setosa Iris-versicolor 0.348280629938078 0.43125651049698 0.220462859564941 25 5.5 2.3 4.0 1.3 Iris-versicolor Iris-versicolor 0.162748807304786 0.699133529903945 0.138117662791268 26 5.6 3.0 4.5 1.5 Iris-versicolor Iris-versicolor 0.13350854171098 0.732517805689522 0.133973652599498 27 5.7 3.0 4.2 1.2 Iris-versicolor Iris-versicolor 0.0992448362719544 0.808275976718031 0.0924791870100147 28 5.8 2.6 4.0 1.2 Iris-versicolor Iris-versicolor 0.0968252431540244 0.817072234848162 0.0861025219978133 29 5.8 2.7 4.1 1.0 Iris-versicolor Iris-versicolor 0.100232730577828 0.804633028055739 0.095134241366433 30 5.8 2.7 5.1 1.9 Iris-virginica Iris-versicolor 0.186898988942092 0.555378996580417 0.257722014477492 31 5.8 2.8 5.1 2.4 Iris-virginica Iris-versicolor 0.230829918559465 0.475955724734366 0.29321435670617 32 5.9 3.0 4.2 1.5 Iris-versicolor Iris-versicolor 0.0890103608466354 0.829609778096014 0.0813798610573502 33 6.3 2.3 4.4 1.3 Iris-versicolor Iris-versicolor 0.129936325799823 0.727017375062842 0.143046299137335 34 6.4 2.7 5.3 1.9 Iris-virginica Iris-versicolor 0.186242417652747 0.508887418013017 0.304870164334236 35 6.4 2.8 5.6 2.2 Iris-virginica Iris-versicolor 0.198223993603442 0.422990674155991 0.378785332240567 36 6.6 3.0 4.4 1.4 Iris-versicolor Iris-versicolor 0.149755725328825 0.681714508186852 0.168529766484323 37 6.7 3.1 4.7 1.5 Iris-versicolor Iris-versicolor 0.171114811278727 0.609042204950581 0.219842983770691 38 6.9 3.1 5.1 2.3 Iris-virginica Iris-versicolor 0.219900357496961 0.462897662922802 0.317201979580237 39 6.9 3.1 5.4 2.1 Iris-virginica Iris-versicolor 0.204584105811045 0.44062725047918 0.354788643709775 40 7.2 3.0 5.8 1.6 Iris-virginica Iris-versicolor 0.191352800835983 0.409822839002349 0.398824360161668 41 7.7 2.8 6.7 2.0 Iris-virginica Iris-virginica 0.197515742964366 0.331045470260923 0.471438786774711 42 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.396381814771746 0.285670531542205 0.317947653686049 43 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 44 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.232107095005353 0.26197205744578 0.505920847548867 Rows: 1-44 | 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([[ 7, 10, 0], [ 0, 9, 0], [ 0, 8, 10]])
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([[27, 0], [17, 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.74090909090909, 2) + POWER("SepalWidthCm" - 3.6469696969697, 2) + POWER("PetalLengthCm" - 7.10606060606061, 2) + POWER("PetalWidthCm" - 1.94242424242424, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.33582089552239, 2) + POWER("SepalWidthCm" - 3.87910447761194, 2) + POWER("PetalLengthCm" - 3.12985074626866, 2) + POWER("PetalWidthCm" - 3.29701492537313, 2), 1 / 2) AND POWER(POWER("SepalLengthCm" - 5.74090909090909, 2) + POWER("SepalWidthCm" - 3.6469696969697, 2) + POWER("PetalLengthCm" - 7.10606060606061, 2) + POWER("PetalWidthCm" - 1.94242424242424, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 5.92439024390244, 2) + POWER("SepalWidthCm" - 2.76829268292683, 2) + POWER("PetalLengthCm" - 4.25609756097561, 2) + POWER("PetalWidthCm" - 1.32682926829268, 2), 1 / 2) THEN \'Iris-virginica\' WHEN POWER(POWER("SepalLengthCm" - 5.92439024390244, 2) + POWER("SepalWidthCm" - 2.76829268292683, 2) + POWER("PetalLengthCm" - 4.25609756097561, 2) + POWER("PetalWidthCm" - 1.32682926829268, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.33582089552239, 2) + POWER("SepalWidthCm" - 3.87910447761194, 2) + POWER("PetalLengthCm" - 3.12985074626866, 2) + POWER("PetalWidthCm" - 3.29701492537313, 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