0
0
ML Pythonml~10 mins

Bagging concept 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 multiple bootstrap samples for bagging.

ML Python
from sklearn.utils import resample

samples = [resample(data, replace=[1], n_samples=100) for _ in range(10)]
Drag options to blanks, or click blank then click option'
AFalse
B0
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using replace=False will create samples without replacement, which is not bagging.
Setting replace to None or 0 causes errors.
2fill in blank
medium

Complete the code to train multiple decision trees on bootstrap samples.

ML Python
from sklearn.tree import DecisionTreeClassifier

models = []
for sample in samples:
    clf = DecisionTreeClassifier()
    clf.fit(sample[[1]], sample[[2]])
    models.append(clf)
Drag options to blanks, or click blank then click option'
Atarget
BX
Cy
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole data instead of target for labels.
Confusing features and target variables.
3fill in blank
hard

Fix the error in the code to aggregate predictions from all models by majority vote.

ML Python
import numpy as np

predictions = np.array([model.predict(X_test) for model in models])
final_prediction = np.apply_along_axis(lambda x: np.bincount(x).[1](), axis=0, arr=predictions)
Drag options to blanks, or click blank then click option'
Asum
Bargmax
Cmax
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using max returns the maximum count value, not the index.
Using sum or mean does not give the majority class.
4fill in blank
hard

Fill both blanks to create a bagging ensemble using scikit-learn's BaggingClassifier.

ML Python
from sklearn.ensemble import BaggingClassifier

bagging = BaggingClassifier(base_estimator=[1], n_estimators=10, random_state=42)
bagging.fit([2], y_train)
Drag options to blanks, or click blank then click option'
ADecisionTreeClassifier()
BRandomForestClassifier()
CX_train
DX_test
Attempts:
3 left
💡 Hint
Common Mistakes
Using RandomForestClassifier as base estimator is incorrect.
Using test data X_test for training causes errors.
5fill in blank
hard

Fill all three blanks to evaluate bagging model accuracy on test data.

ML Python
from sklearn.metrics import [1]

preds = bagging.predict([2])
accuracy = [3](y_test, preds)
print(f"Accuracy: {accuracy:.2f}")
Drag options to blanks, or click blank then click option'
Aaccuracy_score
BX_test
Dconfusion_matrix
Attempts:
3 left
💡 Hint
Common Mistakes
Using confusion_matrix instead of accuracy_score for accuracy.
Predicting on training data instead of test data.