This function as written has a bug. Non positional indexing on data frames will result in behavior different than positional sampling. When I run the notebook as is, I get
intercept 0.717654 horsepower 0.006041
To fix this, we can just write:
def boot_SE_fixed(func, D, n=None, B=1000, seed=0):
rng = np.random.default_rng(seed)
n = len(D) if n is None else n
first_, second_ = 0, 0
for _ in range(B):
positions = rng.choice(
len(D),
size=n,
replace=True,
)
value = func(D, positions)
first_ += value
second_ += value**2
return np.sqrt(second_ / B - (first_ / B)**2)
( here we rng.choice on the length of D )
And
def boot_OLS_fixed(model_matrix, response, D, idx):
D_ = D.iloc[idx]
Y_ = D_[response]
X_ = clone(model_matrix).fit_transform(D_)
return sm.OLS(Y_, X_).fit().params
here we use iloc instead of loc.
hp_func_fixed = partial(boot_OLS_fixed, MS(['horsepower']), 'mpg')
Finally, we correctly wrap it and:
hp_se = boot_SE_fixed(hp_func_fixed,
Auto,
B=1000,
seed=0)
hp_se
produces the desired results, making it consistent with the markdown below.

ISLP_labs/Ch05-resample-lab.ipynb
Line 860 in 4854ffe
This function as written has a bug. Non positional indexing on data frames will result in behavior different than positional sampling. When I run the notebook as is, I get
intercept 0.717654 horsepower 0.006041To fix this, we can just write:
( here we rng.choice on the length of D )
And
here we use iloc instead of loc.
Finally, we correctly wrap it and:
produces the desired results, making it consistent with the markdown below.