Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The pipeline requires a logistic regression model instance as the classifier step.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The 'C' parameter controls the inverse of regularization strength in logistic regression.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'scoring' instead of 'cv' for cross-validation folds.
Omitting the 'cv' argument leading to default behavior.
✗ Incorrect
The 'cv' argument sets the number of cross-validation folds in GridSearchCV.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'predict' instead of 'fit' to train the model.
Trying to access 'best_params' without the trailing underscore.
✗ Incorrect
You fit the grid search with training data and then access best_params_ to get the best parameters.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using confusion_matrix instead of accuracy_score for metric.
Calling predict on pipeline instead of grid_search.
✗ Incorrect
Import accuracy_score, use grid_search.predict to get predictions, then calculate accuracy_score with true labels.