grid_search_cv

In [ ]:
grid_search_cv(estimator,
               param_grid: dict,
               input_relation: (str, vDataFrame),
               X: list,
               y: str,
               metric: str = "auto",
               cv: int = 3,
               pos_label: (int, float, str) = None,
               cutoff: float = -1,
               training_score: bool = True,
               skip_error: bool = False,
               print_info: bool = False,)

Computes the k-fold grid search of an estimator.

Parameters

Name Type Optional Description
estimator
object
Vertica estimator having a fit method.
param_grid
dict
Dictionary of the parameters to test.
input_relation
str / vDataFrame
Input Relation.
X
list
List of the predictor columns.
y
str
Response Column.
metric
str / list
Metric used to do the model evaluation.
  • auto : logloss for classification & rmse for regression.

For Classification:
  • accuracy : Accuracy
  • auc : Area Under the Curve (ROC)
  • bm : Informedness = tpr + tnr - 1
  • csi : Critical Success Index = tp / (tp + fn + fp)
  • f1 : F1 Score
  • logloss : Log Loss
  • mcc : Matthews Correlation Coefficient
  • mk : Markedness = ppv + npv - 1
  • npv : Negative Predictive Value = tn / (tn + fn)
  • prc_auc : Area Under the Curve (PRC)
  • precision : Precision = tp / (tp + fp)
  • recall : Recall = tp / (tp + fn)
  • specificity : Specificity = tn / (tn + fp)

For Regression:
  • max : Max Error
  • mae : Mean Absolute Error
  • median : Median Absolute Error
  • mse : Mean Squared Error
  • msle : Mean Squared Log Error
  • r2 : R-squared coefficient
  • r2a : R2 adjusted
  • rmse : Root Mean Squared Error
  • var : Explained Variance
cv
int
Number of folds.
pos_label
int / float / str
The main class to be considered as positive (classification only).
cutoff
float
The model cutoff (classification only).
training_score
bool
If set to True, the training score will be computed with the validation score.
skip_error
bool
If set to True and an error occurs, it will be displayed and not raised.
print_info
bool
If set to True, prints the model information at each step.

Returns

tablesample : An object containing the result. For more information, see utilities.tablesample.

Example

In [1]:
from verticapy.learn.linear_model import LogisticRegression
model = LogisticRegression(name = "public.LR_titanic",
                           tol = 1e-4,
                           max_iter = 100, 
                           solver = 'Newton')

from verticapy.learn.model_selection import grid_search_cv
grid_search_cv(model,
               {"tol": [1e-2, 1e-4, 1e-6],
                "max_iter": [3, 10, 100],
                "solver": ["Newton", "BFGS"]},
               input_relation = "public.titanic_clean", 
               X = ["age", "fare", "parch", "sex", "boat"], 
               y = "survived", 
               cv = 3)
Out[1]:
avg_score
avg_train_score
avg_time
score_std
score_train_std
10.03387297749497780.0428199540595936640.80940183003743490.005653262735727610.0026011100736774693
20.036383782954275730.0371167103009588661.18103806177775070.00923602914099960.004284315151296585
30.036639009999603460.036679225801423631.1800677776336670.0070315059441868050.00335596962457923
40.0380358968282950360.036840321075125578.3055546283721920.0067166656054996280.0032610693751197366
50.03937519249311450.0365147227654671.12044159571329760.016365414098181880.006214431324554634
60.040939884268196270.0387812697852372640.76725363731384280.0034160651614408950.0019369056238435882
70.04305795355200460.033748667157151938.0170440673828120.0048222562224399080.002044611191173046
80.043714818173990130.0333950737388598661.5803559621175130.009768709836038490.004421770934265993
90.0446955207271778340.0367756048898866960.8357127507527670.0057761402889103810.003256567627819591
100.044762927471439570.03304702231938941.03895489374796560.0100576651535800240.004098239604327156
110.0511217877295125360.0318767692776135361.54917065302530930.0124406340945983140.0023881436104915476
120.176616639416757150.194572294304941332.18532172838846830.099011331782144140.04113450964911643
130.250534649122290340.185910908574265322.4007446765899660.0175298370636913680.029291844865067175
140.281980580973386350.293425527141670360.76991931597391760.02618246070431020.01430022477430164
150.2842855350902210.289289038093368640.87790735562642420.0184410391256228520.012384382206053984
160.2849038166942820.2861381105086540.96340767542521160.0153944638665622720.015584296153305211
170.73922972875618230.70685372202715571.84598962465922050.0338251110063449160.12850141468168103
180.7784756614629340.71974959199651730.99563336372375490.121131964730902620.05978336554898876
Rows: 1-18 | Columns: 6
In [2]:
# Adding the train result

grid_search_cv(model, 
               {"tol": [1e-2, 1e-4, 1e-6],
                "max_iter": [3, 10, 100],
                "solver": ["Newton", "BFGS"]},
               input_relation = "public.titanic_clean", 
               X = ["age", "fare", "parch", "sex", "boat"], 
               y = "survived", 
               cv = 3,
               metric = "auc",
               training_score = True)
Out[2]:
avg_score
avg_train_score
avg_time
score_std
score_train_std
10.99570479167647540.9897650112850698.3429102102915440.00200865053975612550.0015396929438997338
20.99559582594478180.9898673335972780.92112565040588380.002270823840816010.0007777915676018286
30.99313143205270860.99147534510108980.68052999178568520.00145364576542490850.0010435965770400793
40.99281373621953920.9914256754040016.9906109968821210.00107216647996846740.0008190828012935012
50.99197953986115930.99176411948600171.37452689806620290.0053385718550186730.0026096842645156894
60.99080196452006940.99251006393129381.31074253718058280.00207623805265884930.00038865894434134764
70.98996097511921940.9930604876530581.19244670867919920.0032952385774433290.001779423267114273
80.98993694773400810.99314099354082610.98197301228841150.002895355232234080.0016788104414725243
90.98972287329994660.99272052981864181.58397555351257320.00256192609034071140.0010945148894646786
100.98889400089990410.99327174761470861.08346231778462720.0036116778160883910.0012912109439143444
110.98845837995430140.99365499790199440.70541103680928550.0021951854923398490.0005424783641344345
120.97919507212918490.97977377893733182.4529517491658530.0046432772753954550.005688250823444079
130.94193007653442580.94750565915750562.24483839670817040.0250589489128472950.029946625243526214
140.76108595785195520.76629010154423321.01829457283020020.14124870749522680.14779754216228183
150.75227997855364990.77320614589544181.1457517147064210.166400515657848830.14445890706301523
160.65924797500602580.63903840332819470.85908659299214680.032064238303592140.0548401353926451
170.65374110969595760.6781528661034980.72179969151814780.0136734790538657040.03781222947910539
180.64692674627028760.62854094648048690.80403868357340490.0081790889266447570.026820845014610482
Rows: 1-18 | Columns: 6
In [3]:
# Printing the results

grid_search_cv(model,
               {"tol": [1e-2, 1e-4, 1e-6],
                "max_iter": [3, 10, 100],
                "solver": ["Newton", "BFGS"]},
               input_relation = "public.titanic_clean",
               X = ["age", "fare", "parch", "sex", "boat"],
               y = "survived",
               cv = 3,
               print_info = True)
Model: LogisticRegression; Parameters: {'tol': 0.01, 'max_iter': 3, 'solver': 'Newton'}; Test_score: 0.0416958046346368; Train_score: 0.038424415884418965; Time: 0.5732765197753906;
Model: LogisticRegression; Parameters: {'tol': 0.01, 'max_iter': 3, 'solver': 'BFGS'}; Test_score: 0.29398654547193737; Train_score: 0.2988679312647527; Time: 0.5154391924540201;
Model: LogisticRegression; Parameters: {'tol': 0.01, 'max_iter': 10, 'solver': 'Newton'}; Test_score: 0.052628245146305465; Train_score: 0.0301507442632964; Time: 0.832193930943807;
Model: LogisticRegression; Parameters: {'tol': 0.01, 'max_iter': 10, 'solver': 'BFGS'}; Test_score: 0.29820891278644834; Train_score: 0.301466737798373; Time: 0.4071214993794759;
Model: LogisticRegression; Parameters: {'tol': 0.01, 'max_iter': 100, 'solver': 'Newton'}; Test_score: 0.047525308310065496; Train_score: 0.03363348375788787; Time: 0.90839155515035;
Model: LogisticRegression; Parameters: {'tol': 0.01, 'max_iter': 100, 'solver': 'BFGS'}; Test_score: 0.28703377137989067; Train_score: 0.286418012223701; Time: 0.6809303760528564;
Model: LogisticRegression; Parameters: {'tol': 0.0001, 'max_iter': 3, 'solver': 'Newton'}; Test_score: 0.03951581142934674; Train_score: 0.0396534039992437; Time: 0.6992436250050863;
Model: LogisticRegression; Parameters: {'tol': 0.0001, 'max_iter': 3, 'solver': 'BFGS'}; Test_score: 0.7642205218056733; Train_score: 0.6709010814005953; Time: 0.8295307159423828;
Model: LogisticRegression; Parameters: {'tol': 0.0001, 'max_iter': 10, 'solver': 'Newton'}; Test_score: 0.0683423306806069; Train_score: 0.0264460511577688; Time: 1.2579163710276287;
Model: LogisticRegression; Parameters: {'tol': 0.0001, 'max_iter': 10, 'solver': 'BFGS'}; Test_score: 0.22774776768970934; Train_score: 0.197865157052058; Time: 2.21067746480306;
Model: LogisticRegression; Parameters: {'tol': 0.0001, 'max_iter': 100, 'solver': 'Newton'}; Test_score: 0.03324017062712357; Train_score: 0.038765923578225; Time: 1.2479968865712483;
Model: LogisticRegression; Parameters: {'tol': 0.0001, 'max_iter': 100, 'solver': 'BFGS'}; Test_score: 0.036668136667377164; Train_score: 0.036630915731166905; Time: 6.029535373051961;
Model: LogisticRegression; Parameters: {'tol': 1e-06, 'max_iter': 3, 'solver': 'Newton'}; Test_score: 0.043027230095132804; Train_score: 0.03786501867226883; Time: 0.6162236531575521;
Model: LogisticRegression; Parameters: {'tol': 1e-06, 'max_iter': 3, 'solver': 'BFGS'}; Test_score: 0.6341258178264964; Train_score: 0.576540349383636; Time: 0.9407570362091064;
Model: LogisticRegression; Parameters: {'tol': 1e-06, 'max_iter': 10, 'solver': 'Newton'}; Test_score: 0.0307525613810747; Train_score: 0.039218161825095264; Time: 1.073122262954712;
Model: LogisticRegression; Parameters: {'tol': 1e-06, 'max_iter': 10, 'solver': 'BFGS'}; Test_score: 0.19364803033332262; Train_score: 0.235090021592102; Time: 1.710917075475057;
Model: LogisticRegression; Parameters: {'tol': 1e-06, 'max_iter': 100, 'solver': 'Newton'}; Test_score: 0.039198022867401934; Train_score: 0.035456319389767364; Time: 1.282687822977702;
Model: LogisticRegression; Parameters: {'tol': 1e-06, 'max_iter': 100, 'solver': 'BFGS'}; Test_score: 0.0462506713535187; Train_score: 0.0327620077852528; Time: 12.775736331939697;

Model: LogisticRegression; Best_Parameters: {'solver': 'Newton', 'penalty': 'none', 'max_iter': 10, 'tol': 1e-06}; Best_Test_score: 0.0307525613810747; Train_score: 0.039218161825095264; Time: 1.073122262954712;


Out[3]:
avg_score
avg_train_score
avg_time
score_std
score_train_std
10.03075256138107470.0392181618250952641.0731222629547120.0071006626728998380.0035947942452672996
20.033240170627123570.0387659235782251.24799688657124830.0091331798071925050.004709129784307957
30.0366681366673771640.0366309157311669056.0295353730519610.00407688366476447950.0014954728251516528
40.0391980228674019340.0354563193897673641.2826878229777020.0112783178679668610.005932776297560999
50.039515811429346740.03965340399924370.69924362500508630.0057760797651727150.003074111974969664
60.04169580463463680.0384244158844189650.57327651977539060.0064094624342748960.0037795543778024313
70.0430272300951328040.037865018672268830.61622365315755210.0030506477579968530.0014852789809571398
80.04625067135351870.032762007785252812.7757363319396970.0122837306210809050.0057697535215526065
90.0475253083100654960.033633483757887870.908391555150350.0272001562226746460.009522105722295802
100.0526282451463054650.03015074426329640.8321939309438070.009971095800230.0028717054009224905
110.06834233068060690.02644605115776881.25791637102762870.02628436145654340.008707878886529262
120.193648030333322620.2350900215921021.7109170754750570.087822259316194510.021348701782719817
130.227747767689709340.1978651570520582.210677464803060.0282968713550963320.040256901002149334
140.287033771379890670.2864180122237010.68093037605285640.0058386391277273580.020291135406136366
150.293986545471937370.29886793126475270.51543919245402010.020143523706808190.0046708678871667944
160.298208912786448340.3014667377983730.40712149937947590.0077661575239158220.0022740280025160234
170.63412581782649640.5765403493836360.94075703620910640.333434796585750370.26146235649895955
180.76422052180567330.67090108140059530.82953071594238280.424117326662211860.36763046489498064
Rows: 1-18 | Columns: 6