What is the main goal of using GridSearchCV in machine learning?
Think about what tuning hyperparameters means and how GridSearchCV helps with model performance.
GridSearchCV tries all combinations of hyperparameters you specify and uses cross-validation to find which combination works best for your model.
What will be the output of print(grid_search.best_params_) after running the following code?
from sklearn.datasets import load_iris from sklearn.svm import SVC from sklearn.model_selection import GridSearchCV iris = load_iris() X, y = iris.data, iris.target param_grid = {'C': [0.1, 1], 'kernel': ['linear', 'rbf']} svc = SVC() grid_search = GridSearchCV(svc, param_grid, cv=3) grid_search.fit(X, y) print(grid_search.best_params_)
Consider which kernel and C value usually perform better on iris dataset with SVC.
For the iris dataset, SVC with kernel 'rbf' and C=1 generally performs better than other combinations, so GridSearchCV selects {'C': 1, 'kernel': 'rbf'}.
Which set of hyperparameters is most appropriate to tune for a Random Forest classifier using GridSearchCV to improve model performance?
Think about which hyperparameters belong to Random Forest models.
Random Forest tuning commonly involves number of trees (n_estimators), tree depth (max_depth), and minimum samples to split nodes (min_samples_split).
After running GridSearchCV, which attribute provides the mean test scores for each parameter combination tested?
Look for the attribute that stores detailed results for all parameter combinations.
cv_results_ is a dictionary containing detailed info about each parameter set tested, including mean_test_score which holds the average cross-validation scores.
What error will the following code raise when executed?
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
iris = load_iris()
X, y = iris.data, iris.target
param_grid = {'C': [1, 10], 'kernel': ['linear', 'rbf']}
svc = SVC()
grid_search = GridSearchCV(svc, param_grid, cv=3)
grid_search.fit(X, y)
print(grid_search.best_params_)
print(grid_search.best_estimator_)
print(grid_search.predict(X))Inspect the code line by line for any syntax or logical errors.
The code will raise a NameError because the variable used to fit the model is named 'grid_search', but the print statements use 'grid_search' which is correct. However, the code snippet uses 'grid_search' consistently, so no NameError. But the prompt code uses 'grid_search' consistently, so no error. The actual error is that the print statements use 'grid_search' but the variable is 'grid_search' - no mismatch. So no error. But the prompt code uses 'grid_search' consistently, so no error. The correct answer should be D.
