เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

แสดงภาพ Bootstrap

ต่อจากที่ทำค้างไว้ก่อนหน้าในบทเรียนนี้ มาแสดงภาพการกระจายตัวแบบ Bootstrap ของค่าความเร็วที่ประมาณได้จากการสุ่มตัวอย่างแบบ Bootstrap กัน โดยเราได้คำนวณค่า Least-Squares Fit ของ Slope สำหรับทุกตัวอย่าง เพื่อทดสอบความแปรปรวนหรือความไม่แน่นอนในการประมาณค่า Slope

เพื่อให้เริ่มต้นได้ทันที มีฟังก์ชัน compute_resample_speeds(distances, times) โหลดไว้ให้แล้ว สำหรับการคำนวณและสร้างการกระจายตัวของตัวอย่างความเร็ว

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Introduction to Linear Modeling in Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • ใช้ฟังก์ชัน compute_resample_speeds(distances, times) ที่กำหนดไว้แล้วเพื่อคำนวณ resample_speeds
  • ใช้ np.mean() เพื่อคำนวณ speed_estimate จาก resample_speeds
  • ใช้ np.percentile() พร้อมค่า [5, 95] เพื่อคำนวณ percentiles ของ resample_speeds ซึ่งกำหนดขอบเขตของช่วงความเชื่อมั่น
  • ใช้ axis.hist() เพื่อพล็อต resample_speeds โดยระบุ Bin ด้วย hist_bin_edges
  • ใช้ axis.axvline ระบุ Index ที่ถูกต้องทั้ง สอง ค่าของ percentiles เพื่อทำเครื่องหมายขอบเขตของช่วงความเชื่อมั่นบนกราฟ

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# Create the bootstrap distribution of speeds
resample_speeds = compute_resample_speeds(____, ____)
speed_estimate = np.mean(____)
percentiles = np.percentile(____, [5, 95])

# Plot the histogram with the estimate and confidence interval
fig, axis = plt.subplots()
hist_bin_edges = np.linspace(0.0, 4.0, 21)
axis.hist(____, ____, color='green', alpha=0.35, rwidth=0.8)
axis.axvline(speed_estimate, label='Estimate', color='black')
axis.axvline(percentiles[____], label=' 5th', color='blue')
axis.axvline(percentiles[____], label='95th', color='blue')
axis.legend()
plt.show()
แก้ไขและรันโค้ด