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
NearestCentroid
object 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
p
corresponding 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
p
of 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 fromverticapy
are 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()
123SepalLengthCmNumeric(7)123SepalWidthCmNumeric(7)123PetalLengthCmNumeric(7)123PetalWidthCmNumeric(7)AbcSpeciesVarchar(30)1 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 3.3 4.5 5.6 7.8 Iris-setosa 28 3.3 4.5 5.6 7.8 Iris-setosa 29 3.3 4.5 5.6 7.8 Iris-setosa 30 3.3 4.5 5.6 7.8 Iris-setosa 31 3.3 4.5 5.6 7.8 Iris-setosa 32 3.3 4.5 5.6 7.8 Iris-setosa 33 3.3 4.5 5.6 7.8 Iris-setosa 34 3.3 4.5 5.6 7.8 Iris-setosa 35 3.3 4.5 5.6 7.8 Iris-setosa 36 3.3 4.5 5.6 7.8 Iris-setosa 37 3.3 4.5 5.6 7.8 Iris-setosa 38 3.3 4.5 5.6 7.8 Iris-setosa 39 3.3 4.5 5.6 7.8 Iris-setosa 40 3.3 4.5 5.6 7.8 Iris-setosa 41 3.3 4.5 5.6 7.8 Iris-setosa 42 3.3 4.5 5.6 7.8 Iris-setosa 43 4.3 3.0 1.1 0.1 Iris-setosa 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.3 4.7 9.6 1.8 Iris-virginica 55 4.3 4.7 9.6 1.8 Iris-virginica 56 4.3 4.7 9.6 1.8 Iris-virginica 57 4.3 4.7 9.6 1.8 Iris-virginica 58 4.3 4.7 9.6 1.8 Iris-virginica 59 4.3 4.7 9.6 1.8 Iris-virginica 60 4.3 4.7 9.6 1.8 Iris-virginica 61 4.3 4.7 9.6 1.8 Iris-virginica 62 4.3 4.7 9.6 1.8 Iris-virginica 63 4.3 4.7 9.6 1.8 Iris-virginica 64 4.3 4.7 9.6 1.8 Iris-virginica 65 4.3 4.7 9.6 1.8 Iris-virginica 66 4.3 4.7 9.6 1.8 Iris-virginica 67 4.3 4.7 9.6 1.8 Iris-virginica 68 4.3 4.7 9.6 1.8 Iris-virginica 69 4.3 4.7 9.6 1.8 Iris-virginica 70 4.3 4.7 9.6 1.8 Iris-virginica 71 4.3 4.7 9.6 1.8 Iris-virginica 72 4.3 4.7 9.6 1.8 Iris-virginica 73 4.3 4.7 9.6 1.8 Iris-virginica 74 4.3 4.7 9.6 1.8 Iris-virginica 75 4.3 4.7 9.6 1.8 Iris-virginica 76 4.3 4.7 9.6 1.8 Iris-virginica 77 4.3 4.7 9.6 1.8 Iris-virginica 78 4.3 4.7 9.6 1.8 Iris-virginica 79 4.3 4.7 9.6 1.8 Iris-virginica 80 4.3 4.7 9.6 1.8 Iris-virginica 81 4.3 4.7 9.6 1.8 Iris-virginica 82 4.3 4.7 9.6 1.8 Iris-virginica 83 4.3 4.7 9.6 1.8 Iris-virginica 84 4.3 4.7 9.6 1.8 Iris-virginica 85 4.3 4.7 9.6 1.8 Iris-virginica 86 4.4 2.9 1.4 0.2 Iris-setosa 87 4.4 3.0 1.3 0.2 Iris-setosa 88 4.4 3.2 1.3 0.2 Iris-setosa 89 4.5 2.3 1.3 0.3 Iris-setosa 90 4.6 3.1 1.5 0.2 Iris-setosa 91 4.6 3.2 1.4 0.2 Iris-setosa 92 4.6 3.4 1.4 0.3 Iris-setosa 93 4.6 3.6 1.0 0.2 Iris-setosa 94 4.7 3.2 1.3 0.2 Iris-setosa 95 4.7 3.2 1.6 0.2 Iris-setosa 96 4.8 3.0 1.4 0.1 Iris-setosa 97 4.8 3.0 1.4 0.3 Iris-setosa 98 4.8 3.1 1.6 0.2 Iris-setosa 99 4.8 3.4 1.6 0.2 Iris-setosa 100 4.8 3.4 1.9 0.2 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 intotables
ortemporary 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 thepreprocessing
module. 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
NearestCentroid
model: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
vDataFrame
or 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 usingX
matrices andy
vectors. 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.9952153110047846 0.9426523297491041 0.9792892135846296 0.9778593950313572 [null] prc_auc 1.0 0.9834022038567495 0.9488297373358348 0.9774106470641947 0.9774767247850871 [null] accuracy 0.7755102040816326 0.6530612244897959 0.8775510204081632 0.7687074829931971 0.7855060391503541 0.7687074829931972 log_loss 0.230479489381102 0.184993852501743 0.203255665384036 0.20624300242229365 0.21026783963375226 [null] precision 1.0 0.39285714285714285 1.0 0.7976190476190476 0.8637026239067055 0.6530612244897959 recall 0.45 1.0 0.6666666666666666 0.7055555555555556 0.6530612244897959 0.6530612244897959 f1_score 0.6206896551724138 0.5641025641025641 0.8 0.6615974064249927 0.6738555369097241 0.6530612244897959 mcc 0.5711829829397931 0.46594555814804667 0.7473677532236446 0.5948320981038281 0.6122791909479587 0.47959183673469385 informedness 0.44999999999999996 0.5526315789473686 0.6666666666666665 0.5564327485380117 0.5526315789473684 0.47959183673469385 markedness 0.7250000000000001 0.3928571428571428 0.8378378378378377 0.6518983268983268 0.6918879520920337 0.47959183673469385 csi 0.45 0.39285714285714285 0.6666666666666666 0.5031746031746032 0.5167638483965015 0.48484848484848486 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
cutoff
to 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.9952153110047846 0.9426523297491041 0.9792892135846296 0.9778593950313572 [null] prc_auc 1.0 0.9834022038567495 0.9488297373358348 0.9774106470641947 0.9774767247850871 [null] accuracy 0.6326530612244898 0.22448979591836735 0.5306122448979592 0.4625850340136055 0.5035401915868389 0.46258503401360546 log_loss 0.230479489381102 0.184993852501743 0.203255665384036 0.20624300242229365 0.21026783963375226 [null] precision 0.5263157894736842 0.22448979591836735 0.43902439024390244 0.39660999187865137 0.42649270548910134 0.3828125 recall 1.0 1.0 1.0 1.0 1.0 1.0 f1_score 0.6896551724137931 0.36666666666666664 0.6101694915254238 0.5554971102019612 0.5879487271238127 0.5536723163841808 mcc 0.446807591244225 0.0 0.3365956280719286 0.2611344064387178 0.3060178189832493 0.27243118397129207 informedness 0.3793103448275863 0.0 0.25806451612903225 0.2124582869855395 0.24961975891580215 0.19387755102040805 markedness 0.5263157894736841 -0.7755102040816326 0.4390243902439024 0.06327665854531794 0.202002909570734 0.3828125 csi 0.5263157894736842 0.22448979591836735 0.43902439024390244 0.39660999187865137 0.42649270548910134 0.3828125 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.6615974064249927
Note
For multi-class scoring,
verticapy
allows the flexibility to use three averaging techniques:micro
,macro
andweighted
. 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", )
123SepalLengthCmNumeric(7)123SepalWidthCmNumeric(7)123PetalLengthCmNumeric(7)123PetalWidthCmNumeric(7)AbcSpeciesVarchar(30)AbcpredictionVarchar(15)1 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 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 8 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 9 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 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.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 16 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 17 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 18 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 19 4.4 2.9 1.4 0.2 Iris-setosa Iris-versicolor 20 4.4 3.2 1.3 0.2 Iris-setosa Iris-versicolor 21 4.6 3.1 1.5 0.2 Iris-setosa Iris-versicolor 22 4.6 3.6 1.0 0.2 Iris-setosa Iris-versicolor 23 4.9 2.4 3.3 1.0 Iris-versicolor Iris-versicolor 24 4.9 2.5 4.5 1.7 Iris-virginica Iris-versicolor 25 4.9 3.1 1.5 0.1 Iris-setosa Iris-versicolor 26 5.0 3.2 1.2 0.2 Iris-setosa Iris-versicolor 27 5.1 3.4 1.5 0.2 Iris-setosa Iris-versicolor 28 5.1 3.8 1.9 0.4 Iris-setosa Iris-versicolor 29 5.3 3.7 1.5 0.2 Iris-setosa Iris-versicolor 30 5.4 3.9 1.7 0.4 Iris-setosa Iris-versicolor 31 5.5 2.6 4.4 1.2 Iris-versicolor Iris-versicolor 32 5.5 3.5 1.3 0.2 Iris-setosa Iris-versicolor 33 5.6 2.9 3.6 1.3 Iris-versicolor Iris-versicolor 34 5.6 3.0 4.5 1.5 Iris-versicolor Iris-versicolor 35 5.7 2.8 4.1 1.3 Iris-versicolor Iris-versicolor 36 5.8 2.8 5.1 2.4 Iris-virginica Iris-versicolor 37 6.0 2.2 4.0 1.0 Iris-versicolor Iris-versicolor 38 6.3 2.9 5.6 1.8 Iris-virginica Iris-versicolor 39 6.4 2.8 5.6 2.2 Iris-virginica Iris-versicolor 40 6.5 2.8 4.6 1.5 Iris-versicolor Iris-versicolor 41 6.7 3.0 5.0 1.7 Iris-versicolor Iris-versicolor 42 6.7 3.1 4.4 1.4 Iris-versicolor Iris-versicolor 43 6.7 3.1 4.7 1.5 Iris-versicolor Iris-versicolor 44 6.8 3.0 5.5 2.1 Iris-virginica Iris-versicolor 45 6.9 3.1 5.1 2.3 Iris-virginica Iris-versicolor 46 7.0 3.2 4.7 1.4 Iris-versicolor Iris-versicolor 47 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 48 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 49 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica Rows: 1-49 | 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
vDataFrame
to thepredict()
function, but in this case, it’s essential that the column names of thevDataFrame
match 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", )
123SepalLengthCmNumeric(7)123SepalWidthCmNumeric(7)123PetalLengthCmNumeric(7)123PetalWidthCmNumeric(7)AbcSpeciesVarchar(30)AbcpredictionVarchar(15)123prediction_irissetosaFloat(22)123prediction_irisversicolorFloat(22)123prediction_irisvirginicaFloat(22)1 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 2 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 3 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 4 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 5 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 6 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 7 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 8 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 9 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 0.439815281418325 0.265642438277911 0.294542280303764 10 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 11 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 12 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 13 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 14 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 15 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 16 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 17 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 18 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 19 4.4 2.9 1.4 0.2 Iris-setosa Iris-versicolor 0.330268557664226 0.437162827890549 0.232568614445226 20 4.4 3.2 1.3 0.2 Iris-setosa Iris-versicolor 0.336161525711707 0.42997820250681 0.233860271781483 21 4.6 3.1 1.5 0.2 Iris-setosa Iris-versicolor 0.325485630582585 0.443610599036623 0.230903770380793 22 4.6 3.6 1.0 0.2 Iris-setosa Iris-versicolor 0.342818580663625 0.420601299084306 0.236580120252069 23 4.9 2.4 3.3 1.0 Iris-versicolor Iris-versicolor 0.239239402214376 0.573841888713742 0.186918709071882 24 4.9 2.5 4.5 1.7 Iris-virginica Iris-versicolor 0.217435664264753 0.581183998862614 0.201380336872633 25 4.9 3.1 1.5 0.1 Iris-setosa Iris-versicolor 0.316147455336299 0.45280678646746 0.231045758196242 26 5.0 3.2 1.2 0.2 Iris-setosa Iris-versicolor 0.325419753092588 0.442570064467993 0.232010182439419 27 5.1 3.4 1.5 0.2 Iris-setosa Iris-versicolor 0.317790705135042 0.452190713876606 0.230018580988352 28 5.1 3.8 1.9 0.4 Iris-setosa Iris-versicolor 0.317293975607921 0.456098478841408 0.226607545550671 29 5.3 3.7 1.5 0.2 Iris-setosa Iris-versicolor 0.318048056683047 0.449922228213781 0.232029715103172 30 5.4 3.9 1.7 0.4 Iris-setosa Iris-versicolor 0.318133728494953 0.452895565571247 0.2289707059338 31 5.5 2.6 4.4 1.2 Iris-versicolor Iris-versicolor 0.103993834829428 0.78502198346498 0.110984181705592 32 5.5 3.5 1.3 0.2 Iris-setosa Iris-versicolor 0.316711024287613 0.451033354681892 0.232255621030495 33 5.6 2.9 3.6 1.3 Iris-versicolor Iris-versicolor 0.158619967378488 0.710282638619325 0.131097394002188 34 5.6 3.0 4.5 1.5 Iris-versicolor Iris-versicolor 0.116687495515223 0.759555225638263 0.123757278846513 35 5.7 2.8 4.1 1.3 Iris-versicolor Iris-versicolor 0.0645564442231668 0.872875429934949 0.0625681258418844 36 5.8 2.8 5.1 2.4 Iris-virginica Iris-versicolor 0.228518115798783 0.500361348874077 0.271120535327141 37 6.0 2.2 4.0 1.0 Iris-versicolor Iris-versicolor 0.128894137166244 0.738615492396869 0.132490370436887 38 6.3 2.9 5.6 1.8 Iris-virginica Iris-versicolor 0.185177915925555 0.477491515609994 0.337330568464451 39 6.4 2.8 5.6 2.2 Iris-virginica Iris-versicolor 0.204166313842683 0.451050971046454 0.344782715110863 40 6.5 2.8 4.6 1.5 Iris-versicolor Iris-versicolor 0.134430019682679 0.702711083935956 0.162858896381365 41 6.7 3.0 5.0 1.7 Iris-versicolor Iris-versicolor 0.177209338525633 0.574741953490661 0.248048707983707 42 6.7 3.1 4.4 1.4 Iris-versicolor Iris-versicolor 0.154442423809786 0.665725724202858 0.179831851987356 43 6.7 3.1 4.7 1.5 Iris-versicolor Iris-versicolor 0.163087805032487 0.628444189432862 0.208468005534651 44 6.8 3.0 5.5 2.1 Iris-virginica Iris-versicolor 0.204169926585465 0.459137472431628 0.336692600982907 45 6.9 3.1 5.1 2.3 Iris-virginica Iris-versicolor 0.220019948242774 0.484640415937677 0.295339635819549 46 7.0 3.2 4.7 1.4 Iris-versicolor Iris-versicolor 0.180976565317765 0.582191912609919 0.236831522072316 47 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 48 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 49 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.222969212886079 0.244390864011189 0.532639923102732 Rows: 1-49 | Columns: 9Note
Probabilities are added to the
vDataFrame
, and VerticaPy uses the corresponding probability function in SQL behind the scenes. You can use thepos_label
parameter to add only the probability of the selected category.Confusion Matrix#
You can obtain the confusion matrix.
model.confusion_matrix() Out[5]: array([[ 9, 11, 0], [ 0, 11, 0], [ 0, 6, 12]])
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_label
and 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([[29, 0], [20, 0]])
Note
In classification, the
cutoff
is 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
MemModel
objects 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-learn
model.The following methods for exporting the model use
MemModel
, and it is recommended to useMemModel
directly.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.58875, 2) + POWER("SepalWidthCm" - 3.76, 2) + POWER("PetalLengthCm" - 7.3975, 2) + POWER("PetalWidthCm" - 1.92, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.16282051282051, 2) + POWER("SepalWidthCm" - 3.96153846153846, 2) + POWER("PetalLengthCm" - 3.53589743589744, 2) + POWER("PetalWidthCm" - 4.02435897435898, 2), 1 / 2) AND POWER(POWER("SepalLengthCm" - 5.58875, 2) + POWER("SepalWidthCm" - 3.76, 2) + POWER("PetalLengthCm" - 7.3975, 2) + POWER("PetalWidthCm" - 1.92, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 5.8948717948718, 2) + POWER("SepalWidthCm" - 2.75384615384615, 2) + POWER("PetalLengthCm" - 4.24871794871795, 2) + POWER("PetalWidthCm" - 1.32051282051282, 2), 1 / 2) THEN \'Iris-virginica\' WHEN POWER(POWER("SepalLengthCm" - 5.8948717948718, 2) + POWER("SepalWidthCm" - 2.75384615384615, 2) + POWER("PetalLengthCm" - 4.24871794871795, 2) + POWER("PetalWidthCm" - 1.32051282051282, 2), 1 / 2) <= POWER(POWER("SepalLengthCm" - 4.16282051282051, 2) + POWER("SepalWidthCm" - 3.96153846153846, 2) + POWER("PetalLengthCm" - 3.53589743589744, 2) + POWER("PetalWidthCm" - 4.02435897435898, 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
()NearestCentroid
models 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