Created
November 17, 2019 21:46
-
-
Save marskar/6dff9335d0e41e4411f9560546aa8b3e to your computer and use it in GitHub Desktop.
Multi-hyperparameter, cross-validated grid search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.model_selection import KFold, GridSearchCV | |
from sklearn.ensemble import RandomForestRegressor | |
from sklearn.datasets import load_boston | |
boston = load_boston() | |
X = boston.data | |
y = boston.target | |
# Create kf instance | |
kf = KFold(n_splits=5, shuffle=True, random_state=42) | |
# Create dt instance | |
rf = RandomForestRegressor() | |
# Create grid search instance | |
gscv = GridSearchCV( | |
rf, | |
{"max_depth": range(1, 20), | |
"n_estimators": range(2, 20), | |
"min_samples_leaf": range(1, 6), | |
"min_samples_split": range(2, 10)}, | |
cv=kf, | |
n_jobs=-1 | |
) | |
gscv.fit(X, y) | |
# Get cross-validation data | |
cv_df = pd.DataFrame(gscv.cv_results_) | |
# Create a heatmap-style table | |
piv_df = cv_df.pivot_table(index=["param_max_depth", "param_min_samples_leaf"], | |
columns=["param_n_estimators", "param_min_samples_split"], | |
values="mean_test_score").round(3) | |
piv_df.style.background_gradient(cmap="nipy_spectral", axis=None) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment