开始使用免费开始使用

绘制自举回归线

可视化线性回归中可能出现的变异性,一个好方法是把每一次自举得到的斜率和截距所对应的直线都画出来。请对前 100 个斜率与截距的自举重复(保存在 bs_slope_repsbs_intercept_reps 中)绘制对应的回归直线。

本练习是课程的一部分

Python 统计思维(第 2 部分)

查看课程

练习说明

  • 生成用于绘制回归线的 \(x\) 数组,只包含 0100。请使用 np.array() 函数。
  • 编写一个 for 循环,按配对自举得到的斜率与截距绘制回归直线。共绘制 100 条线。
    • 在每次循环中绘制回归线时,回忆回归方程 y = a*x + b。这里 abs_slope_reps[i]bbs_intercept_reps[i]
    • 在调用 plt.plot() 时指定关键字参数 linewidth=0.5alpha=0.2color='red'
  • 绘制散点图,x 轴为 illiteracy,y 轴为 fertility。记得指定关键字参数 marker='.'linestyle='none'
  • 为坐标轴添加标签,设置 2% 边距,并显示图形。这些已为您完成,直接提交即可查看自举回归线的可视化!

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Generate array of x-values for bootstrap lines: x
x = ____

# Plot the bootstrap lines
for i in range(____):
    _ = plt.plot(____, 
                 ____*x + ____,
                 ____=0.5, ____=0.2, ____='red')

# Plot the data
_ = ____

# Label axes, set the margins, and show the plot
_ = plt.xlabel('illiteracy')
_ = plt.ylabel('fertility')
plt.margins(0.02)
plt.show()
编辑并运行代码