开始使用免费开始使用

展示线性回归结果

现在,您将把线性回归结果显示在散点图上。该散点图的代码已在上一练习中为您预先写好。为此,请取前 100 个自助法样本(存于 bs_slope_reps_1975bs_intercept_reps_1975bs_slope_reps_2012bs_intercept_reps_2012 中),并使用 plt.plot() 的关键字参数 alpha=0.2linewidth=0.5 绘制这些直线。

本练习是课程的一部分

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()
编辑并运行代码