0
0
ML Pythonml~20 mins

Pipeline with GridSearchCV in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pipeline Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of GridSearchCV best parameters in pipeline
What will be the output of the following code snippet after fitting the GridSearchCV pipeline?
ML Python
from sklearn.datasets import load_iris
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC

iris = load_iris()
X, y = iris.data, iris.target

pipe = Pipeline([
    ('scaler', StandardScaler()),
    ('svc', SVC())
])

param_grid = {
    'svc__C': [0.1, 1],
    'svc__kernel': ['linear', 'rbf']
}

gs = GridSearchCV(pipe, param_grid, cv=3)
gs.fit(X, y)

print(gs.best_params_)
A{'svc__C': 0.1, 'svc__kernel': 'rbf'}
B{'svc__C': 0.1, 'svc__kernel': 'linear'}
C{'svc__C': 1, 'svc__kernel': 'linear'}
D{'svc__C': 1, 'svc__kernel': 'rbf'}
Attempts:
2 left
💡 Hint
Think about which kernel usually performs better on iris dataset with default settings.
Model Choice
intermediate
1:30remaining
Choosing the correct pipeline step for text data
You want to build a pipeline for text classification using GridSearchCV. Which pipeline step should you include before the classifier to convert text into numbers?
ACountVectorizer()
BStandardScaler()
CPCA()
DMinMaxScaler()
Attempts:
2 left
💡 Hint
Text data needs to be converted into numeric features before classification.
Hyperparameter
advanced
1:30remaining
Correct hyperparameter name in pipeline for GridSearchCV
Given a pipeline named 'pipe' with steps [('scaler', StandardScaler()), ('clf', RandomForestClassifier())], which is the correct hyperparameter name to tune the number of trees in GridSearchCV?
A'n_estimators'
B'pipe__n_estimators'
C'clf__n_estimators'
D'scaler__n_estimators'
Attempts:
2 left
💡 Hint
In pipelines, hyperparameters are prefixed by the step name and two underscores.
Metrics
advanced
1:30remaining
Evaluating GridSearchCV best score attribute
After fitting GridSearchCV with cv=5, what does the attribute 'best_score_' represent?
AThe training score of the best estimator on the full training data
BThe mean cross-validation score of the best estimator across the 5 folds
CThe test score on unseen data
DThe highest score from a single fold during cross-validation
Attempts:
2 left
💡 Hint
GridSearchCV uses cross-validation to estimate performance.
🔧 Debug
expert
2:00remaining
Identifying error in pipeline parameter grid
What error will the following GridSearchCV code raise? from sklearn.pipeline import Pipeline from sklearn.preprocessing import StandardScaler from sklearn.linear_model import LogisticRegression from sklearn.model_selection import GridSearchCV pipe = Pipeline([ ('scaler', StandardScaler()), ('logreg', LogisticRegression()) ]) param_grid = { 'C': [0.1, 1, 10] } gs = GridSearchCV(pipe, param_grid) gs.fit(X, y)
AKeyError because 'C' is not a valid parameter name for the pipeline
BTypeError because LogisticRegression() is missing required arguments
CValueError because param_grid is empty
DNo error, code runs successfully
Attempts:
2 left
💡 Hint
In pipelines, hyperparameters must be prefixed by the step name.