Skip to content

Instantly share code, notes, and snippets.

@kperry2215
Created July 30, 2019 22:31
Show Gist options
  • Save kperry2215/16acf8da344e67a12f6337d76fd35dba to your computer and use it in GitHub Desktop.
Save kperry2215/16acf8da344e67a12f6337d76fd35dba to your computer and use it in GitHub Desktop.
def grid_search_rf(parameter_grid, train_features, train_labels):
"""
Perform Grid Search on the random forest classifier model, in order to optimize model
parameters
parameter_grid: grid parameters to test against to determine optimal parameters
train_features: Numpy array, containing training set features
train_labels: Numpy array, containing training set labels
"""
# Create a random forest classifier model
rf = RandomForestClassifier()
# Instantiate the grid search model
grid_search = GridSearchCV(estimator = rf,
param_grid = parameter_grid,
cv = 3,
n_jobs = -1,
verbose = 2)
grid_search.fit(train_features, train_labels)
print(grid_search.best_params_)
##Execute in main script
#Create the parameter grid, which is plugged into GridSearchCV, where all hyperparameter combos
#are tested to find the optimal parameters combination
parameter_grid={'max_depth': [80, 90, 100, 110],
'n_estimators': [700, 800, 900, 1000, 1100, 1200]}
grid_search_rf(parameter_grid, train_features, train_labels)
"""
Grid Search Outputs:
Fitting 3 folds for each of 20 candidates, totalling 60 fits
[Parallel(n_jobs=-1)]: Using backend LokyBackend with 4 concurrent workers.
[Parallel(n_jobs=-1)]: Done 33 tasks | elapsed: 2.1min
[Parallel(n_jobs=-1)]: Done 60 out of 60 | elapsed: 3.7min finished
{'max_depth': 80, 'n_estimators': 1000}
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment