<jupyter_start><jupyter_text>Bootstrap replicatesin other words repeating the same experiment for a given number of times.<jupyter_code># importing required modules
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
import numpy as np

# runing the functions script
%run stats_func.py
# loading the iris dataset
df = pd.read_csv('iris.csv')
df.head()
# extracting sepal length and sepal width for further analysis
sepalLength = np.array(df['SepalLengthCm'])
sepalWidth = np.array(df['SepalWidthCm'])<jupyter_output><empty_output><jupyter_text>For further experiments Sepal Length variable will be used. Additionally, we will focus on bootstrap technique used for mean.<jupyter_code># calculating the mean from data
np.mean(sepalLength)<jupyter_output><empty_output><jupyter_text>Use of numpy.random.choice for bootstrapingFor the boostraping technique we will use numpy.random.choice function that will randomly select the data from a given variable forming a new data set. Then a function of choice can be used to calculate required statistics. In our case it will be mean value.The code of the function:    def bootstrap_replicate_1d(data, func):        '''Generate bootstrap replicate of 1D data'''        bs_sample = np.random.choice(data, len(data))        return func(bs_sample)To show the impact of the function on the mean value, the function is called three times in the cells below:<jupyter_code>bootstrap_replicate_1d(sepalLength, np.mean)
bootstrap_replicate_1d(sepalLength, np.mean)
bootstrap_replicate_1d(sepalLength, np.mean)<jupyter_output><empty_output><jupyter_text>It would be interesting to repeat call of **boostrap_replicate_1d** function many times to see how the mean value changes. For that reason another function is defined:    def draw_bs_reps(data, func, size=1):        """Draw bootstrap replicates."""         Initialize array of replicates: bs_replicates        bs_replicates = np.empty(size)         Generate replicates        for i in range(size):            bs_replicates[i] = bootstrap_replicate_1d(data, func)        return bs_replicatesIn the example we will call the **boostrap_replicate_1d** function 10,000 times.<jupyter_code>bs_replicates = draw_bs_reps(sepalLength, np.mean, size=10000)
# histogram plot will show us how the mean value of sepal length changes when the experiment is repeated 10,000 times
plt.hist(bs_replicates, bins=30, normed=True, edgecolor='black')
plt.xlabel("Mean sepal length [cm]")
plt.ylabel('PDF');
# calculating 95% confidence interval for the mean based on boostrap technique
conf_intervals = np.percentile(bs_replicates, [2.5, 97.5])
conf_intervals<jupyter_output><empty_output><jupyter_text>Repeating the same exercise for Sepal Width<jupyter_code>bs_replicates2 = draw_bs_reps(sepalWidth, np.mean, size=10000)

# histogram plot will show us how the mean value of sepal width changes when the experiment is repeated 10,000 times
plt.hist(bs_replicates2, bins=30, normed=True, edgecolor='black')
plt.xlabel("Mean sepal width [cm]")
plt.ylabel('PDF');
# calculating 95% confidence interval for the mean based on boostrap technique
conf_intervals2 = np.percentile(bs_replicates2, [2.5, 97.5])
conf_intervals2<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.07841328413284133, 'fraction_numerical': 0.017527675276752766, 'mean_word_length': 5.791231732776618, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '455064', 'license': 'MIT', 'n_tokens_mistral': 1034, 'n_tokens_neox': 962, 'n_words': 337, 'path': '03_bootstrap_replicates.ipynb', 'repo_name': 'rafburzy/Statistics', 'size': '3252'}