通过最小二乘法算法实现多项式拟合的公式推导见http://blog.youkuaiyun.com/jairuschan/article/details/7517773/
对于给定样本空间D[(x1,y1),(x2,y2)...(xn,yn)],求解
权值向量W,使得 Xi*W=Yi.根据最小二乘法,W=(X'X)-1X'Y
,其中X是每个样本Xi构成的范德蒙矩阵的转置,(X'X)-1是X的伪逆。
下面使用Python实现
import matplotlib.pyplot as plt
import math
import numpy
import random
fig = plt.figure()
ax = fig.add_subplot(111)
order=10
#create training examples
x = numpy.arange(-1,1,0.02)
y = [((a*a-1)*(a*a-1)*(a*a-1)+0.5)*numpy.cos(a*2) for a in x]
#insert noise
i=0
xa=[]
ya=[]
for xx in x:
yy=y[i]
d=float(random.randint(60,140))/100
i+=1
xa.append(xx*d)
ya.append(yy*d)
plt.plot(xa,ya,color='m',linestyle='',marker='.')
#A
lis = [numpy.power(xa,i) for i in range(order)]
matX_T = numpy.array(lis).reshape(order,100)
matX = matX_T.transpose()
matY= numpy.array(ya).reshape(100,1)
A = numpy.linalg.solve(numpy.dot(matX_T,matX), numpy.dot(matX_T,matY)).reshape(order,1)
#new sample, get hypothesis
xxa = numpy.linspace(-1,1,100)
lis = [numpy.power(xxa,i) for i in range(order)]
X_T = numpy.array(lis).reshape(order,100)
X = X_T.transpose()
Y = numpy.dot(X,A)
#show
ax.plot(xxa,Y,color='g',linestyle='-',marker='')
plt.show()