신뢰 구간의 부트스트래핑
데이터의 변동성을 평가할 때 유용한 도구가 바로 부트스트랩입니다. 이번 연습에서는 부트스트랩 신뢰 구간을 반환하는 사용자 정의 부트스트래핑 함수를 직접 작성해 보겠습니다.
이 함수는 세 가지 매개변수를 받습니다: 숫자로 이루어진 2차원 배열(data), 계산할 백분위수 목록(percentiles), 사용할 부트스트랩 반복 횟수(n_boots). 이 함수는 resample 함수를 사용해 부트스트랩 샘플을 생성하고, 이를 여러 번 반복해 신뢰 구간을 계산합니다.
이 연습은 강의의 일부입니다
Python으로 배우는 시계열 데이터 Machine Learning
연습 안내
- 함수는 부트스트랩 횟수(
n_boots매개변수로 지정)를 반복하면서 다음을 수행해야 해요:- 데이터에서 복원 추출로 무작위 샘플을 뽑고, 그 무작위 샘플의 평균을 계산하세요
bootstrap_means의 백분위수를 계산해 반환하세요
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
from sklearn.utils import ____
def bootstrap_interval(data, percentiles=(2.5, 97.5), n_boots=100):
"""Bootstrap a confidence interval for the mean of columns of a 2-D dataset."""
# Create our empty array to fill the results
bootstrap_means = np.zeros([n_boots, data.shape[-1]])
for ii in range(____):
# Generate random indices for our data *with* replacement, then take the sample mean
random_sample = ____
bootstrap_means[ii] = random_sample.mean(axis=0)
# Compute the percentiles of choice for the bootstrapped means
percentiles = ____(bootstrap_means, percentiles, axis=0)
return percentiles