Loading...

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()
123
pclass
Integer
123
survived
Integer
Abc
Varchar(164)
Abc
sex
Varchar(20)
123
age
Numeric(8)
123
sibsp
Integer
123
parch
Integer
Abc
ticket
Varchar(36)
123
fare
Numeric(12)
Abc
cabin
Varchar(30)
Abc
embarked
Varchar(20)
Abc
boat
Varchar(100)
123
body
Integer
Abc
Varchar(100)
110male71.000PC 1760949.5042[null]C[null]22
210male45.00011378435.5TS[null][null]
310male[null]0011379831.0[null]S[null][null]
410male17.00011305947.1[null]S[null][null]
510male27.01013508136.7792C89C[null][null]
610male37.011PC 1775683.1583E52C[null][null]
710male31.010F.C. 1275052.0B71S[null][null]
810male50.010PC 17761106.425C86C[null]62
910female36.000PC 1753131.6792A29C[null][null]
1010male37.01011380353.1C123S[null][null]
1110male24.000PC 1759379.2B86C[null][null]
1210male45.0103697383.475C83S[null][null]
1310male40.0001120590.0B94S[null]110
1410male42.00011303842.5B11S[null][null]
1510male[null]001746351.8625E46S[null][null]
1610male42.01011378952.0[null]S[null]38
1710male[null]00PC 1760030.6958[null]C14[null]
1810male29.00011350130.0D6S[null]126
1910male46.0001305075.2417C6C[null]292
2010male54.0001746351.8625E46S[null]175
2110male47.00011379642.4[null]S[null][null]
2210male58.00235273113.275D48C[null]122
2310male45.50011304328.5C124S[null]166
2410male29.01011377666.6C2S[null][null]
2510male47.00011046552.0C110S[null]207
2610male38.000199720.0[null]S[null][null]
2710male22.000PC 17760135.6333[null]C[null]232
2810male31.000PC 1759050.4958A24S[null][null]
2910male50.0101350755.9E44S[null][null]
3010male56.0001776430.6958A7C[null][null]
3110male57.010PC 17569146.5208B78C[null][null]
3210female63.010PC 17483221.7792C55 C57S[null][null]
3310male61.0003696332.3208D50S[null]46
3410male21.0013528177.2875D26S[null]169
3510male51.001PC 1759761.3792[null]C[null][null]
3611female63.0101350277.9583D7S10[null]
3711female32.0001181376.2917D15C8[null]
3811female58.00011378326.55C103S8[null]
3911female44.000PC 1761027.7208B4C6[null]
4011female41.00016966134.5E40C3[null]
4111female53.000PC 1760627.4458[null]C6[null]
4211male36.001PC 17755512.3292B51 B53 B55C3[null]
4311female58.001PC 17755512.3292B51 B53 B55C3[null]
4411male11.012113760120.0B96 B98S4[null]
4511female76.0101987778.85C46S6[null]
4611female[null]0111350555.0E33S6[null]
4711female39.011PC 1775683.1583E49C14[null]
4811female27.012F.C. 1275052.0B71S3[null]
4911female[null]0017421110.8833[null]C4[null]
5011female35.000113503211.5C130C4[null]
5111female22.00111237859.4[null]C7[null]
5211female25.0101176555.4417E50C5[null]
5311male48.010PC 1757276.7292D33C3[null]
5411female35.0103697383.475C83SD[null]
5511male27.000PC 1757276.7292D49C3[null]
5611female24.0001176783.1583C54C7[null]
5711female52.0111274993.5B69S3[null]
5811female44.00111136157.9792B18C4[null]
5911female15.00124160211.3375B5S2[null]
6011male30.0101323657.75C78C11[null]
6111female31.01035273113.275D36C6[null]
6211female39.000PC 17758108.9C105C8[null]
6311female22.00111350961.9792B36C5[null]
6411male52.00011378630.5C104S6[null]
6511female43.00124160211.3375B3S2[null]
6611female33.00011015286.5B77S8[null]
6711male45.01116966134.5E34C3[null]
6811female40.01116966134.5E34C3[null]
6911male48.0101999652.0C126S5 7[null]
7011female[null]00PC 1758579.2[null]CD[null]
7111female35.000PC 17755512.3292[null]C3[null]
7211female60.01011081375.25D37C5[null]
7311male21.001PC 1759761.3792[null]CA[null]
7420male23.000C.A. 3103010.5[null]S[null][null]
7520male28.00024435826.0[null]S[null][null]
7620male60.0112975039.0[null]S[null][null]
7720female44.01024425226.0[null]S[null][null]
7820male29.010200326.0[null]S[null][null]
7920male18.000S.O.C. 1487973.5[null]S[null][null]
8020male18.000S.O.C. 1487973.5[null]S[null][null]
8120male54.0002840326.0[null]S[null][null]
8220male18.00023617113.0[null]S[null][null]
8320male36.00022923613.0[null]S[null]236
8420male34.0102866421.0[null]S[null][null]
8520male21.0102813311.5[null]S[null][null]
8620male21.0102813411.5[null]S[null][null]
8720male24.00023386613.0[null]S[null]155
8820male34.0001223313.0[null]S[null][null]
8920male30.00025065313.0[null]S[null]75
9020male44.00024874613.0[null]S[null]35
9120male49.01222084565.0[null]S[null][null]
9220male21.020S.O.C. 1487973.5[null]S[null][null]
9320male21.000S.O.C. 1487973.5[null]S[null][null]
9420female60.0102406526.0[null]S[null][null]
9520male24.020C.A. 3102931.5[null]S[null][null]
9620male22.020C.A. 3102931.5[null]S[null][null]
9720male35.00023373412.35[null]Q[null][null]
9820male31.000C.A. 1872310.5[null]S[null]165
9920male36.000SC/Paris 216312.875DC[null][null]
10020male[null]00SC/A.3 286115.5792[null]C[null][null]
Rows: 1-100 | Columns: 14

Note

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_cv function 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
10.25060012616509030.262929386339359642.2145175139109290.00214749203854605040.0014016221399229076
20.250746022925178340.2581691674035992.2666888236999510.0020469888894544840.000768794229565933
30.253992815206702640.255259924788441672.27677234013875340.003150830912246460.0011982141807638281
40.255576243068616650.257402152791092352.4072456359863280.0069201305991503150.0031673112602994785
50.256531376564828330.25445793212870732.23094471295674660.0103817908719579020.004152680787457593
60.260658390324433660.257013990688605642.3065511385599770.008403942455955190.004271851904456816
70.260689551888103670.2604491493333552.1083323955535890.0050611114369095150.00253459894411372
80.26770914026589340.269175988557348642.2137311299641930.00363643076598962370.0015706041108292563
90.2697960207816510.25360898176990072.28804659843444820.00362968290823455440.0016344413855915673
100.270258846348955030.2679555639325312.2842303911844890.0062485153590344140.002939782840266127
110.270640124644443650.272836584502894332.2163887023925780.0048168058345535630.0025788358238585207
120.275380375722789340.270691292748482.3782045841217040.0082890438479219160.004532597904482789
130.28213822431314070.284212885802008042.05927348136901860.0039473804277828390.0019378427439336605
140.290815228759633670.28682696582488372.14366420110066750.0070761254388592930.0036100428034907157
150.2930413229591740.289662662158398642.13050182660420750.00687265621992440.002929513909999179
Rows: 1-15 | Columns: 6

Note

The models are arranged in ascending order of avg_score.