How to do it...

Now that we have read our data, let's see how we can perform bootstrap sampling:

  1. We write a custom function, create_bootstrap_oob(), which takes a DataFrame as a parameter and uses the resample() function from sklearn.utils to create a bootstrap sample with 100 observations:
# This custom function takes a dataframe as an argument
def create_bootstrap_oob(df):
global df_OOB
global df_bootstrap_sample

# creating the bootstrap sample
df_bootstrap_sample = resample(df, replace=True, n_samples=100)

# creating the OOB sample
bootstrap_sample_index = tuple(df_bootstrap_sample.index)
bootstrap_df = df.index.isin(bootstrap_sample_index)
df_OOB = df[~bootstrap_df]
  1. We loop through 50 iterations and call the custom function by passing the df_autodata DataFrame. We capture the mean of the mpg variable for each bootstrap sample, which we'll measure against the mean of the mpg variable in our original DataFrame, which is df_autodata:
iteration=50
bootstap_statistics=list()
originalsample_statistics=list()

for i in range(iteration):
# Call custom function create_bootstrap_oob(). Pass df_autodata
create_bootstrap_oob(df_autodata)

# Capture mean value of mpg variable for all bootstrap samples
bootstap_statistics.append(df_bootstrap_sample.iloc[:,0].mean())

originalsample_statistics.append(df_autodata['mpg'].mean())
  1. We plot the mean of the mpg variable for each iteration, for which a separate bootstrap sample has been considered. We capture the mean of the mpg variable for each bootstrap sample in each iteration:
import matplotlib.pyplot as plt
f, ax= plt.subplots(figsize=(6,6))

plt.plot(bootstap_statistics, 'c--', label='Bootstrap Sample Statistic')
plt.plot(originalsample_statistics, 'grey', label='Original Sample Statistic')
plt.xlabel('Iterations')
plt.ylabel('Statistic (Mean of mpg)')
plt.legend(loc=4)
plt.show()

We finally plot the mean of the mpg variable against each iteration, which can be seen in the following image:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset