Budgeted model selection
A large or continuous hyperparameter space makes exhaustive enumeration impractical.
Scikit-Learn · Class
RandomizedSearchCV samples a fixed number of hyperparameter settings and evaluates them with cross-validation. It is useful when exhaustive grid search is too costly.
RandomizedSearchCV samples a fixed number of parameter configurations and evaluates them with cross-validation. It refits the estimator using the selected configuration when refit is enabled, after which the search object can provide estimator operations supported by that estimator.
class sklearn.model_selection.RandomizedSearchCV(estimator, param_distributions, *, n_iter=10, scoring=None, n_jobs=None, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', random_state=None, error_score=nan, return_train_score=False)| Name | Type | Description |
|---|---|---|
estimator | estimator object | Estimator implementing the scikit-learn estimator interface; a fresh instance is used for each sampled setting. |
param_distributions | dict | Dictionary mapping parameter names to lists or distributions from which settings are sampled. |
n_iter | int, default=10 | Number of parameter settings sampled. |
scoring | str, callable, list, tuple or dict, default=None | Metric strategy for evaluating cross-validated test performance. |
refit | bool, str, or callable, default=True | Whether and how to refit using the best result. |
cv | int, CV splitter, iterable, or None, default=None | Cross-validation strategy; None uses default five-fold cross-validation. |
n_jobs | int or None, default=None | Controls parallel job count. |
verbose | int, default=0 | Controls verbosity of the search. |
pre_dispatch | int or str, default='2*n_jobs' | Number of jobs dispatched during parallel execution. |
random_state | int, RandomState instance, or None, default=None | Controls reproducible sampling. |
error_score | number or 'raise', default=nan | Value assigned when a fitting error occurs. |
return_train_score | bool, default=False | Whether training scores are included in results. |
A fitted RandomizedSearchCV search object; after fitting, it can expose the best estimator and supported estimator methods such as predict, predict_proba, transform, or inverse_transform.
from scipy.stats import randint, uniform
from sklearn.datasets import make_regression
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import RandomizedSearchCV
X, y = make_regression(n_samples=120, n_features=5, random_state=0)
search = RandomizedSearchCV(
RandomForestRegressor(random_state=0),
{"n_estimators": randint(10, 50), "max_depth": randint(2, 10),
"max_features": uniform(0.5, 0.5)},
n_iter=5, cv=3, random_state=0, n_jobs=-1
)
search.fit(X, y)
print(search.best_params_)
print(search.best_score_)Samples five parameter settings, evaluates each with three-fold cross-validation, and exposes the best configuration and score.
A large or continuous hyperparameter space makes exhaustive enumeration impractical.
Several candidate metrics or a custom scorer must rank configurations.
The final model should be retrained using the selected settings.
RandomizedSearchCV evaluates a fixed number of sampled parameter settings rather than exhaustively trying every combination. The number sampled is controlled by n_iter, and each setting is assessed through cross-validation.
Follow-up: How does sampling change when distributions are used? If every parameter is represented by lists, sampling is without replacement; if any parameter is a distribution, it is with replacement.
param_distributions maps parameter names to candidate lists or probability distributions. n_iter controls how many settings are drawn, making the runtime budget explicit.
Follow-up: What trade-off does n_iter introduce? More iterations improve the chance of finding a strong configuration but increase runtime.
With refit=True, the selected estimator is refit on the complete dataset after the search. The search object then exposes methods such as predict or transform when the underlying estimator implements them.
Follow-up: What changes for multiple metrics? refit must be a scorer name or callable that determines which result is used for refitting.
GridSearchCVParameterSamplercross_validateBaseSearchCV