Complete the code to create multiple bootstrap samples for bagging.
from sklearn.utils import resample samples = [resample(data, replace=[1], n_samples=100) for _ in range(10)]
Bagging uses bootstrap samples with replacement, so replace=True is needed.
Complete the code to train multiple decision trees on bootstrap samples.
from sklearn.tree import DecisionTreeClassifier models = [] for sample in samples: clf = DecisionTreeClassifier() clf.fit(sample[[1]], sample[[2]]) models.append(clf)
Decision trees are trained using features X and target y.
Fix the error in the code to aggregate predictions from all models by majority vote.
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)
max returns the maximum count value, not the index.sum or mean does not give the majority class.np.bincount(x).argmax() returns the most common prediction (majority vote) among models.
Fill both blanks to create a bagging ensemble using scikit-learn's BaggingClassifier.
from sklearn.ensemble import BaggingClassifier bagging = BaggingClassifier(base_estimator=[1], n_estimators=10, random_state=42) bagging.fit([2], y_train)
X_test for training causes errors.The base estimator for bagging is usually a decision tree, and training uses X_train features.
Fill all three blanks to evaluate bagging model accuracy on test data.
from sklearn.metrics import [1] preds = bagging.predict([2]) accuracy = [3](y_test, preds) print(f"Accuracy: {accuracy:.2f}")
Use accuracy_score metric and X_test features to predict and evaluate accuracy.