Skip to content

Instantly share code, notes, and snippets.

@supachaic
Last active September 9, 2017 05:30
Show Gist options
  • Save supachaic/c8bf1b4f99ec089aebde194dc92f02e8 to your computer and use it in GitHub Desktop.
Save supachaic/c8bf1b4f99ec089aebde194dc92f02e8 to your computer and use it in GitHub Desktop.
def vote(estimators, threshold, X_train, y_train, X_val, y_val, X_test, score_name):
predict_list = []
estimator_list = []
for estimator_name in estimators:
try:
params, val_score, timestamp = get_params(estimator_name)
if val_score > threshold:
print('Ensemble add %s' % estimator_name)
estimator_list.append(estimator_name)
if estimator_name == 'keras':
clf = keras_model(**params)
model_path = os.path.join('saves/', timestamp + '.json')
weights_path = os.path.join('saves/', timestamp + '.h5')
clf_pred = clf.predict_proba(X_val, model_path, weights_path)
predict_list.append(clf_pred)
else:
if estimator_name == 'xgb':
clf = XGBClassifier(nthreads=-1, **params)
elif estimator_name == 'log':
clf = LogisticRegression(**params)
elif estimator_name == 'knn':
clf = KNeighborsClassifier(**params)
elif estimator_name == 'rfo':
clf = RandomForestClassifier(**params)
elif estimator_name == 'ada':
clf = AdaBoostClassifier(**params)
elif estimator_name == 'ext':
clf = ExtraTreesClassifier(**params)
elif estimator_name == 'svc':
clf = SVC(**params)
clf.fit(X_train, y_train)
clf_pred = clf.predict_proba(X_val)
predict_list.append(clf_pred)
except: FileNotFoundError
y_pred = np.mean(predict_list, axis=0)
y_pred = np.argmax(y_pred, axis=1)
return y_pred
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment