How can SciKit-Learn Random Forest sub sample size may be equal to original training data size?

Viewed 3118

In the documentation of SciKit-Learn Random Forest classifier , it is stated that

The sub-sample size is always the same as the original input sample size but the samples are drawn with replacement if bootstrap=True (default).

What I dont understand is that if the sample size is always the same as the input sample size than how can we talk about a random selection. There is no selection here because we use all the (and naturally the same) samples at each training.

Am I missing something here?

4 Answers

The answer from @communitywiki misses out the question: "What I dont understand is that if the sample size is always the same as the input sample size than how can we talk about a random selection": It has to do with the nature of bootstrapping itself. Bootstrapping includes repeating the same values different times but still have same sample size as original data: Example (courtesy wiki page of Bootstrapping/Approach):

  • Original Sample : [1,2,3,4,5]

  • Boostrap 1 : [1,2,4,4,1]

  • Bootstrap 2: [1,1,3,3,5]

    and so on.

This is how random selection can occur and still sample size can remain same.

Although I am pretty new to python, I had a similar problem.

I tried to fit a RandomForestClassifier to my data. I splitted the data into train and test:

train_x, test_x, train_y, test_y = train_test_split(X, Y, test_size=0.2, random_state=0)

The length of the DFs were the same but after I predicted the model:

rfc_pred = rf_mod.predict(test_x)

The results had a different length.

To solve this I set the bootstrap option to false:

param_grid = {
    'bootstrap': [False],
    'max_depth': [110, 150, 200],
    'max_features': [3, 5],
    'min_samples_leaf': [1, 3],
    'min_samples_split': [6, 8],
    'n_estimators': [100, 200]
}

And ran the process all over again. It worked fine and I could calculate my confusion matrix. But I wish to understand how to use bootstrap and generate the predicted data with the same length.

Related