Which statement best describes how RandomizedSearchCV selects hyperparameter combinations during tuning?
Think about how RandomizedSearchCV differs from GridSearchCV.
RandomizedSearchCV randomly picks a fixed number of hyperparameter combinations from the given distributions, unlike GridSearchCV which tries all combinations exhaustively.
What will be the output of the following code snippet?
from sklearn.datasets import load_iris from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import RandomizedSearchCV import numpy as np iris = load_iris() X, y = iris.data, iris.target param_dist = {'n_estimators': [10, 50], 'max_depth': [2, 4]} clf = RandomForestClassifier(random_state=42) search = RandomizedSearchCV(clf, param_distributions=param_dist, n_iter=2, random_state=42) search.fit(X, y) print(search.best_params_)
RandomizedSearchCV with random_state=42 and n_iter=2 samples two parameter sets randomly.
With the given random seed, the two sampled parameter sets are {'n_estimators': 50, 'max_depth': 2} and {'n_estimators': 10, 'max_depth': 4}. The best performing is {'n_estimators': 50, 'max_depth': 2}.
What is the effect of increasing the n_iter parameter in RandomizedSearchCV?
Consider what n_iter controls in the search process.
n_iter controls how many random hyperparameter combinations are tested. Increasing it means more combinations are evaluated, which can improve the chance of finding better parameters but takes more time.
After running RandomizedSearchCV, which attribute gives the mean test score for each parameter combination tested?
Look for the attribute that stores detailed results for all tested parameters.
cv_results_ is a dictionary containing detailed results, including mean_test_score for each parameter set tested. best_score_ is the best mean test score found.
What error will the following code raise?
from sklearn.datasets import load_iris
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
iris = load_iris()
X_train, y_train = iris.data, iris.target
param_dist = {'n_estimators': 100, 'max_depth': [3, 5, 7]}
clf = RandomForestClassifier()
search = RandomizedSearchCV(clf, param_distributions=param_dist, n_iter=2)
search.fit(X_train, y_train)Check the type of values in param_distributions.
RandomizedSearchCV expects each hyperparameter value to be a list or distribution to sample from. Here, 'n_estimators' is given as a single integer, causing a ValueError.
