0
0
ML Pythonml~10 mins

Pipeline with GridSearchCV in ML Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a pipeline with a scaler and a logistic regression model.

ML Python
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression

pipeline = Pipeline([('scaler', StandardScaler()), ('clf', [1])])
Drag options to blanks, or click blank then click option'
AKNeighborsClassifier()
BRandomForestClassifier()
CLogisticRegression()
DSVC()
Attempts:
3 left
💡 Hint
Common Mistakes
Using a classifier other than LogisticRegression when the task specifies logistic regression.
Forgetting to instantiate the model with parentheses.
2fill in blank
medium

Complete the code to define the parameter grid for GridSearchCV to tune logistic regression's C parameter.

ML Python
param_grid = {'clf__[1]': [0.1, 1, 10]}
Drag options to blanks, or click blank then click option'
Asolver
Bpenalty
Cmax_iter
DC
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'penalty' instead of 'C' for regularization strength.
Not prefixing the parameter with 'clf__' to target the pipeline step.
3fill in blank
hard

Fix the error in the GridSearchCV instantiation by completing the missing argument.

ML Python
from sklearn.model_selection import GridSearchCV

grid_search = GridSearchCV(pipeline, param_grid, [1]=5)
Drag options to blanks, or click blank then click option'
Acv
Bscoring
Cverbose
Dn_jobs
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'scoring' instead of 'cv' for cross-validation folds.
Omitting the 'cv' argument leading to default behavior.
4fill in blank
hard

Fill both blanks to fit the grid search on training data and get the best parameters.

ML Python
grid_search.[1](X_train, y_train)
best_params = grid_search.[2]
Drag options to blanks, or click blank then click option'
Afit
Bpredict
Cbest_params_
Dscore
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'predict' instead of 'fit' to train the model.
Trying to access 'best_params' without the trailing underscore.
5fill in blank
hard

Fill all three blanks to predict on test data, calculate accuracy, and print the result.

ML Python
from sklearn.metrics import [1]
predictions = grid_search.[2](X_test)
accuracy = [3](y_test, predictions)
print(f'Accuracy: {accuracy:.2f}')
Drag options to blanks, or click blank then click option'
Aaccuracy_score
Bpredict
Dconfusion_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using confusion_matrix instead of accuracy_score for metric.
Calling predict on pipeline instead of grid_search.