Rakl
Rakl

Reputation: 1

How to Get the Best Parameters of Cross_val_score?

How can I get the best parameters? wrapped = KerasClassifier(build_fn=createmodel_batch, epochs=100, batch_size=5, verbose=0) folds = StratifiedKFold(n_splits=3, shuffle=True, random_state=15) results = cross_val_score(wrapped, X, Y, cv=folds)

Upvotes: 0

Views: 491

Answers (1)

K0mp0t
K0mp0t

Reputation: 99

Looks like you're using sklearn's Keras wrapper. So, you should use GridSearchCV instead of cross_val_score. cross_val_score is used for evaluating estimator with choosen parameters. GridSearchCV accepts parameters grid and performs grid search on it (crossvalidates every combination of parameters), heres the exaple:

params = {'clf__kernel': ['linear', 'poly', 'rbf', 'sigmoid'],
          'clf__class_weight': ['balanced', None], 
          'clf__probability': [True, False], 
          'clf__random_state': [42], 
          'scaler__with_mean': [True, False]} # define params grid

svc_gs = GridSearchCV(Pipeline([('scaler', StandardScaler()), ('clf', SVC())]), params, verbose=1, n_jobs=-1, scoring='roc_auc') # initialize GridSearchCV obj
svc_gs.fit(train_data.drop(columns=['label']), train_data.label) # perform grid search
print(svc_gs.best_score_) # get best score on choosen params grid
print(svc_gs.best_params_) # get best combination of params from choosen params grid

Upvotes: 1

Related Questions