<jupyter_start><jupyter_text>Performing regression with use of bootstrap technique<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()
# for further analysis sepal length and sepal width will be used
sepalLength = np.array(df['SepalLengthCm'])
sepalWidth = np.array(df['SepalWidthCm'])

# the plot above is for all iris spieces, which can be listed via:
df['Species'].unique()
# the remaining analysis will focus only on Iris-setosa
sepalLengthSetosa = np.array(df['SepalLengthCm'][df['Species'] == 'Iris-setosa'])
sepalWidthSetosa = np.array(df['SepalWidthCm'][df['Species'] == 'Iris-setosa'])

# linear regression
a, b = np.polyfit(sepalLengthSetosa, sepalWidthSetosa, 1)

# regression line
x = np.linspace(4, 6, 10)
y = a * x + b

# scatter plot with regression
plt.plot(sepalLengthSetosa, sepalWidthSetosa, marker='.', linestyle='none', label='data points')
plt.plot(x, y, color='green', label='lin reg line')
plt.legend(loc='lower right')
plt.xlabel('Sepal length [cm] Iris-setosa')
plt.ylabel('Sepal width [cm] Iris-setosa');<jupyter_output><empty_output><jupyter_text>In order to use bootstrap technique for linear regression, the following function was used:    def draw_bs_pairs_linreg(x, y, size=1):        """Perform pairs bootstrap for linear regression."""         Set up array of indices to sample from: inds        inds = np.arange(len(x))         Initialize replicates: bs_slope_reps, bs_intercept_reps        bs_slope_reps = np.empty(size)        bs_intercept_reps = np.empty(size)         Generate replicates        for i in range(size):            bs_inds = np.random.choice(inds, size=len(inds))            bs_x, bs_y = x[bs_inds], y[bs_inds]            bs_slope_reps[i], bs_intercept_reps[i] = np.polyfit(bs_x, bs_y, 1)        return bs_slope_reps, bs_intercept_repsrandom choice function acts here on the indices of the data set and reorders two data sets. This type of function can be easily remade to use a function of choice instead of numpy.polyfit.In the example below we will use the function 100 times to receive the slope and intercept coefficients of linear regression.<jupyter_code># running bootstrap regression
bs_slope, bs_inter = draw_bs_pairs_linreg(sepalLengthSetosa, sepalWidthSetosa, 100)

# scatter plot with regression
plt.plot(sepalLengthSetosa, sepalWidthSetosa, marker='.', linestyle='none', label='data points')
plt.plot(x, y, color='red', label='lin reg line')
for i in range(len(bs_slope)):
    plt.plot(x, bs_slope[i] * x + bs_inter[i], color='green', alpha=0.1)
plt.legend(loc='lower right')
plt.xlabel('Sepal length [cm] Iris-setosa')
plt.ylabel('Sepal width [cm] Iris-setosa');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11192214111922141, 'fraction_numerical': 0.005213764337851929, 'mean_word_length': 5.084566596194503, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '519920', 'license': 'MIT', 'n_tokens_mistral': 1002, 'n_tokens_neox': 948, 'n_words': 287, 'path': '04_bootstrap_regression.ipynb', 'repo_name': 'rafburzy/Statistics', 'size': '2877'}