python
30 lines · 1 tab
Dr. Elena Vasquez
Apr 2026
1 tab
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.ensemble import RandomForestClassifier
grid_search = GridSearchCV(
estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
param_grid={
'n_estimators': [200, 400],
'max_depth': [None, 10, 20],
'min_samples_leaf': [1, 3, 5],
},
scoring='roc_auc',
cv=5,
n_jobs=-1,
verbose=1,
)
randomized_search = RandomizedSearchCV(
estimator=RandomForestClassifier(random_state=42, n_jobs=-1),
param_distributions={
'n_estimators': [200, 300, 400, 500],
'max_depth': [None, 8, 12, 16, 24],
'min_samples_split': [2, 5, 10],
'min_samples_leaf': [1, 2, 4],
},
n_iter=15,
scoring='roc_auc',
cv=5,
n_jobs=-1,
random_state=42,
)
1 file · python
Explain with highlit
Hyperparameter search should be targeted, not theatrical. I usually combine a strong baseline, a compact search space, and a metric aligned with business cost. GridSearchCV is good for interpretable sweeps; randomized search is better when the space gets large and the budget is fixed.
Share this code
Here's the card — post it anywhere.