0
0
ML Pythonml~20 mins

Bagging concept in ML Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bagging Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the main purpose of bagging in machine learning?

Bagging is a technique used in machine learning. What is its main goal?

ATo increase the bias of a model by simplifying the training data.
BTo reduce the size of the training dataset to speed up training.
CTo combine models by selecting the single best performing model.
DTo reduce the variance of a model by training multiple models on different samples and averaging their predictions.
Attempts:
2 left
💡 Hint

Think about how bagging uses multiple models and what problem it tries to solve.

Predict Output
intermediate
1:30remaining
Output of bagging predictions averaging

Given three models trained on different samples, their predictions on a test point are: Model1: 0.7, Model2: 0.4, Model3: 0.9. What is the final bagging prediction by averaging?

A0.73
B0.67
C0.80
D0.50
Attempts:
2 left
💡 Hint

Calculate the average of the three predictions.

Model Choice
advanced
2:00remaining
Which model type benefits most from bagging?

Bagging is most effective in reducing variance. Which of these model types typically benefits the most from bagging?

ADecision trees with high depth (complex trees)
BLinear regression models
CSimple logistic regression models
DNaive Bayes classifiers
Attempts:
2 left
💡 Hint

Think about which models tend to have high variance and overfit easily.

Hyperparameter
advanced
2:00remaining
Effect of increasing number of base models in bagging

What is the effect of increasing the number of base models (estimators) in a bagging ensemble?

AIt generally decreases variance and improves stability up to a point, but with diminishing returns.
BIt increases bias and reduces model accuracy.
CIt causes the model to overfit the training data more.
DIt has no effect on the ensemble's performance.
Attempts:
2 left
💡 Hint

Think about how averaging more models affects variance and prediction stability.

🔧 Debug
expert
3:00remaining
Identify the error in this bagging implementation snippet

Consider this Python code snippet for bagging:

from sklearn.tree import DecisionTreeClassifier
from sklearn.utils import resample

X_train, y_train = ...  # training data
models = []
for _ in range(5):
    X_sample, y_sample = resample(X_train, y_train)
    model = DecisionTreeClassifier()
    model.fit(X_sample, y_sample)
    models.append(model)

# Predict on test data
predictions = []
for model in models:
    predictions.append(model.predict(X_test))

final_prediction = sum(predictions) / len(models)

What error will this code raise or what is the problem?

ANameError because X_test is not defined.
BValueError because resample requires additional parameters.
CTypeError because predictions are arrays and cannot be summed directly with sum().
DNo error; code runs correctly and outputs final predictions.
Attempts:
2 left
💡 Hint

Check the type of objects in predictions and how sum() works on lists of arrays.