การ Bootstrap เพื่อสร้างช่วงความเชื่อมั่น
Bootstrap เป็นเครื่องมือที่มีประโยชน์สำหรับประเมินความแปรปรวนของข้อมูล ในแบบฝึกหัดนี้ คุณจะเขียนฟังก์ชัน bootstrapping ขึ้นเองเพื่อใช้คืนค่าช่วงความเชื่อมั่นแบบ bootstrap
ฟังก์ชันนี้รับพารามิเตอร์สามตัว ได้แก่ อาร์เรย์ตัวเลข 2 มิติ (data) รายการเปอร์เซ็นไทล์ที่ต้องการคำนวณ (percentiles) และจำนวนรอบของการทำ bootstrap (n_boots) โดยใช้ฟังก์ชัน resample เพื่อสร้างตัวอย่าง bootstrap แล้วทำซ้ำหลายรอบเพื่อคำนวณช่วงความเชื่อมั่น
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Machine Learning สำหรับข้อมูล Time Series ใน Python
คำแนะนำการฝึกหัด
- ฟังก์ชันควรวนลูปตามจำนวนรอบ bootstrap (กำหนดโดยพารามิเตอร์
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