Skip to content

Instantly share code, notes, and snippets.

@jinhangjiang
Last active November 15, 2021 00:29
Show Gist options
  • Save jinhangjiang/1343a415bcf705257a5055e328413c84 to your computer and use it in GitHub Desktop.
Save jinhangjiang/1343a415bcf705257a5055e328413c84 to your computer and use it in GitHub Desktop.
votingclassifier2
#set parameters
params = {'voting':['hard', 'soft'],
'weights':[(1,1,1,1,1), (2,1,1,1,1),
(1,2,1,1,1), (1,1,2,1,1),
(1,1,1,2,1), (1,1,1,1,2),
(1,1,1,2,2), (2,1,1,1,2)]}
#fit gridsearch & print best params
grid = GridSearchCV(vc, params)
grid.fit(X, y)
print('\n')
print(f'The best params is : {grid.best_params_}')
#print the final cv score
tuned_vc = VotingClassifier([('dt', DecisionTree),
('KNN', KNN),
('MLPC', MLPC),
('rf', RandomForest),
('xgb', XGB)],
**grid.best_params_, n_jobs = -1)
tuned_cvm = cross_val_score(tuned_vc, X, y)
tuned_score = tuned_cvm.mean()
tuned_std = tuned_cvm.std()
print('\n')
print(f'The average tuned cross-validation score is {round(tuned_score, 4)} (+- {round(tuned_std, 4)})')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment