Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is stratified bootstrap?

I have learned bootstrap and stratification. But what is stratified bootstrap? And how does it work?

Let's say we have a dataset of n instances (observations), and m is the number of classes. How should I divide the dataset, and what's the percentage for training and testing?

like image 932
Kevin217 Avatar asked Feb 10 '16 23:02

Kevin217


2 Answers

Just had to implement this in python, I will just post my current approach here in case this is of interest for others.

Function to create index for original Dataframe to create stratified bootstrapped sample

I chose to iterate over all relevant strata clusters in the original Dataframe , retrieve the index of the relevant rows in each stratum and randomly (with replacement) draw the same amount of samples from the stratum that this very stratum consists of.

In turn, the randomly drawn indices can just be combined into one list (that should in the end have the same length as the original Dataframe).

import pandas as pd
from random import choices

def provide_stratified_bootstap_sample_indices(bs_sample):

    strata = bs_sample.loc[:, "STRATIFICATION_VARIABLE"].value_counts()
    bs_index_list_stratified = []

    for idx_stratum_var, n_stratum_var in strata.iteritems():

        data_index_stratum = list(bs_sample[bs_sample["STRATIFICATION_VARIABLE"] == idx_stratum_var[0]].index)
        bs_index_list_stratified.extend(choices(data_index_stratum , k = len(data_index_stratum )))

    return bs_index_list_stratified

And then the actual bootstrapping loop

(say 10k times):

k=10000

for i in range(k):
    bs_sample = DATA_original.copy()

    bs_index_list_stratified = provide_stratified_bootstap_sample_indices(bs_sample)
    bs_sample = bs_sample.loc[bs_index_list_stratified , :]

    # process data with some statistical operation as required and save results as required for each iteration
    RESULTS = FUNCTION_X(bs_sample)
like image 93
Hal Koons Avatar answered Oct 01 '22 17:10

Hal Koons


You split your dataset per class. Afterwards, you sample from each sub-population independently. The number of instances you sample from one sub-population should be relative to its proportion.

 data
 d(i) <- { x in data | class(x) =i }
 for each class
    for j = 0..samplesize*(size(d(i))/size(data))
       sample(i) <- draw element from d(i)
 sample <- U sample(i)

If you sample four elements from a dataset with classes {'a', 'a', 'a', 'a', 'a', 'a', 'b', 'b'}, this procedure makes sure that at least one element of class b is contained in the stratified sample.

like image 30
CAFEBABE Avatar answered Oct 01 '22 19:10

CAFEBABE