liveBook · Manning

RandomizedSearchCV

As explained in Software Engineering for Data Scientists

Randomized search is a hyperparameter-optimization technique that evaluates randomly selected combinations from a search space.

RandomizedSearchCV tunes model hyperparameters by randomly selecting combinations from a supplied search space, then training and evaluating those combinations. It is especially useful when a large dataset or search space makes exhaustive grid search slow. section 5.2.1

Configure the search with:

  • A model, such as GradientBoostingClassifier, RandomForestClassifier, or RandomForestRegressor. section 4.2.2 section 5.2.1
  • A parameter dictionary containing candidate values such as tree depth, leaf size, split size, feature count, estimators, and learning rate. section 4.2.2
  • n_jobs for parallel execution, scoring for model evaluation, n_iter for the number of combinations, and random_state for repeatable selection. section 4.2.2 section 5.2.1
  • verbose when progress updates are needed during a long-running search. section 5.2.1

Tune a churn model

Define candidate random-forest values, create RandomizedSearchCV with scoring="roc_auc", n_jobs=4, n_iter=10, and random_state=0, then call fit(X_features, y). Inspect the selected model through best_estimator_, and use the trained wrapper to produce train and test predictions.

Listing 5.8 Use the verbose argument to monitor the progress of the hyperparameter tuning process.
from sklearn.model_selection import RandomizedSearchCV #1
from sklearn.ensemble import GradientBoostingClassifier #1
from ch4.dataset_class_final import DataSet #1

customer_obj = DataSet( #2
    feature_list = ["total_day_minutes", #2
    "total_day_calls", #2
    "number_customer_service_calls"], #2
    file_name = "data/customer_churn_data.csv", #2
    label_col = "churn", #2
    pos_category = "yes" #2
    ) #2

parameters = {"max_depth":range(2, 8), #3
    "min_samples_leaf": range(5, 55, 5), #3
    "min_samples_split": range(10, 110, 5), #3
    "max_features": [2, 3], #3
    "n_estimators": [100, 150, 200, #3
        250, 300, 350, 400]} #3

clf = RandomizedSearchCV( #4
    GradientBoostingClassifier(), #4
    parameters, #4
    n_jobs=4, #4
    scoring = "roc_auc", #4
    n_iter = 200, #4
    random_state = 0, #4
    verbose = 1) #4

clf.fit(train_features, #5
    train_labels) #5

Inspect a complete GradientBoostingClassifier search configuration, including a parameter space, parallel jobs, ROC-AUC scoring, iteration count, and progress verbosity. section 5.2.1

How Software Engineering for Data Scientists covers it

How deep it goes
Explains configuration, fitting, inspection, prediction, and wrapper generalization, but does not detail the search algorithm internally.
Examples
Uses customer-churn classification, random-forest and gradient-boosting searches, optimized estimators, and train/test predictions.
What it assumes
Assumes familiarity with Python models, training data, parameter dictionaries, and evaluation metrics while demonstrating wrapper construction from those pieces.
What you can do after
Apply it via scikit-learn to search model parameters, inspect the best estimator, and generate predictions.
How it connects
Connects hyperparameter tuning with random forest, gradient boosting, grid search, ROC-AUC, parallel execution, and progress monitoring.

Also explained in

Compare how 2 books explain RandomizedSearchCV →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Software Engineering for Data Scientists. The section links point to the parts of the book it draws on.