参见 textbook p4-12。完成以下任务: (1) 生成正弦序列 s(n); (2) 使用噪声函数对正弦序列加噪 x(n)=s(n)+w(n); (3) 使用多项式回归模型对 x(n)进行拟合,并分析过拟合和欠拟合情况
当M=3时:


当M=10时:


当M=20时:


1.Generate n = 2,000 points uniformly at random in the two-dimensional unit square. Which point do you expect the centroid to be? (0.5, 0.5) 2.What objective does the centroid of the points optimize? 应当让中心点与所有点的欧式距离最小
化为代码即为
def cost(c, all_points):
return sum(sum((c - all_points) ** 2, axis=1) ** 0.5)3.Apply gradient descent (GD) to find the centroid. 先根据损失函数, 写出求导公式
该公式会基于全部点的误差进行迭代。 每次迭代都计算全局误差和梯度,将梯度置于中心点,更新中心点。 重复迭代,直到更新步长小于某个定值为止。 具体见代码。

4.Apply stochastic gradient descent (SGD) to find the centroid. Can you say in simple words, what the algorithm is doing? 需要改变损失函数和迭代逻辑。

可见,使用SGD后,点的更新变得十分曲折。