I have a few questions concerning Randomized grid search in a Random Forest Regression Model. My parameter grid looks like this:
JavaScript
x
7
1
random_grid = {'bootstrap': [True, False],
2
'max_depth': [10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, None],
3
'max_features': ['auto', 'sqrt'],
4
'min_samples_leaf': [1, 2, 4],
5
'min_samples_split': [2, 5, 10],
6
'n_estimators': [130, 180, 230]}
7
and my code for the RandomizedSearchCV like this:
JavaScript
1
10
10
1
# Use the random grid to search for best hyperparameters
2
# First create the base model to tune
3
from sklearn.ensemble import RandomForestRegressor
4
rf = RandomForestRegressor()
5
# Random search of parameters, using 3 fold cross validation,
6
# search across 100 different combinations, and use all available cores
7
rf_random = RandomizedSearchCV(estimator = rf, param_distributions = random_grid, n_iter = 100, cv = 3, verbose=2, random_state=42, n_jobs = -1)
8
# Fit the random search model
9
rf_random.fit(X_1, Y)
10
is there any way to calculate the Root mean square at each parameter set? This would be more interesting to me as the R^2 score? If I now want to get the best parameter set, as printed underneath i would also use the lowest RMSE score. Is there any way to do that?
JavaScript
1
4
1
rf_random.best_params_
2
rf_random.best_score_
3
rf_random.best_estimator_
4
thank you, R
Advertisement
Answer
Add the ‘scoring’-parameter to RandomizedSearchCV.
JavaScript
1
2
1
RandomizedSearchCV(scoring="neg_mean_squared_error",
2
Alternative options can be found in the docs
With this, you can print the RMSE for each parameter set, along with the parameter set:
JavaScript
1
4
1
cv_results = rf_random.cv_results_
2
for mean_score, params in zip(cv_results["mean_test_score"], cvres["params"]):
3
print(np.sqrt(-mean_score), params)
4