LoslegenKostenlos loslegen

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)

Diese Übung ist Teil des Kurses

Ensemble Methods in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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
____
Code bearbeiten und ausführen