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)
Cet exercice fait partie du cours
Ensemble Methods in Python
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.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de 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
____