用多项式拟合sin
1)数据产生
import numpy as np def real_func(x): return np.sin(2*np.pi*x) x=np.linspace(0,1,10) y0=real_func(x) y1=[np.random.normal(0,0.1)+y for y in y0]#构造含噪数据点 print(y1)
#
[-0.057796679658847487, 0.40735932912281247, 1.0817688020699658, 0.86746776941749437, 0.32993597854579781, -0.4914894089922538, -0.76762888306902965, -1.1451046976989734, -0.81624475047423251, 0.076217949103347865]
2)拟合程序
用上一篇的程序
3)不同n的拟合效果
n=2
n=5
n=10
从中可以看出n=2时欠拟合,n=5时拟合效果较好,n=10时过拟合;