Skip to content

Instantly share code, notes, and snippets.

@anirudhshenoy
Created November 19, 2019 08:32
Show Gist options
  • Select an option

  • Save anirudhshenoy/78b04291dd5234352c9773d61edda450 to your computer and use it in GitHub Desktop.

Select an option

Save anirudhshenoy/78b04291dd5234352c9773d61edda450 to your computer and use it in GitHub Desktop.
Running hyperopt for a stacking classifier
from hyperopt import fmin, tpe, hp, STATUS_OK, Trials
def run_voting_clf(model_weights):
y_pred_prob = 0
for model_name, model in model_dict.items():
y_pred_prob += (model.predict_proba(test_features)[:,1] * model_weights[model_name])
y_pred_prob += (simple_nn.predict(test_features.todense()).ravel() * model_weights['simple_nn'])
y_pred_prob /= sum(model_weights.values())
f1 = print_model_metrics(y_test, y_pred_prob, return_metrics = True, verbose = 0)[0]
return {'loss' : -f1, 'status' : STATUS_OK} #return negative of F1 since hyperopt is running fmin
trials = Trials()
model_weights = fmin(run_voting_clf,
space= {
'LR' : hp.uniform('LR', 0, 1),
'SVM' : hp.uniform('SVM', 0, 1),
'NB' : hp.uniform('NB', 0, 1),
'KNN' : hp.uniform('KNN', 0, 1),
'RF' : hp.uniform('RF', 0, 1),
'XGB' : hp.uniform('XGB', 0, 1),
'simple_nn' : hp.uniform('simple_nn', 0, 1),
},
algo=tpe.suggest,
max_evals=500,
trials = trials)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment