Get startedGet started for free

Training with bootstrapping

Let's now build a "weak" decision tree classifier and train it on a sample of the training set drawn with replacement. This will help you understand what happens on every iteration of a bagging ensemble.

To take a sample, you'll use pandas' .sample() method, which has a replace parameter. For example, the following line of code samples with replacement from the whole DataFrame df:

df.sample(frac=1.0, replace=True, random_state=42)

This exercise is part of the course

Ensemble Methods in Python

View Course

Exercise instructions

  • Take a sample drawn with replacement (replace=True) from the whole (frac=1.0) training set, X_train.
  • Build a decision tree classifier using the parameter max_depth = 4.
  • Fit the model to the sampled training data.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Take a sample with replacement
X_train_sample = X_train.____(____, ____, random_state=42)
y_train_sample = y_train.loc[X_train_sample.index]

# Build a "weak" Decision Tree classifier
clf = ____(____, random_state=500)

# Fit the model to the training sample
____
Edit and Run Code