แสดงผลลัพธ์การถดถอยเชิงเส้น
ขั้นตอนนี้จะนำผลลัพธ์การถดถอยเชิงเส้นมาแสดงบน scatter plot ซึ่งโค้ดสำหรับสร้างกราฟได้เตรียมไว้ให้แล้วจากแบบฝึกหัดก่อนหน้า โดยให้นำ bootstrap sample 100 ตัวแรก (ที่เก็บอยู่ใน bs_slope_reps_1975, bs_intercept_reps_1975, bs_slope_reps_2012 และ bs_intercept_reps_2012) มาพล็อตเส้น โดยกำหนด alpha=0.2 และ linewidth=0.5 เป็น keyword arguments ใน plt.plot()
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Statistical Thinking in Python (ตอนที่ 2)
คำแนะนำการฝึกหัด
- สร้างค่าแกน \(x\) สำหรับเส้น bootstrap โดยใช้
np.array()โดยให้ประกอบด้วยค่า 10 มม. และ 17 มม. - เขียนลูป
forเพื่อพล็อต bootstrap line จำนวน 100 เส้นสำหรับชุดข้อมูลปี 1975 และ 2012 โดยให้เส้นของชุดข้อมูลปี 1975 เป็นสี'blue'และเส้นของชุดข้อมูลปี 2012 เป็นสี'red' - กด Submit เพื่อดูกราฟ!
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# Make scatter plot of 1975 data
_ = plt.plot(bl_1975, bd_1975, marker='.',
linestyle='none', color='blue', alpha=0.5)
# Make scatter plot of 2012 data
_ = plt.plot(bl_2012, bd_2012, marker='.',
linestyle='none', color='red', alpha=0.5)
# Label axes and make legend
_ = plt.xlabel('beak length (mm)')
_ = plt.ylabel('beak depth (mm)')
_ = plt.legend(('1975', '2012'), loc='upper left')
# Generate x-values for bootstrap lines: x
x = np.array([____, ____])
# Plot the bootstrap lines
for i in range(100):
plt.plot(____, ____,
linewidth=0.5, alpha=0.2, color=____)
plt.plot(____, ____,
linewidth=0.5, alpha=0.2, color=____)
# Draw the plot again
plt.show()