Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

RandomizedSearchCV

RandomizedSearchCV samples a fixed number of hyperparameter settings and evaluates them with cross-validation. It is useful when exhaustive grid search is too costly.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
estimatorestimator objectEstimator implementing the scikit-learn estimator interface; a fresh instance is used for each sampled setting.
param_distributionsdictDictionary mapping parameter names to lists or distributions from which settings are sampled.
n_iterint, default=10Number of parameter settings sampled.
scoringstr, callable, list, tuple or dict, default=NoneMetric strategy for evaluating cross-validated test performance.
refitbool, str, or callable, default=TrueWhether and how to refit using the best result.
cvint, CV splitter, iterable, or None, default=NoneCross-validation strategy; None uses default five-fold cross-validation.
n_jobsint or None, default=NoneControls parallel job count.
verboseint, default=0Controls verbosity of the search.
pre_dispatchint or str, default='2*n_jobs'Number of jobs dispatched during parallel execution.
random_stateint, RandomState instance, or None, default=NoneControls reproducible sampling.
error_scorenumber or 'raise', default=nanValue assigned when a fitting error occurs.
return_train_scorebool, default=FalseWhether training scores are included in results.

Returns

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.

Key points

  • The class provides fit and score operations, following the estimator-style interface.
  • Unlike exhaustive grid search, it explores a bounded sample of the supplied parameter space.
  • Set n_iter to choose the number of configurations tested.
  • List-only search spaces avoid drawing the same complete configuration twice.
  • cv accepts None for the default five-fold strategy, an integer, a splitter, or explicit train/test splits.
  • The wrapped estimator's parameters are selected according to cross-validated performance.

Common mistakes

  • Including a distribution changes sampling to occur with replacement, so repeated configurations can be possible.
  • Increasing n_iter generally spends more computation exploring configurations, while a small value may miss stronger settings.
  • When several metrics are used, refit must identify which scorer selects the estimator to refit.

Example: Randomized hyperparameter search

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.

When to use it

Budgeted model selection

A large or continuous hyperparameter space makes exhaustive enumeration impractical.

Custom performance evaluation

Several candidate metrics or a custom scorer must rank configurations.

Automatic refitting

The final model should be retrained using the selected settings.

Interview questions

QHow does RandomizedSearchCV differ from GridSearchCV?

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.

QHow do you define the search space and computational budget?

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.

QWhat does refit do after cross-validation?

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.

  • GridSearchCV
  • ParameterSampler
  • cross_validate
  • BaseSearchCV

Source: RandomizedSearchCV — scikit-learn 1.9.1 documentation. Checked 2026-10-15.