verticapy.machine_learning.model_selection.randomized_features_search_cv¶
- verticapy.machine_learning.model_selection.randomized_features_search_cv(estimator: VerticaModel, input_relation: Annotated[str | vDataFrame, ''], X: Annotated[str | list[str], 'STRING representing one column or a list of columns'], y: str, metric: str = 'auto', cv: int = 3, average: Literal['binary', 'micro', 'macro', 'weighted'] = 'weighted', pos_label: Annotated[bool | float | str | timedelta | datetime, 'Python Scalar'] | None = None, cutoff: Annotated[int | float | Decimal, 'Python Numbers'] = -1, training_score: bool = True, comb_limit: int = 100, skip_error: bool = True, print_info: bool = True, **kwargs) TableSample¶
Computes the k-fold grid search of an estimator using different feature combinations. It can be used to find the set of variables that will optimize the model.
- estimator: VerticaModel
Vertica estimator with a fit method.
- input_relation: SQLRelation
Relation used to train the model.
- X: SQLColumns
List of the predictor columns.
- y: str
Response Column.
- metric: str, optional
Metric used for the model evaluation.
- auto:
logloss for classification & rmse for regression.
For Classification:
- accuracy:
Accuracy.
\[Accuracy = \frac{TP + TN}{TP + TN + FP + FN}\]
- auc:
Area Under the Curve (ROC).
\[AUC = \int_{0}^{1} TPR(FPR) \, dFPR\]
- ba:
Balanced Accuracy.
\[BA = \frac{TPR + TNR}{2}\]
- bm:
Informedness
\[BM = TPR + TNR - 1\]
- csi:
Critical Success Index
\[index = \frac{TP}{TP + FN + FP}\]
- f1:
F1 Score .. math:
F_1 Score = 2 \times
rac{Precision times Recall}{Precision + Recall}
- fdr:
False Discovery Rate
\[FDR = 1 - PPV\]
- fm:
Fowlkes-Mallows index
\[FM = \sqrt{PPV * TPR}\]
- fnr:
False Negative Rate
\[FNR = \frac{FN}{FN + TP}\]
- for:
False Omission Rate
\[FOR = 1 - NPV\]
- fpr:
False Positive Rate
\[FPR = \frac{FP}{FP + TN}\]
- logloss:
Log Loss
\[Loss = -\frac{1}{N} \sum_{i=1}^{N} \left( y_i \log(p_i) + (1 - y_i) \log(1 - p_i) \right)\]
- lr+:
Positive Likelihood Ratio.
\[LR+ = \frac{TPR}{FPR}\]
- lr-:
Negative Likelihood Ratio.
\[LR- = \frac{FNR}{TNR}\]
- dor:
Diagnostic Odds Ratio.
\[DOR = \frac{TP \times TN}{FP \times FN}\]
- mcc:
Matthews Correlation Coefficient
- mk:
Markedness
\[MK = PPV + NPV - 1\]
- npv:
Negative Predictive Value
\[NPV = \frac{TN}{TN + FN}\]
- prc_auc:
Area Under the Curve (PRC)
\[AUC = \int_{0}^{1} Precision(Recall) \, dRecall\]
- precision:
Precision
\[TP / (TP + FP)\]
- pt:
Prevalence Threshold.
\[\frac{\sqrt{FPR}}{\sqrt{TPR} + \sqrt{FPR}}\]
- recall:
Recall.
\[TP / (TP + FN)\]
- specificity:
Specificity.
\[TN / (TN + FP)\]
For Regression:
- max:
Max Error.
\[ME = \max_{i=1}^{n} \left| y_i - \hat{y}_i \right|\]
- mae:
Mean Absolute Error.
\[MAE = \frac{1}{n} \sum_{i=1}^{n} \left| y_i - \hat{y}_i \right|\]
- median:
Median Absolute Error.
\[MedAE = \text{median}_{i=1}^{n} \left| y_i - \hat{y}_i \right|\]
- mse:
Mean Squared Error.
\[MSE = \frac{1}{n} \sum_{i=1}^{n} \left( y_i - \hat{y}_i \right)^2\]
- msle:
Mean Squared Log Error.
\[MSLE = \frac{1}{n} \sum_{i=1}^{n} (\log(1 + y_i) - \log(1 + \hat{y}_i))^2\]
- r2:
R squared coefficient.
\[R^2 = 1 - \frac{\sum_{i=1}^{n} (y_i - \hat{y}_i)^2}{\sum_{i=1}^{n} (y_i - \bar{y})^2}\]
- r2a:
R2 adjusted
\[\text{Adjusted } R^2 = 1 - \frac{(1 - R^2)(n - 1)}{n - k - 1}\]
- var:
Explained Variance.
\[VAR = 1 - \frac{Var(y - \hat{y})}{Var(y)}\]
- rmse:
Root-mean-squared error
\[RMSE = \sqrt{\frac{1}{n} \sum_{i=1}^{n} (y_i - \hat{y}_i)^2}\]
- cv: int, optional
Number of folds.
- average: str, optional
The method used to compute the final score for multiclass-classification.
- binary:
considers one of the classes as positive and use the binary confusion matrix to compute the score.
- micro:
positive and negative values globally.
- macro:
average of the score of each class.
- weighted:
weighted average of the score of each class.
- pos_label: PythonScalar, optional
The main class to be considered as positive (classification only).
- cutoff: float, optional
The model cutoff (classification only).
- training_score: bool, optional
If set to True, the training score is computed with the validation score.
- comb_limit: int, optional
Maximum number of features combinations used to train the model.
- skip_error: bool, optional
If set to True and an error occurs, the error is displayed but not raised.
- print_info: bool, optional
If set to True, prints the model information at each step.
- TableSample
result of the randomized features search.
Let us use a dataset which has a variety of predictors and one value of interest. The Titanic dataset is a good example.
import verticapy.datasets as vpd data = vpd.load_titanic()
123pclass123survivedAbcAbcsex123age123sibsp123parchAbcticket123fareAbccabinAbcembarkedAbcboat123bodyAbc1 1 0 male 71.0 0 0 PC 17609 49.5042 [null] C [null] 22 2 1 0 male 45.0 0 0 113784 35.5 T S [null] [null] 3 1 0 male [null] 0 0 113798 31.0 [null] S [null] [null] 4 1 0 male 17.0 0 0 113059 47.1 [null] S [null] [null] 5 1 0 male 27.0 1 0 13508 136.7792 C89 C [null] [null] 6 1 0 male 37.0 1 1 PC 17756 83.1583 E52 C [null] [null] 7 1 0 male 31.0 1 0 F.C. 12750 52.0 B71 S [null] [null] 8 1 0 male 50.0 1 0 PC 17761 106.425 C86 C [null] 62 9 1 0 female 36.0 0 0 PC 17531 31.6792 A29 C [null] [null] 10 1 0 male 37.0 1 0 113803 53.1 C123 S [null] [null] 11 1 0 male 24.0 0 0 PC 17593 79.2 B86 C [null] [null] 12 1 0 male 45.0 1 0 36973 83.475 C83 S [null] [null] 13 1 0 male 40.0 0 0 112059 0.0 B94 S [null] 110 14 1 0 male 42.0 0 0 113038 42.5 B11 S [null] [null] 15 1 0 male [null] 0 0 17463 51.8625 E46 S [null] [null] 16 1 0 male 42.0 1 0 113789 52.0 [null] S [null] 38 17 1 0 male [null] 0 0 PC 17600 30.6958 [null] C 14 [null] 18 1 0 male 29.0 0 0 113501 30.0 D6 S [null] 126 19 1 0 male 46.0 0 0 13050 75.2417 C6 C [null] 292 20 1 0 male 54.0 0 0 17463 51.8625 E46 S [null] 175 21 1 0 male 47.0 0 0 113796 42.4 [null] S [null] [null] 22 1 0 male 58.0 0 2 35273 113.275 D48 C [null] 122 23 1 0 male 45.5 0 0 113043 28.5 C124 S [null] 166 24 1 0 male 29.0 1 0 113776 66.6 C2 S [null] [null] 25 1 0 male 47.0 0 0 110465 52.0 C110 S [null] 207 26 1 0 male 38.0 0 0 19972 0.0 [null] S [null] [null] 27 1 0 male 22.0 0 0 PC 17760 135.6333 [null] C [null] 232 28 1 0 male 31.0 0 0 PC 17590 50.4958 A24 S [null] [null] 29 1 0 male 50.0 1 0 13507 55.9 E44 S [null] [null] 30 1 0 male 56.0 0 0 17764 30.6958 A7 C [null] [null] 31 1 0 male 57.0 1 0 PC 17569 146.5208 B78 C [null] [null] 32 1 0 female 63.0 1 0 PC 17483 221.7792 C55 C57 S [null] [null] 33 1 0 male 61.0 0 0 36963 32.3208 D50 S [null] 46 34 1 0 male 21.0 0 1 35281 77.2875 D26 S [null] 169 35 1 0 male 51.0 0 1 PC 17597 61.3792 [null] C [null] [null] 36 1 1 female 63.0 1 0 13502 77.9583 D7 S 10 [null] 37 1 1 female 32.0 0 0 11813 76.2917 D15 C 8 [null] 38 1 1 female 58.0 0 0 113783 26.55 C103 S 8 [null] 39 1 1 female 44.0 0 0 PC 17610 27.7208 B4 C 6 [null] 40 1 1 female 41.0 0 0 16966 134.5 E40 C 3 [null] 41 1 1 female 53.0 0 0 PC 17606 27.4458 [null] C 6 [null] 42 1 1 male 36.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 43 1 1 female 58.0 0 1 PC 17755 512.3292 B51 B53 B55 C 3 [null] 44 1 1 male 11.0 1 2 113760 120.0 B96 B98 S 4 [null] 45 1 1 female 76.0 1 0 19877 78.85 C46 S 6 [null] 46 1 1 female [null] 0 1 113505 55.0 E33 S 6 [null] 47 1 1 female 39.0 1 1 PC 17756 83.1583 E49 C 14 [null] 48 1 1 female 27.0 1 2 F.C. 12750 52.0 B71 S 3 [null] 49 1 1 female [null] 0 0 17421 110.8833 [null] C 4 [null] 50 1 1 female 35.0 0 0 113503 211.5 C130 C 4 [null] 51 1 1 female 22.0 0 1 112378 59.4 [null] C 7 [null] 52 1 1 female 25.0 1 0 11765 55.4417 E50 C 5 [null] 53 1 1 male 48.0 1 0 PC 17572 76.7292 D33 C 3 [null] 54 1 1 female 35.0 1 0 36973 83.475 C83 S D [null] 55 1 1 male 27.0 0 0 PC 17572 76.7292 D49 C 3 [null] 56 1 1 female 24.0 0 0 11767 83.1583 C54 C 7 [null] 57 1 1 female 52.0 1 1 12749 93.5 B69 S 3 [null] 58 1 1 female 44.0 0 1 111361 57.9792 B18 C 4 [null] 59 1 1 female 15.0 0 1 24160 211.3375 B5 S 2 [null] 60 1 1 male 30.0 1 0 13236 57.75 C78 C 11 [null] 61 1 1 female 31.0 1 0 35273 113.275 D36 C 6 [null] 62 1 1 female 39.0 0 0 PC 17758 108.9 C105 C 8 [null] 63 1 1 female 22.0 0 1 113509 61.9792 B36 C 5 [null] 64 1 1 male 52.0 0 0 113786 30.5 C104 S 6 [null] 65 1 1 female 43.0 0 1 24160 211.3375 B3 S 2 [null] 66 1 1 female 33.0 0 0 110152 86.5 B77 S 8 [null] 67 1 1 male 45.0 1 1 16966 134.5 E34 C 3 [null] 68 1 1 female 40.0 1 1 16966 134.5 E34 C 3 [null] 69 1 1 male 48.0 1 0 19996 52.0 C126 S 5 7 [null] 70 1 1 female [null] 0 0 PC 17585 79.2 [null] C D [null] 71 1 1 female 35.0 0 0 PC 17755 512.3292 [null] C 3 [null] 72 1 1 female 60.0 1 0 110813 75.25 D37 C 5 [null] 73 1 1 male 21.0 0 1 PC 17597 61.3792 [null] C A [null] 74 2 0 male 23.0 0 0 C.A. 31030 10.5 [null] S [null] [null] 75 2 0 male 28.0 0 0 244358 26.0 [null] S [null] [null] 76 2 0 male 60.0 1 1 29750 39.0 [null] S [null] [null] 77 2 0 female 44.0 1 0 244252 26.0 [null] S [null] [null] 78 2 0 male 29.0 1 0 2003 26.0 [null] S [null] [null] 79 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 80 2 0 male 18.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 81 2 0 male 54.0 0 0 28403 26.0 [null] S [null] [null] 82 2 0 male 18.0 0 0 236171 13.0 [null] S [null] [null] 83 2 0 male 36.0 0 0 229236 13.0 [null] S [null] 236 84 2 0 male 34.0 1 0 28664 21.0 [null] S [null] [null] 85 2 0 male 21.0 1 0 28133 11.5 [null] S [null] [null] 86 2 0 male 21.0 1 0 28134 11.5 [null] S [null] [null] 87 2 0 male 24.0 0 0 233866 13.0 [null] S [null] 155 88 2 0 male 34.0 0 0 12233 13.0 [null] S [null] [null] 89 2 0 male 30.0 0 0 250653 13.0 [null] S [null] 75 90 2 0 male 44.0 0 0 248746 13.0 [null] S [null] 35 91 2 0 male 49.0 1 2 220845 65.0 [null] S [null] [null] 92 2 0 male 21.0 2 0 S.O.C. 14879 73.5 [null] S [null] [null] 93 2 0 male 21.0 0 0 S.O.C. 14879 73.5 [null] S [null] [null] 94 2 0 female 60.0 1 0 24065 26.0 [null] S [null] [null] 95 2 0 male 24.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 96 2 0 male 22.0 2 0 C.A. 31029 31.5 [null] S [null] [null] 97 2 0 male 35.0 0 0 233734 12.35 [null] Q [null] [null] 98 2 0 male 31.0 0 0 C.A. 18723 10.5 [null] S [null] 165 99 2 0 male 36.0 0 0 SC/Paris 2163 12.875 D C [null] [null] 100 2 0 male [null] 0 0 SC/A.3 2861 15.5792 [null] C [null] [null] Rows: 1-100 | Columns: 14Note
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.
Next, we can initialize a Logistic Regression model:
from verticapy.machine_learning.vertica import LogisticRegression model = LogisticRegression()
Now we can conveniently use the
randomized_features_search_cvfunction to do either forward or backward randomized features search feature selection.from verticapy.machine_learning.model_selection import randomized_features_search_cv result = randomized_features_search_cv( model, input_relation = data, X = ["age", "fare", "parch", "pclass",], y = "survived", cv = 3, )
avg_score avg_train_score avg_time score_std score_train_std 1 0.2506001261650903 0.26292938633935964 2.214517513910929 0.0021474920385460504 0.0014016221399229076 2 0.25074602292517834 0.258169167403599 2.266688823699951 0.002046988889454484 0.000768794229565933 3 0.25399281520670264 0.25525992478844167 2.2767723401387534 0.00315083091224646 0.0011982141807638281 4 0.25557624306861665 0.25740215279109235 2.407245635986328 0.006920130599150315 0.0031673112602994785 5 0.25653137656482833 0.2544579321287073 2.2309447129567466 0.010381790871957902 0.004152680787457593 6 0.26065839032443366 0.25701399068860564 2.306551138559977 0.00840394245595519 0.004271851904456816 7 0.26068955188810367 0.260449149333355 2.108332395553589 0.005061111436909515 0.00253459894411372 8 0.2677091402658934 0.26917598855734864 2.213731129964193 0.0036364307659896237 0.0015706041108292563 9 0.269796020781651 0.2536089817699007 2.2880465984344482 0.0036296829082345544 0.0016344413855915673 10 0.27025884634895503 0.267955563932531 2.284230391184489 0.006248515359034414 0.002939782840266127 11 0.27064012464444365 0.27283658450289433 2.216388702392578 0.004816805834553563 0.0025788358238585207 12 0.27538037572278934 0.27069129274848 2.378204584121704 0.008289043847921916 0.004532597904482789 13 0.2821382243131407 0.28421288580200804 2.0592734813690186 0.003947380427782839 0.0019378427439336605 14 0.29081522875963367 0.2868269658248837 2.1436642011006675 0.007076125438859293 0.0036100428034907157 15 0.293041322959174 0.28966266215839864 2.1305018266042075 0.0068726562199244 0.002929513909999179 Rows: 1-15 | Columns: 6Note
The models are arranged in ascending order of
avg_score.