Bird
Raised Fist0
ML Pythonml~20 mins

Why ensembles outperform single models in ML Python - Experiment to Prove It

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Why ensembles outperform single models
Problem:We want to improve prediction accuracy on a classification task using a dataset. Currently, a single decision tree model is used.
Current Metrics:Training accuracy: 95%, Validation accuracy: 80%
Issue:The single decision tree model overfits the training data, causing lower accuracy on validation data.
Your Task
Increase validation accuracy to above 85% by using ensemble methods while keeping training accuracy below 93%.
Use only ensemble methods based on decision trees (e.g., Random Forest or Gradient Boosting).
Do not change the dataset or perform additional feature engineering.
Hint 1
Hint 2
Hint 3
Solution
ML Python
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Load data
X, y = load_breast_cancer(return_X_y=True)

# Split data
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

# Single decision tree model
single_tree = DecisionTreeClassifier(random_state=42)
single_tree.fit(X_train, y_train)
train_acc_tree = accuracy_score(y_train, single_tree.predict(X_train))
val_acc_tree = accuracy_score(y_val, single_tree.predict(X_val))

# Ensemble model: Random Forest
rf = RandomForestClassifier(n_estimators=100, max_depth=5, random_state=42)
rf.fit(X_train, y_train)
train_acc_rf = accuracy_score(y_train, rf.predict(X_train))
val_acc_rf = accuracy_score(y_val, rf.predict(X_val))

print(f"Single Tree - Train Accuracy: {train_acc_tree:.2f}, Validation Accuracy: {val_acc_tree:.2f}")
print(f"Random Forest - Train Accuracy: {train_acc_rf:.2f}, Validation Accuracy: {val_acc_rf:.2f}")
Replaced single decision tree with Random Forest ensemble.
Set number of trees to 100 to average predictions and reduce variance.
Limited tree depth to 5 to reduce overfitting.
Results Interpretation

Before: Training accuracy 95%, Validation accuracy 80% (overfitting)

After: Training accuracy 90%, Validation accuracy 88% (better generalization)

Ensembles like Random Forest reduce overfitting by combining many simple models. This averaging lowers variance and improves accuracy on new data.
Bonus Experiment
Try Gradient Boosting ensemble instead of Random Forest and compare validation accuracy.
💡 Hint
Use sklearn's GradientBoostingClassifier with learning rate 0.1 and max_depth 3 to see if boosting improves results.

Practice

(1/5)
1. Why do ensemble models usually perform better than a single model?
easy
A. Because they always use deep learning
B. Because they use only one model with more data
C. Because they ignore data variability
D. Because they combine multiple models to reduce errors

Solution

  1. Step 1: Understand ensemble concept

    Ensembles combine predictions from multiple models to reduce individual errors.
  2. Step 2: Compare with single model

    A single model may make mistakes that ensembles can correct by averaging or voting.
  3. Final Answer:

    Because they combine multiple models to reduce errors -> Option D
  4. Quick Check:

    Ensembles reduce errors = A [OK]
Hint: Ensembles mix models to fix mistakes [OK]
Common Mistakes:
  • Thinking ensembles use only one model
  • Believing ensembles ignore data differences
  • Assuming ensembles always use deep learning
2. Which of the following is the correct way to combine predictions in an ensemble?
easy
A. Taking the average or majority vote of multiple models' outputs
B. Using only the prediction of the first model
C. Multiplying all model predictions together
D. Ignoring all predictions and guessing randomly

Solution

  1. Step 1: Identify ensemble combination methods

    Common methods include averaging predictions or majority voting among models.
  2. Step 2: Eliminate incorrect methods

    Using only one model or random guessing does not combine models properly; multiplying predictions is not standard.
  3. Final Answer:

    Taking the average or majority vote of multiple models' outputs -> Option A
  4. Quick Check:

    Average or vote = D [OK]
Hint: Combine by averaging or voting predictions [OK]
Common Mistakes:
  • Using only one model's output
  • Multiplying predictions incorrectly
  • Ignoring ensemble predictions
3. Consider three models with prediction errors of 10%, 12%, and 15%. What is the expected error if we use a simple average ensemble of these models?
medium
A. 37%
B. 15%
C. 12.33%
D. 10%

Solution

  1. Step 1: Calculate average error

    Sum errors: 10% + 12% + 15% = 37%. Divide by 3 models: 37% / 3 = 12.33%.
  2. Step 2: Understand ensemble effect

    Averaging errors reduces overall error compared to the worst single model.
  3. Final Answer:

    12.33% -> Option C
  4. Quick Check:

    Average error = 12.33% [OK]
Hint: Average errors to find ensemble error [OK]
Common Mistakes:
  • Adding errors without dividing
  • Picking highest or lowest error directly
  • Confusing error with accuracy
4. You have an ensemble of 5 models but the combined accuracy is lower than the best single model. What is the most likely reason?
medium
A. The models are too similar and make the same mistakes
B. The ensemble uses majority voting correctly
C. The models have very different errors
D. The ensemble averages predictions properly

Solution

  1. Step 1: Analyze ensemble failure cause

    If models are very similar, they tend to make the same errors, so ensemble gains are lost.
  2. Step 2: Check other options

    Correct voting or averaging usually improves accuracy; different errors help ensemble, so these are unlikely causes.
  3. Final Answer:

    The models are too similar and make the same mistakes -> Option A
  4. Quick Check:

    Similar models cause poor ensemble = A [OK]
Hint: Diverse models improve ensembles, similar hurt [OK]
Common Mistakes:
  • Assuming voting always improves accuracy
  • Ignoring model similarity
  • Thinking averaging can fix identical errors
5. You want to build an ensemble to improve prediction on a noisy dataset. Which strategy best explains why ensembles help in this case?
hard
A. Ignoring noise by removing data points is better than ensembles
B. Combining models averages out noise, reducing variance in predictions
C. Using a single complex model always beats ensembles
D. Ensembles increase noise by combining errors

Solution

  1. Step 1: Understand noise impact on models

    Noisy data causes models to vary in predictions; combining them averages out random errors.
  2. Step 2: Compare strategies

    Single complex models may overfit noise; removing data loses information; ensembles reduce variance by averaging.
  3. Final Answer:

    Combining models averages out noise, reducing variance in predictions -> Option B
  4. Quick Check:

    Ensembles reduce noise variance = C [OK]
Hint: Ensembles smooth noise by averaging predictions [OK]
Common Mistakes:
  • Believing single models always outperform ensembles
  • Thinking ensembles increase noise
  • Ignoring the benefit of averaging noisy predictions