verticapy.machine_learning.vertica.naive_bayes.NaiveBayes¶
- class verticapy.machine_learning.vertica.naive_bayes.NaiveBayes(name: str = None, overwrite_model: bool = False, alpha: Annotated[int | float | Decimal, 'Python Numbers'] = 1.0, nbtype: Literal['auto', 'bernoulli', 'categorical', 'multinomial', 'gaussian'] = 'auto')¶
Creates a
NaiveBayesobject using the Vertica Naive Bayes algorithm. It is a “probabilistic classifier” based on applying Bayes’ theorem with strong (naïve) independence assumptions between the features.Parameters¶
- name: str, optional
Name of the model. The model is stored in the database.
- overwrite_model: bool, optional
If set to
True, training a model with the same name as an existing model overwrites the existing model.- alpha: float, optional
A
floatthat specifies use of Laplace smoothing if the event model is categorical, multinomial, or Bernoulli.- nbtype: str, optional
Naive Bayes type.
- auto:
Vertica NaiveBayes objects treat columns according to data type:
- FLOAT:
values are assumed to follow some Gaussian distribution.
- INTEGER:
values are assumed to belong to one multinomial distribution.
- CHAR/VARCHAR:
values are assumed to follow some categorical distribution. The string values stored in these columns must be no greater than 128 characters.
- BOOLEAN:
values are treated as categorical with two values.
- bernoulli:
Casts the variables to boolean.
- categorical:
Casts the variables to categorical.
- multinomial:
Casts the variables to integer.
- gaussian:
Casts the variables to float.
Attributes¶
Many attributes are created during the fitting phase.
- prior_: numpy.array
The model’s classes probabilities.
- attributes: list of dict
listof the model’s attributes. Each feature is represented by adictionary, which differs based on the distribution.- classes_: numpy.array
The classes labels.
Note
All attributes can be accessed using the
get_attributes()method.Note
Several other attributes can be accessed by using the
get_vertica_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
NaiveBayesmodel:from verticapy.machine_learning.vertica import NaiveBayes
Then we can create the model:
model = NaiveBayes()
Hint
In
verticapy1.0.x and higher, you do not need to specify the model name, as the name is automatically assigned. If you need to re-use the model, you can fetch the model name from the model’s attributes.Important
The model name is crucial for the model management system and versioning. It’s highly recommended to provide a name if you plan to reuse the model later.
Model Training¶
We can now fit the model:
model.fit( train, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "Species", test, ) ======= details ======= index| predictor | type -----+-------------+--------- 0 | Species |ResponseC 1 |SepalLengthCm|Gaussian 2 |SepalWidthCm |Gaussian 3 |PetalLengthCm|Gaussian 4 |PetalWidthCm |Gaussian ===== prior ===== class |probability ---------------+----------- Iris-setosa | 0.41500 Iris-versicolor| 0.19500 Iris-virginica | 0.39000 =========== call_string =========== naive_bayes('"public"."_verticapy_tmp_naivebayes_v_mldb_3135b3cc979a11efa8720242ac120002_"', '"public"."_verticapy_tmp_view_v_mldb_316d4026979a11efa8720242ac120002_"', '"species"', '"SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm"' USING PARAMETERS exclude_columns='', alpha=1) ==================== gaussian.Iris-setosa ==================== index| mu |sigma_sq -----+--------+-------- 1 | 4.08554| 0.79369 2 | 3.98675| 0.38507 3 | 3.71687| 4.26044 4 | 4.34217|14.33637 ======================== gaussian.Iris-versicolor ======================== index| mu |sigma_sq -----+--------+-------- 1 | 5.91026| 0.28094 2 | 2.75385| 0.09571 3 | 4.22564| 0.22933 4 | 1.32051| 0.04009 ======================= gaussian.Iris-virginica ======================= index| mu |sigma_sq -----+--------+-------- 1 | 5.54744| 1.53006 2 | 3.74872| 0.80669 3 | 7.38077| 4.23846 4 | 1.91667| 0.05517 =============== Additional Info =============== Name | Value ------------------+-------- alpha | 1.00000 accepted_row_count| 200 rejected_row_count| 0
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.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.9976689976689976 0.9983766233766234 0.9986818736818736 0.9987728937728937 [null] prc_auc 1.0 0.9920798898071624 0.9979787998562702 0.996686229887811 0.9973682476943346 [null] accuracy 1.0 0.98 0.98 0.9866666666666667 0.9868000000000001 0.9866666666666667 log_loss 0.00109386792206096 0.0172986331108811 0.0178135394391364 0.012068680157359487 0.012015571731114585 [null] precision 1.0 0.9166666666666666 1.0 0.9722222222222222 0.9816666666666666 0.98 recall 1.0 1.0 0.9545454545454546 0.9848484848484849 0.98 0.98 f1_score 1.0 0.9565217391304348 0.9767441860465117 0.9777553083923155 0.9802022244691608 0.98 mcc 1.0 0.9450726919990827 0.9600156738532383 0.9683627886174403 0.9703228887352231 0.97 informedness 1.0 0.9743589743589745 0.9545454545454546 0.9763014763014763 0.9743589743589743 0.97 markedness 1.0 0.9166666666666665 0.9655172413793105 0.960727969348659 0.9664942528735634 0.97 csi 1.0 0.9166666666666666 0.9545454545454546 0.9570707070707071 0.9616666666666666 0.9607843137254902 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.9976689976689976 0.9983766233766234 0.9986818736818736 0.9987728937728937 [null] prc_auc 1.0 0.9920798898071624 0.9979787998562702 0.996686229887811 0.9973682476943346 [null] accuracy 1.0 0.98 0.98 0.9866666666666667 0.9868000000000001 0.9866666666666667 log_loss 0.00109386792206096 0.0172986331108811 0.0178135394391364 0.012068680157359487 0.012015571731114585 [null] precision 1.0 0.9166666666666666 0.9565217391304348 0.9577294685990339 0.962536231884058 0.9615384615384616 recall 1.0 1.0 1.0 1.0 1.0 1.0 f1_score 1.0 0.9565217391304348 0.9777777777777777 0.9780998389694041 0.9806570048309178 0.9803921568627451 mcc 1.0 0.9450726919990827 0.9603958811069554 0.9684895243686794 0.9704901799268586 0.9707253433941511 informedness 1.0 0.9743589743589745 0.9642857142857144 0.9795482295482296 0.9786446886446888 0.98 markedness 1.0 0.9166666666666665 0.9565217391304348 0.9577294685990339 0.962536231884058 0.9615384615384617 csi 1.0 0.9166666666666666 0.9565217391304348 0.9577294685990339 0.962536231884058 0.9615384615384616 Rows: 1-11 | Columns: 7You can also use the
NaiveBayes.scorefunction to compute any classification metric. The default metric is the accuracy:model.score(metric = "f1", average = "macro") Out[4]: 0.9777553083923155
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.6 3.6 1.0 0.2 Iris-setosa Iris-setosa 2 4.8 3.4 1.9 0.2 Iris-setosa Iris-setosa 3 5.0 2.3 3.3 1.0 Iris-versicolor Iris-versicolor 4 5.1 3.5 1.4 0.2 Iris-setosa Iris-setosa 5 6.1 3.0 4.6 1.4 Iris-versicolor Iris-versicolor 6 6.4 2.9 4.3 1.3 Iris-versicolor Iris-versicolor 7 6.7 3.0 5.0 1.7 Iris-versicolor Iris-versicolor 8 6.8 3.2 5.9 2.3 Iris-virginica Iris-virginica 9 7.1 3.0 5.9 2.1 Iris-virginica Iris-virginica 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-setosa 16 5.0 3.5 1.3 0.3 Iris-setosa Iris-setosa 17 5.0 3.5 1.6 0.6 Iris-setosa Iris-setosa 18 5.1 3.5 1.4 0.3 Iris-setosa Iris-setosa 19 5.5 2.6 4.4 1.2 Iris-versicolor Iris-versicolor 20 6.0 3.4 4.5 1.6 Iris-versicolor Iris-versicolor 21 6.1 2.8 4.0 1.3 Iris-versicolor Iris-versicolor 22 6.3 2.5 5.0 1.9 Iris-virginica Iris-virginica 23 6.9 3.1 5.1 2.3 Iris-virginica Iris-virginica 24 5.2 3.5 1.5 0.2 Iris-setosa Iris-setosa 25 5.2 4.1 1.5 0.1 Iris-setosa Iris-setosa 26 5.5 3.5 1.3 0.2 Iris-setosa Iris-setosa 27 6.1 2.8 4.7 1.2 Iris-versicolor Iris-versicolor 28 6.3 2.3 4.4 1.3 Iris-versicolor Iris-versicolor 29 6.3 2.7 4.9 1.8 Iris-virginica Iris-versicolor 30 6.9 3.1 5.4 2.1 Iris-virginica Iris-virginica 31 6.9 3.2 5.7 2.3 Iris-virginica Iris-virginica 32 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 33 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 34 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 35 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 36 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 37 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 38 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 39 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 40 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 41 4.4 3.2 1.3 0.2 Iris-setosa Iris-setosa 42 5.5 4.2 1.4 0.2 Iris-setosa Iris-setosa 43 5.7 2.8 4.5 1.3 Iris-versicolor Iris-versicolor 44 6.4 3.2 4.5 1.5 Iris-versicolor Iris-versicolor 45 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 46 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 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 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", )
123SepalLengthCm123SepalWidthCm123PetalLengthCm123PetalWidthCmAbcSpeciesAbcpredictionAbcprediction_irissetosaAbcprediction_irisversicolorAbcprediction_irisvirginica1 4.6 3.6 1.0 0.2 Iris-setosa Iris-setosa 1 1.99673e-17 7.13812e-13 2 4.8 3.4 1.9 0.2 Iris-setosa Iris-setosa 1 1.12607e-11 2.45985e-12 3 5.0 2.3 3.3 1.0 Iris-versicolor Iris-versicolor 0.0220485 0.97766 0.000291911 4 5.1 3.5 1.4 0.2 Iris-setosa Iris-setosa 1 8.48865e-14 2.24375e-12 5 6.1 3.0 4.6 1.4 Iris-versicolor Iris-versicolor 0.000245307 0.996889 0.00286587 6 6.4 2.9 4.3 1.3 Iris-versicolor Iris-versicolor 6.93524e-05 0.999395 0.000535208 7 6.7 3.0 5.0 1.7 Iris-versicolor Iris-versicolor 0.000964116 0.545336 0.4537 8 6.8 3.2 5.9 2.3 Iris-virginica Iris-virginica 0.00277814 2.04969e-07 0.997222 9 7.1 3.0 5.9 2.1 Iris-virginica Iris-virginica 0.000319768 6.21202e-06 0.999674 10 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 1 2.74274e-239 1.97778e-136 11 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 1 2.74274e-239 1.97778e-136 12 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 1 2.74274e-239 1.97778e-136 13 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 1 2.74274e-239 1.97778e-136 14 3.3 4.5 5.6 7.8 Iris-setosa Iris-setosa 1 2.74274e-239 1.97778e-136 15 4.3 3.0 1.1 0.1 Iris-setosa Iris-setosa 1 6.82381e-17 4.78456e-14 16 5.0 3.5 1.3 0.3 Iris-setosa Iris-setosa 1 2.34306e-13 3.51505e-11 17 5.0 3.5 1.6 0.6 Iris-setosa Iris-setosa 1 4.68594e-09 1.22179e-07 18 5.1 3.5 1.4 0.3 Iris-setosa Iris-setosa 1 1.19124e-12 4.4719e-11 19 5.5 2.6 4.4 1.2 Iris-versicolor Iris-versicolor 0.000237094 0.999595 0.000168385 20 6.0 3.4 4.5 1.6 Iris-versicolor Iris-versicolor 0.00806317 0.816023 0.175914 21 6.1 2.8 4.0 1.3 Iris-versicolor Iris-versicolor 8.38083e-05 0.999612 0.000303761 22 6.3 2.5 5.0 1.9 Iris-virginica Iris-virginica 0.00114306 0.191146 0.807711 23 6.9 3.1 5.1 2.3 Iris-virginica Iris-virginica 0.00368549 3.16115e-05 0.996283 24 5.2 3.5 1.5 0.2 Iris-setosa Iris-setosa 1 4.04714e-13 2.87186e-12 25 5.2 4.1 1.5 0.1 Iris-setosa Iris-setosa 1 2.38416e-17 8.65558e-14 26 5.5 3.5 1.3 0.2 Iris-setosa Iris-setosa 1 1.12599e-13 4.04774e-12 27 6.1 2.8 4.7 1.2 Iris-versicolor Iris-versicolor 0.000128818 0.99961 0.000260846 28 6.3 2.3 4.4 1.3 Iris-versicolor Iris-versicolor 2.48038e-05 0.999365 0.000610549 29 6.3 2.7 4.9 1.8 Iris-virginica Iris-versicolor 0.00105369 0.600495 0.398451 30 6.9 3.1 5.4 2.1 Iris-virginica Iris-virginica 0.000991242 0.000205446 0.998803 31 6.9 3.2 5.7 2.3 Iris-virginica Iris-virginica 0.00253763 6.79503e-07 0.997462 32 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 33 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 34 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 35 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 36 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 37 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 38 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 39 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 40 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 41 4.4 3.2 1.3 0.2 Iris-setosa Iris-setosa 1 8.42109e-15 1.12566e-12 42 5.5 4.2 1.4 0.2 Iris-setosa Iris-setosa 1 9.59925e-17 3.1532e-12 43 5.7 2.8 4.5 1.3 Iris-versicolor Iris-versicolor 0.000210141 0.999274 0.000515488 44 6.4 3.2 4.5 1.5 Iris-versicolor Iris-versicolor 0.000621606 0.977122 0.0222561 45 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 46 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 47 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 48 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 49 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 50 4.3 4.7 9.6 1.8 Iris-virginica Iris-virginica 0.00534796 6.42903e-38 0.994652 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([[17, 0, 0], [ 0, 11, 0], [ 0, 1, 21]])
Hint
In the context of multi-class classification, you typically work with an overall confusion matrix that summarizes the classification efficiency across all classes. However, you have the flexibility to specify a
pos_labeland adjust the cutoff threshold. In this case, a binary confusion matrix is computed, where the chosen class is treated as the positive class, allowing you to evaluate its efficiency as if it were a binary classification problem.model.confusion_matrix(pos_label = "Iris-setosa", cutoff = 0.6) Out[6]: array([[33, 0], [ 0, 17]])
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]: {'alpha': 1.0, 'nbtype': 'auto'}
And to manually change some of the parameters:
model.set_params({'alpha': 0.9})
Model Register¶
In order to register the model for tracking and versioning:
model.register("model_v1")
Please refer to /notebooks/ml/model_tracking_versioning/index.ipynb for more details on model tracking and versioning.
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 0.32251957572020434 * EXP(- POWER("SepalLengthCm" - 5.5474358974359, 2) / 3.06011655011654) * 0.44417860091853717 * EXP(- POWER("SepalWidthCm" - 3.74871794871795, 2) / 1.613373293373328) * 0.19377877157815893 * EXP(- POWER("PetalLengthCm" - 7.38076923076923, 2) / 8.47691308691294) * 1.698424069445676 * EXP(- POWER("PetalWidthCm" - 1.91666666666667, 2) / 0.1103463203463292) * 0.39 >= 0.4478002956817784 * EXP(- POWER("SepalLengthCm" - 4.0855421686747, 2) / 1.58738172200995) * 0.642898499888941 * EXP(- POWER("SepalWidthCm" - 3.98674698795181, 2) / 0.77013223626212) * 0.19327810066060777 * EXP(- POWER("PetalLengthCm" - 3.71686746987952, 2) / 8.520887452248) * 0.1053635639885031 * EXP(- POWER("PetalWidthCm" - 4.3421686746988, 2) / 28.6727416985012) * 0.415 AND 0.32251957572020434 * EXP(- POWER("SepalLengthCm" - 5.5474358974359, 2) / 3.06011655011654) * 0.44417860091853717 * EXP(- POWER("SepalWidthCm" - 3.74871794871795, 2) / 1.613373293373328) * 0.19377877157815893 * EXP(- POWER("PetalLengthCm" - 7.38076923076923, 2) / 8.47691308691294) * 1.698424069445676 * EXP(- POWER("PetalWidthCm" - 1.91666666666667, 2) / 0.1103463203463292) * 0.39 >= 0.7526614411763235 * EXP(- POWER("SepalLengthCm" - 5.91025641025641, 2) / 0.561889338731422) * 1.2895399654494928 * EXP(- POWER("SepalWidthCm" - 2.75384615384615, 2) / 0.191417004048581) * 0.8330750754134963 * EXP(- POWER("PetalLengthCm" - 4.22564102564103, 2) / 0.45865047233468) * 1.9923601374340967 * EXP(- POWER("PetalWidthCm" - 1.32051282051282, 2) / 0.0801889338731448) * 0.195 THEN \'Iris-virginica\' WHEN 0.7526614411763235 * EXP(- POWER("SepalLengthCm" - 5.91025641025641, 2) / 0.561889338731422) * 1.2895399654494928 * EXP(- POWER("SepalWidthCm" - 2.75384615384615, 2) / 0.191417004048581) * 0.8330750754134963 * EXP(- POWER("PetalLengthCm" - 4.22564102564103, 2) / 0.45865047233468) * 1.9923601374340967 * EXP(- POWER("PetalWidthCm" - 1.32051282051282, 2) / 0.0801889338731448) * 0.195 >= 0.4478002956817784 * EXP(- POWER("SepalLengthCm" - 4.0855421686747, 2) / 1.58738172200995) * 0.642898499888941 * EXP(- POWER("SepalWidthCm" - 3.98674698795181, 2) / 0.77013223626212) * 0.19327810066060777 * EXP(- POWER("PetalLengthCm" - 3.71686746987952, 2) / 8.520887452248) * 0.1053635639885031 * EXP(- POWER("PetalWidthCm" - 4.3421686746988, 2) / 28.6727416985012) * 0.415 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='<U15')
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, alpha: Annotated[int | float | Decimal, 'Python Numbers'] = 1.0, nbtype: Literal['auto', 'bernoulli', 'categorical', 'multinomial', 'gaussian'] = 'auto') None¶
Must be overridden in the child class
Methods
__init__([name, overwrite_model, alpha, nbtype])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()Drops the model from the Vertica 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, 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