Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the stacking classifier from scikit-learn.
ML Python
from sklearn.ensemble import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing VotingClassifier instead of StackingClassifier.
Importing RandomForestClassifier which is a single model, not stacking.
✗ Incorrect
The StackingClassifier is the correct class to import for stacking models in scikit-learn.
2fill in blank
mediumComplete the code to create a stacking classifier with logistic regression as the final estimator.
ML Python
stack = StackingClassifier(estimators=estimators, final_estimator=[1]()) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a complex model like RandomForestClassifier as final estimator.
Using a base estimator instead of a final estimator.
✗ Incorrect
LogisticRegression is commonly used as the final estimator in stacking to combine base model predictions.
3fill in blank
hardFix the error in the code to correctly fit the stacking classifier on training data.
ML Python
stacking_model = StackingClassifier(estimators=estimators, final_estimator=LogisticRegression())
stacking_model.[1](X_train, y_train) Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling predict before fitting the model.
Using transform which is for feature processing.
✗ Incorrect
The fit method trains the stacking model on the training data.
4fill in blank
hardFill both blanks to create a blending dataset by training base models and collecting their predictions.
ML Python
blend_data = np.zeros((X_val.shape[0], len(estimators))) for i, (name, model) in enumerate(estimators): model.[1](X_train, y_train) blend_data[:, i] = model.[2](X_val)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using transform instead of predict for collecting predictions.
Calling score instead of fit to train models.
✗ Incorrect
First, each base model is trained with fit, then predictions are collected with predict to form the blending dataset.
5fill in blank
hardFill all three blanks to define a stacking classifier with two base models and a final estimator.
ML Python
estimators = [("rf", RandomForestClassifier()), ("svc", [1]())] stack = StackingClassifier(estimators=estimators, final_estimator=[2]()) stack.fit([3], y_train)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using DecisionTreeClassifier instead of SVC as base model.
Passing y_train instead of X_train to fit.
✗ Incorrect
SVC is used as a base model, LogisticRegression as the final estimator, and X_train is the training data to fit the model.