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

