Skip to content

Instantly share code, notes, and snippets.

@tiaplagata
Created November 16, 2020 02:33
Show Gist options
  • Save tiaplagata/c9e06a76add76a544c46e4ca43fe4071 to your computer and use it in GitHub Desktop.
Save tiaplagata/c9e06a76add76a544c46e4ca43fe4071 to your computer and use it in GitHub Desktop.
Using a Pipeline with GridSearch
from sklearn.model_selection import GridSearchCV
param_grid = {"estimator__n_estimators" : [100, 150, 200],
"estimator__criterion" : ["gini", "entropy"],
"estimator__max_depth" : [3, 4, 5]}
# You can change the scoring parameter here depending on which score you want to maximize
# You can also change the cv parameter to perform a cross validation with n folds for each model you fit
grid_rf = GridSearchCV(estimator= basic_pipe,
param_grid = param_grid,
scoring= "accuracy",
cv=5)
# Fit the gridsearch object to your training data
grid_rf.fit(X_train, y_train)
# See the mean cross-validated score of the best model
grid_rf.best_score_
# See the params of the best model
grid_rf.best_params_
# Store the best model in a variable to reference later
best_rf = grid_rf.best_estimator_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment