開始使用免費開始

顯示線性迴歸結果

現在你要把線性迴歸的結果顯示在散佈圖上;繪製散佈圖的程式碼已從前一個練習為你預先寫好。請從自助抽樣結果中取前 100 個樣本(存於 bs_slope_reps_1975bs_intercept_reps_1975bs_slope_reps_2012bs_intercept_reps_2012),並使用 plt.plot() 繪出線條,同時設定關鍵字參數 alpha=0.2linewidth=0.5

本練習屬於課程

Statistical Thinking in Python(第 2 部分)

檢視課程

練習說明

  • 使用 np.array() 產生自助抽樣線條的 \(x\) 值。它們應為 10 mm 與 17 mm。
  • 撰寫一個 for 迴圈,為 1975 與 2012 年的資料集各繪製 100 條自助抽樣線。1975 年資料集的線條顏色為 'blue',2012 年資料集的線條顏色為 'red'
  • 按下 Submit Answer 以檢視圖形!

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼