Skip to content

Instantly share code, notes, and snippets.

@deepak-karkala
Created December 16, 2020 13:31
Show Gist options
  • Select an option

  • Save deepak-karkala/237d37585d8fd0f5f552c2a88b73d27f to your computer and use it in GitHub Desktop.

Select an option

Save deepak-karkala/237d37585d8fd0f5f552c2a88b73d27f to your computer and use it in GitHub Desktop.
Tune hyperparameters using Grid search and Randomised search Cross Validation
# Tune hyperparameters using Grid search and Randomised search Cross Validation
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import RandomizedSearchCV
# Grid Search Cross Validation
# Specify discrete values for hyperparameters
param_grid = [
{'max_depth': [1, 20, 100], 'max_features': [1, 5, 15, 20],
'max_leaf_nodes': [5, 50, 100]},
]
search = GridSearchCV(estimator=model, param_grid=param_grid, cv=10,
scoring='neg_mean_squared_error', return_train_score=True)
# Randomised search Cross Validation
# Specify distribution for hyperparameters
param_distribs = {
'max_depth': randint(low=1, high=10),
'max_features': randint(low=1, high=20),
}
search = RandomizedSearchCV(estimator=model, param_distributions=param_distributions,
n_iter=5, cv=10, scoring='neg_mean_squared_error',
random_state=42, return_train_score=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment