vastorbit.machine_learning.vast.naive_bayes.NaiveBayes¶
- class vastorbit.machine_learning.vast.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 an
NaiveBayesobject usingscikit-learnfor training and the scalability of VAST DataBase for the inferences.- 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.**kwargs (
scikit-learnmodel parameters.)
- Variables:
created (Many attributes are)
phase. (during the fitting)
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.
Examples
The following examples provide a basic understanding of usage. For more detailed examples, please refer to the Machine Learning or the Examples section on the website.
Load data for machine learning¶
We import
vastorbit:import vastorbit as vo
Hint
By assigning an alias to
vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions fromvastorbitare used as intended without interfering with functions from other libraries.For this example, we will use the iris dataset.
import vastorbit.datasets as vod data = vod.load_iris()
123sepallengthcmDecimal(5, 2)123sepalwidthcmDecimal(5, 2)123petallengthcmDecimal(5, 2)123petalwidthcmDecimal(5, 2)AbcspeciesVarchar(30)1 5.1 3.5 1.4 0.2 Iris-setosa 2 4.9 3.0 1.4 0.2 Iris-setosa 3 4.7 3.2 1.3 0.2 Iris-setosa 4 4.6 3.1 1.5 0.2 Iris-setosa 5 5.0 3.6 1.4 0.2 Iris-setosa 6 5.4 3.9 1.7 0.4 Iris-setosa 7 4.6 3.4 1.4 0.3 Iris-setosa 8 5.0 3.4 1.5 0.2 Iris-setosa 9 4.4 2.9 1.4 0.2 Iris-setosa 10 4.9 3.1 1.5 0.1 Iris-setosa 11 5.4 3.7 1.5 0.2 Iris-setosa 12 4.8 3.4 1.6 0.2 Iris-setosa 13 4.8 3.0 1.4 0.1 Iris-setosa 14 4.3 3.0 1.1 0.1 Iris-setosa 15 5.8 4.0 1.2 0.2 Iris-setosa 16 5.7 4.4 1.5 0.4 Iris-setosa 17 5.4 3.9 1.3 0.4 Iris-setosa 18 5.1 3.5 1.4 0.3 Iris-setosa 19 5.7 3.8 1.7 0.3 Iris-setosa 20 5.1 3.8 1.5 0.3 Iris-setosa 21 5.4 3.4 1.7 0.2 Iris-setosa 22 5.1 3.7 1.5 0.4 Iris-setosa 23 4.6 3.6 1.0 0.2 Iris-setosa 24 5.1 3.3 1.7 0.5 Iris-setosa 25 4.8 3.4 1.9 0.2 Iris-setosa 26 5.0 3.0 1.6 0.2 Iris-setosa 27 5.0 3.4 1.6 0.4 Iris-setosa 28 5.2 3.5 1.5 0.2 Iris-setosa 29 5.2 3.4 1.4 0.2 Iris-setosa 30 4.7 3.2 1.6 0.2 Iris-setosa 31 4.8 3.1 1.6 0.2 Iris-setosa 32 5.4 3.4 1.5 0.4 Iris-setosa 33 5.2 4.1 1.5 0.1 Iris-setosa 34 5.5 4.2 1.4 0.2 Iris-setosa 35 4.9 3.1 1.5 0.1 Iris-setosa 36 5.0 3.2 1.2 0.2 Iris-setosa 37 5.5 3.5 1.3 0.2 Iris-setosa 38 4.9 3.1 1.5 0.1 Iris-setosa 39 4.4 3.0 1.3 0.2 Iris-setosa 40 5.1 3.4 1.5 0.2 Iris-setosa 41 5.0 3.5 1.3 0.3 Iris-setosa 42 4.5 2.3 1.3 0.3 Iris-setosa 43 4.4 3.2 1.3 0.2 Iris-setosa 44 5.0 3.5 1.6 0.6 Iris-setosa 45 5.1 3.8 1.9 0.4 Iris-setosa 46 4.8 3.0 1.4 0.3 Iris-setosa 47 5.1 3.8 1.6 0.2 Iris-setosa 48 4.6 3.2 1.4 0.2 Iris-setosa 49 5.3 3.7 1.5 0.2 Iris-setosa 50 5.0 3.3 1.4 0.2 Iris-setosa 51 7.0 3.2 4.7 1.4 Iris-versicolor 52 6.4 3.2 4.5 1.5 Iris-versicolor 53 6.9 3.1 4.9 1.5 Iris-versicolor 54 5.5 2.3 4.0 1.3 Iris-versicolor 55 6.5 2.8 4.6 1.5 Iris-versicolor 56 5.7 2.8 4.5 1.3 Iris-versicolor 57 6.3 3.3 4.7 1.6 Iris-versicolor 58 4.9 2.4 3.3 1.0 Iris-versicolor 59 6.6 2.9 4.6 1.3 Iris-versicolor 60 5.2 2.7 3.9 1.4 Iris-versicolor 61 5.0 2.0 3.5 1.0 Iris-versicolor 62 5.9 3.0 4.2 1.5 Iris-versicolor 63 6.0 2.2 4.0 1.0 Iris-versicolor 64 6.1 2.9 4.7 1.4 Iris-versicolor 65 5.6 2.9 3.6 1.3 Iris-versicolor 66 6.7 3.1 4.4 1.4 Iris-versicolor 67 5.6 3.0 4.5 1.5 Iris-versicolor 68 5.8 2.7 4.1 1.0 Iris-versicolor 69 6.2 2.2 4.5 1.5 Iris-versicolor 70 5.6 2.5 3.9 1.1 Iris-versicolor 71 5.9 3.2 4.8 1.8 Iris-versicolor 72 6.1 2.8 4.0 1.3 Iris-versicolor 73 6.3 2.5 4.9 1.5 Iris-versicolor 74 6.1 2.8 4.7 1.2 Iris-versicolor 75 6.4 2.9 4.3 1.3 Iris-versicolor 76 6.6 3.0 4.4 1.4 Iris-versicolor 77 6.8 2.8 4.8 1.4 Iris-versicolor 78 6.7 3.0 5.0 1.7 Iris-versicolor 79 6.0 2.9 4.5 1.5 Iris-versicolor 80 5.7 2.6 3.5 1.0 Iris-versicolor 81 5.5 2.4 3.8 1.1 Iris-versicolor 82 5.5 2.4 3.7 1.0 Iris-versicolor 83 5.8 2.7 3.9 1.2 Iris-versicolor 84 6.0 2.7 5.1 1.6 Iris-versicolor 85 5.4 3.0 4.5 1.5 Iris-versicolor 86 6.0 3.4 4.5 1.6 Iris-versicolor 87 6.7 3.1 4.7 1.5 Iris-versicolor 88 6.3 2.3 4.4 1.3 Iris-versicolor 89 5.6 3.0 4.1 1.3 Iris-versicolor 90 5.5 2.5 4.0 1.3 Iris-versicolor 91 5.5 2.6 4.4 1.2 Iris-versicolor 92 6.1 3.0 4.6 1.4 Iris-versicolor 93 5.8 2.6 4.0 1.2 Iris-versicolor 94 5.0 2.3 3.3 1.0 Iris-versicolor 95 5.6 2.7 4.2 1.3 Iris-versicolor 96 5.7 3.0 4.2 1.2 Iris-versicolor 97 5.7 2.9 4.2 1.3 Iris-versicolor 98 6.2 2.9 4.3 1.3 Iris-versicolor 99 5.1 2.5 3.0 1.1 Iris-versicolor 100 5.7 2.8 4.1 1.3 Iris-versicolor Rows: 1-100 | Columns: 5Note
vastorbit offers a wide range of sample datasets that are ideal for training and testing purposes. You can explore the full list of available datasets in the Datasets, which provides detailed information on each dataset and how to use them effectively. These datasets are invaluable resources for honing your data analysis and machine learning skills within the vastorbit environment.
You can easily divide your dataset into training and testing subsets using the
VastFrame.train_test_split()method. This is a crucial step when preparing your data for machine learning, as it allows you to evaluate the performance of your models accurately.data = vod.load_iris() train, test = data.train_test_split(test_size = 0.2)
Warning
In this case, vastorbit utilizes seeded randomization to guarantee the reproducibility of your data split. However, please be aware that this approach may lead to reduced performance. For a more efficient data split, you can use the
VastFrame.to_db()method to save your results intotablesortemporary tables. This will help enhance the overall performance of the process.Balancing the Dataset¶
In vastorbit, balancing a dataset to address class imbalances is made straightforward through the
balance()function within thepreprocessingmodule. This function enables users to rectify skewed class distributions efficiently. By specifying the target variable and setting parameters like the method for balancing, users can effortlessly achieve a more equitable representation of classes in their dataset. Whether opting for over-sampling, under-sampling, or a combination of both, vastorbit’sbalance()function streamlines the process, empowering users to enhance the performance and fairness of their machine learning models trained on imbalanced data.To balance the dataset, use the following syntax.
from vastorbit.machine_learning.vast.preprocessing import balance balanced_train = balance( name = "my_schema.train_balanced", input_relation = train, y = "good", method = "hybrid", )
Note
With this code, a table named train_balanced is created in the my_schema schema. It can then be used to train the model. In the rest of the example, we will work with the full dataset.
Hint
Balancing the dataset is a crucial step in improving the accuracy of machine learning models, particularly when faced with imbalanced class distributions. By addressing disparities in the number of instances across different classes, the model becomes more adept at learning patterns from all classes rather than being biased towards the majority class. This, in turn, enhances the model’s ability to make accurate predictions for under-represented classes. The balanced dataset ensures that the model is not dominated by the majority class and, as a result, leads to more robust and unbiased model performance. Therefore, by employing techniques such as over-sampling, under-sampling, or a combination of both during dataset preparation, practitioners can significantly contribute to achieving higher accuracy and better generalization of their machine learning models.
Model Initialization¶
First we import the
NaiveBayesmodel:from vastorbit.machine_learning.vast import NaiveBayes
Then we can create the model:
model = NaiveBayes()
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, )
Important
To train a model, you can directly use the
VastFrameor the name of the relation stored in the database. The test set is optional and is only used to compute the test metrics. Invastorbit, we don’t work usingXmatrices andyvectors. Instead, we work directly with lists of predictors and the response name.Metrics¶
We can get the entire report using:
model.report()
Iris-setosa Iris-versicolor Iris-virginica avg_macro avg_weighted avg_micro auc 0.9252136752136754 0.9615384615384616 1.0 0.9622507122507123 0.9587813620071686 [null] prc_auc 0.45278119672746187 0.17613927291346643 0.2760989010989011 0.30167312357994314 0.33654388076623154 [null] accuracy 0.9354838709677419 0.967741935483871 0.9032258064516129 0.935483870967742 0.9302809573361082 0.9354838709677419 log_loss 6.403479896870502e-15 0.19892365592416997 6.696425791762324 2.2984498158955 2.4146524401590543 [null] precision 1.0 0.8571428571428571 0.8333333333333334 0.8968253968253969 0.91321044546851 0.9032258064516129 recall 0.8571428571428571 1.0 0.9090909090909091 0.9220779220779222 0.9032258064516129 0.9032258064516129 f1_score 0.923076923076923 0.923076923076923 0.8695652173913043 0.9052396878483835 0.9040888984788003 0.9032258064516129 mcc 0.875738141931184 0.9071147352221454 0.7947695843356161 0.8592074871629819 0.8530802524535879 0.8548387096774194 informedness 0.8571428571428572 0.96 0.8090909090909091 0.8754112554112554 0.8599999999999999 0.8548387096774195 markedness 0.8947368421052633 0.8571428571428572 0.7807017543859649 0.8441938178780285 0.846996523566982 0.8548387096774195 csi 0.8571428571428571 0.8571428571428571 0.7692307692307693 0.8278388278388279 0.8259482453030841 0.8235294117647058 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 0.9252136752136754 0.97 0.7894736842105263 0.8948957864747339 0.8784667397660819 [null] prc_auc 0.40198511166253104 0.2806451612903227 0.17331029185867886 0.2853135216038442 0.28633472542242705 [null] accuracy 0.967741935483871 0.41935483870967744 0.1935483870967742 0.5268817204301076 0.5504032258064516 0.5268817204301075 log_loss 1.1531886556987614e-12 0.011501328231786097 0.19892365592418046 0.07014166138570659 0.08296923426312669 [null] precision 1.0 0.0 0.0 0.3333333333333333 0.40625 0.3333333333333333 recall 0.9230769230769231 0.0 0.0 0.3076923076923077 0.375 0.375 f1_score 0.9600000000000001 0.0 0.0 0.32 0.39 0.35294117647058826 mcc 0.935143800239036 -0.3893314107138301 -0.6753816335059704 -0.04318974799358818 0.03252874072646478 -0.017987628765650892 informedness 0.9230769230769231 -0.48 -0.6666666666666667 -0.07452991452991453 0.01416666666666666 -0.018442622950819665 markedness 0.9473684210526314 -0.3157894736842105 -0.6842105263157895 -0.01754385964912286 0.047697368421052544 -0.01754385964912286 csi 0.9230769230769231 0.0 0.0 0.3076923076923077 0.375 0.21428571428571427 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")
Note
For multi-class scoring,
vastorbitallows the flexibility to use three averaging techniques:micro,macroandweighted. Please refer to this link for more details on how they are calculated.Prediction¶
Prediction is straight-forward:
model.predict( test, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "prediction", )
123sepallengthcmDecimal(5, 2)123sepalwidthcmDecimal(5, 2)123petallengthcmDecimal(5, 2)123petalwidthcmDecimal(5, 2)AbcspeciesVarchar(30)123seedrandDecimal(26, 6)AbcpredictionVarchar(15)1 3.3 4.5 5.6 7.8 Iris-setosa 0.1 Iris-virginica 2 5.1 3.5 1.4 0.2 Iris-setosa 0.01 Iris-setosa 3 4.6 3.1 1.5 0.2 Iris-setosa 0.06 Iris-setosa 4 4.8 3.0 1.4 0.1 Iris-setosa 0.07 Iris-setosa 5 5.4 3.9 1.3 0.4 Iris-setosa 0.07 Iris-setosa 6 5.7 3.8 1.7 0.3 Iris-setosa 0.11 Iris-setosa 7 4.6 3.6 1.0 0.2 Iris-setosa 0.16 Iris-setosa 8 4.7 3.2 1.6 0.2 Iris-setosa 0.04 Iris-setosa 9 5.4 3.4 1.5 0.4 Iris-setosa 0.04 Iris-setosa 10 4.9 3.1 1.5 0.1 Iris-setosa 0.12 Iris-setosa 11 5.5 3.5 1.3 0.2 Iris-setosa 0.15 Iris-setosa 12 4.9 3.1 1.5 0.1 Iris-setosa 0.16 Iris-setosa 13 4.8 3.0 1.4 0.3 Iris-setosa 0.0 Iris-setosa 14 7.0 3.2 4.7 1.4 Iris-versicolor 0.08 Iris-versicolor 15 5.7 2.8 4.5 1.3 Iris-versicolor 0.06 Iris-versicolor 16 6.1 2.9 4.7 1.4 Iris-versicolor 0.05 Iris-versicolor 17 6.4 2.9 4.3 1.3 Iris-versicolor 0.07 Iris-versicolor 18 5.5 2.4 3.8 1.1 Iris-versicolor 0.03 Iris-versicolor 19 5.7 3.0 4.2 1.2 Iris-versicolor 0.16 Iris-versicolor 20 5.8 2.7 5.1 1.9 Iris-virginica 0.05 Iris-virginica 21 4.9 2.5 4.5 1.7 Iris-virginica 0.12 Iris-versicolor 22 5.7 2.5 5.0 2.0 Iris-virginica 0.01 Iris-virginica 23 7.7 3.8 6.7 2.2 Iris-virginica 0.01 Iris-virginica 24 5.6 2.8 4.9 2.0 Iris-virginica 0.11 Iris-virginica 25 6.7 3.3 5.7 2.1 Iris-virginica 0.04 Iris-virginica 26 7.2 3.0 5.8 1.6 Iris-virginica 0.09 Iris-virginica 27 7.4 2.8 6.1 1.9 Iris-virginica 0.1 Iris-virginica 28 7.9 3.8 6.4 2.0 Iris-virginica 0.16 Iris-virginica 29 6.0 3.0 4.8 1.8 Iris-virginica 0.03 Iris-virginica 30 6.7 3.1 5.6 2.4 Iris-virginica 0.04 Iris-virginica 31 6.8 3.2 5.9 2.3 Iris-virginica 0.15 Iris-virginica Rows: 1-31 | Columns: 7Note
Predictions can be made automatically using the test set, in which case you don’t need to specify the predictors. Alternatively, you can pass only the
VastFrameto thepredict()function, but in this case, it’s essential that the column names of theVastFramematch the predictors and response name in the model.Probabilities¶
It is also easy to get the model’s probabilities:
model.predict_proba( test, [ "SepalLengthCm", "SepalWidthCm", "PetalLengthCm", "PetalWidthCm", ], "prediction", )
123sepallengthcmDecimal(5, 2)123sepalwidthcmDecimal(5, 2)123petallengthcmDecimal(5, 2)123petalwidthcmDecimal(5, 2)AbcspeciesVarchar(30)123seedrandDecimal(26, 6)AbcpredictionVarchar(15)123prediction_irissetosaDouble123prediction_irisversicolorDouble123prediction_irisvirginicaDouble1 3.3 4.5 5.6 7.8 Iris-setosa 0.1 Iris-virginica 0.0 0.0 0.0 2 4.9 3.0 1.4 0.2 Iris-setosa 0.01 Iris-setosa 1.0 1.0 1.0 3 5.0 3.6 1.4 0.2 Iris-setosa 0.06 Iris-setosa 1.0 1.0 1.0 4 4.3 3.0 1.1 0.1 Iris-setosa 0.07 Iris-setosa 1.0 1.0 1.0 5 5.1 3.5 1.4 0.3 Iris-setosa 0.07 Iris-setosa 0.9999999999999997 0.9999999999999997 0.9999999999999997 6 5.1 3.8 1.5 0.3 Iris-setosa 0.11 Iris-setosa 0.9999999999999996 0.9999999999999996 0.9999999999999996 7 5.1 3.3 1.7 0.5 Iris-setosa 0.16 Iris-setosa 0.9999999999642523 0.9999999999642523 0.9999999999642523 8 4.8 3.1 1.6 0.2 Iris-setosa 0.04 Iris-setosa 0.9999999999999997 0.9999999999999997 0.9999999999999997 9 5.2 4.1 1.5 0.1 Iris-setosa 0.04 Iris-setosa 1.0 1.0 1.0 10 5.0 3.2 1.2 0.2 Iris-setosa 0.12 Iris-setosa 1.0 1.0 1.0 11 4.9 3.1 1.5 0.1 Iris-setosa 0.15 Iris-setosa 1.0 1.0 1.0 12 4.4 3.0 1.3 0.2 Iris-setosa 0.16 Iris-setosa 1.0 1.0 1.0 13 5.1 3.8 1.6 0.2 Iris-setosa 0.0 Iris-setosa 1.0 1.0 1.0 14 6.4 3.2 4.5 1.5 Iris-versicolor 0.08 Iris-versicolor 1.4024046089753773e-102 1.4024046089753773e-102 1.4024046089753773e-102 15 6.3 3.3 4.7 1.6 Iris-versicolor 0.06 Iris-versicolor 2.955498914528877e-116 2.955498914528877e-116 2.955498914528877e-116 16 5.6 2.9 3.6 1.3 Iris-versicolor 0.05 Iris-versicolor 1.9152980936586173e-57 1.9152980936586173e-57 1.9152980936586173e-57 17 6.6 3.0 4.4 1.4 Iris-versicolor 0.07 Iris-versicolor 1.8746957658747432e-94 1.8746957658747432e-94 1.8746957658747432e-94 18 5.5 2.4 3.7 1.0 Iris-versicolor 0.03 Iris-versicolor 6.792244965186134e-48 6.792244965186134e-48 6.792244965186134e-48 19 5.7 2.9 4.2 1.3 Iris-versicolor 0.16 Iris-versicolor 3.370177313986364e-78 3.370177313986364e-78 3.370177313986364e-78 20 7.1 3.0 5.9 2.1 Iris-virginica 0.05 Iris-virginica 6.944055988322943e-222 6.944055988322943e-222 6.944055988322943e-222 21 7.3 2.9 6.3 1.8 Iris-virginica 0.12 Iris-virginica 6.145624281196476e-225 6.145624281196476e-225 6.145624281196476e-225 22 5.8 2.8 5.1 2.4 Iris-virginica 0.01 Iris-virginica 2.4654726407268578e-198 2.4654726407268578e-198 2.4654726407268578e-198 23 7.7 2.6 6.9 2.3 Iris-virginica 0.01 Iris-virginica 1.50907293132e-312 1.50907293132e-312 1.50907293132e-312 24 7.7 2.8 6.7 2.0 Iris-virginica 0.11 Iris-virginica 1.974774735118236e-271 1.974774735118236e-271 1.974774735118236e-271 25 7.2 3.2 6.0 1.8 Iris-virginica 0.04 Iris-virginica 1.1100389392256957e-204 1.1100389392256957e-204 1.1100389392256957e-204 26 7.4 2.8 6.1 1.9 Iris-virginica 0.09 Iris-virginica 1.6854017652731458e-220 1.6854017652731458e-220 1.6854017652731458e-220 27 7.9 3.8 6.4 2.0 Iris-virginica 0.1 Iris-virginica 7.562531432181313e-252 7.562531432181313e-252 7.562531432181313e-252 28 6.4 2.8 5.6 2.2 Iris-virginica 0.16 Iris-virginica 6.96680243706805e-209 6.96680243706805e-209 6.96680243706805e-209 29 6.9 3.1 5.4 2.1 Iris-virginica 0.03 Iris-virginica 1.0247164094833497e-191 1.0247164094833497e-191 1.0247164094833497e-191 30 6.9 3.1 5.1 2.3 Iris-virginica 0.04 Iris-virginica 4.634820040037661e-194 4.634820040037661e-194 4.634820040037661e-194 31 6.7 3.3 5.7 2.5 Iris-virginica 0.15 Iris-virginica 1.3665154659861449e-244 1.3665154659861449e-244 1.3665154659861449e-244 Rows: 1-31 | Columns: 10Note
Probabilities are added to the
VastFrame, and vastorbit uses the corresponding probability function in SQL behind the scenes. You can use thepos_labelparameter to add only the probability of the selected category.Confusion Matrix¶
You can obtain the confusion matrix.
model.confusion_matrix()
Hint
In the context of multi-class classification, you typically work with an overall confusion matrix that summarizes the classification efficiency across all classes. However, you have the flexibility to specify a
pos_labeland adjust the cutoff threshold. In this case, a binary confusion matrix is computed, where the chosen class is treated as the positive class, allowing you to evaluate its efficiency as if it were a binary classification problem.model.confusion_matrix(pos_label = "Iris-setosa", cutoff = 0.6)
Note
In classification, the
cutoffis a threshold value used to determine class assignment based on predicted probabilities or scores from a classification model. In binary classification, if the predicted probability for a specific class is greater than or equal to the cutoff, the instance is assigned to the positive class; otherwise, it is assigned to the negative class. Adjusting the cutoff allows for trade-offs between true positives and false positives, enabling the model to be optimized for specific objectives or to consider the relative costs of different classification errors. The choice of cutoff is critical for tailoring the model’s performance to meet specific needs.Main Plots (Classification Curves)¶
Classification models allow for the creation of various plots that are very helpful in understanding the model, such as the ROC Curve, PRC Curve, Cutoff Curve, Gain Curve, and more.
Most of the classification curves can be found in the Machine Learning - Classification Curve.
For example, let’s draw the model’s ROC curve.
model.roc_curve(pos_label = "Iris-setosa")
Important
Most of the curves have a parameter called
nbins, which is essential for estimating metrics. The larger thenbins, the more precise the estimation, but it can significantly impact performance. Exercise caution when increasing this parameter excessively.Hint
In binary classification, various curves can be easily plotted. However, in multi-class classification, it’s important to select the
pos_label, representing the class to be treated as positive when drawing the curve.Other Plots¶
Contour plot is another useful plot that can be produced for models with two predictors.
model.contour(pos_label = "Iris-setosa")
Important
Machine learning models with two predictors can usually benefit from their own contour plot. This visual representation aids in exploring predictions and gaining a deeper understanding of how these models perform in different scenarios. Please refer to Contour Plot for more examples.
Parameter Modification¶
In order to see the parameters:
model.get_params()
And to manually change some of the parameters:
model.set_params({'alpha': 0.9})
Model Exporting¶
To Memmodel
model.to_memmodel()
Note
MemModelobjects serve as in-memory representations of machine learning models. They can be used for both in-database and in-memory prediction tasks. These objects can be pickled in the same way that you would pickle ascikit-learnmodel.The following methods for exporting the model use
MemModel, and it is recommended to useMemModeldirectly.To SQL
You can get the SQL code by:
model.to_sql()
To Python
To obtain the prediction function in Python syntax, use the following code:
X = [[5, 2, 3, 1]] model.to_python()(X)
Hint
The
to_python()method is used to retrieve predictions, probabilities, or cluster distances. For specific details on how to use this method for different model types, refer to the relevant documentation for each model.- __init__(name: str = None, overwrite_model: bool = False, alpha: Annotated[int | float | Decimal, 'Python Numbers'] = 1.0, nbtype: Literal['auto', 'bernoulli', 'categorical', 'multinomial', 'gaussian'] = 'auto') None¶
Methods
__init__([name, overwrite_model, alpha, nbtype])classification_report([metrics, cutoff, ...])Computes a classification report using multiple model evaluation metrics (
auc,accuracy,f1...).confusion_matrix([pos_label, cutoff])Computes the model confusion matrix.
contour([pos_label, nbins, chart])Draws the model's contour plot.
cutoff_curve([pos_label, nbins, show, chart])Draws the model Cutoff curve.
deploySQL([X, pos_label, cutoff, allSQL])Returns the SQL code needed to deploy the model.
drop()Drops the model from the VAST DataBase.
export_models(name, path[, kind])Exports machine learning models.
fit(input_relation, X, y[, test_relation, ...])Trains the model.
get_attributes([attr_name])Returns the model attributes.
get_match_index(x, col_list[, str_check])Returns the matching index.
Returns the parameters of the model.
get_plotting_lib([class_name, chart, ...])Returns the first available library (Plotly, Matplotlib) to draw a specific graphic.
import_models(path[, schema, kind])Imports machine learning models.
lift_chart([pos_label, nbins, show, chart])Draws the model Lift Chart.
prc_curve([pos_label, nbins, show, chart])Draws the model PRC curve.
predict(vdf[, X, name, cutoff, inplace])Predicts using the input relation.
predict_proba(vdf[, X, name, pos_label, inplace])Returns the model's probabilities using the input relation.
report([metrics, cutoff, labels, nbins])Computes a classification report using multiple model evaluation metrics (
auc,accuracy,f1...).roc_curve([pos_label, nbins, show, chart])Draws the model ROC curve.
score([metric, average, pos_label, cutoff, ...])Computes the model score.
set_params([parameters])Sets the parameters of the model.
Summarizes the model.
to_binary(path)Exports the model to the VAST Binary format.
Converts the model to an InMemory object that can be used for different types of predictions.
to_python([return_proba, ...])Returns the Python function needed for in-memory scoring without using built-in VAST functions.
to_sql([X, return_proba, ...])Returns the SQL code needed to deploy the model without using built-in VAST functions.
Attributes