开始使用免费开始使用

最小化残差

在本练习中,您将完善一个函数,用于直观对比模型与数据,并计算和打印 RSS。您将多次调用该函数,观察当您更改 a0a1 的取值时,RSS 如何变化。我们会看到,之前求得的参数值正是用于将 RSS 最小化 的那一组。

本练习是课程的一部分

Python 线性建模入门

查看课程

练习说明

  • 补全对 model() 的调用,传入数据 xd 和模型参数 a0a1
  • rss 计算为 residuals 的平方和。
  • 使用 compute_rss_and_plot_fit() 尝试多组 a0a1 的取值,查看它们如何改变 RSS。
  • 请确认最初的取值 a0=150a1=25 能最小化 RSS,并以此提交您的答案。

交互式实操练习

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

# Complete function to load data, build model, compute RSS, and plot
def compute_rss_and_plot_fit(a0, a1):
    xd, yd = load_data()
    ym = model(xd, ____, ____)
    residuals = ym - yd
    rss = np.sum(np.square(____))
    summary = "Parameters a0={}, a1={} yield RSS={:0.2f}".format(____, ____, rss)
    fig = plot_data_with_model(xd, yd, ym, summary)
    return rss, summary

# Chose model parameter values and pass them into RSS function
rss, summary = compute_rss_and_plot_fit(a0=____, a1=____)
print(summary)
编辑并运行代码