最小二乘法与曲线拟合

通过最小二乘法算法实现多项式拟合的公式推导见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()

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值