python一元线性回归算法,代码及参数更新动图展示

python一元线性回归算法,代码及参数更新动图展示,第1张

python一元线性回归算法,代码及参数更新动图展示 (一元)线性回归算法 假设函数和代价函数

1.设已知数据集为(X,Y),假设其线性关系为:
y _ p r e d = h θ ( x ) = θ 0 + θ 1 × x y_pred=h_{theta}left( x right) =theta _0+theta _1times x y_pred=hθ​(x)=θ0​+θ1​×x

2.用均平方误差(MSE)作为代价函数(损失函数),其中m为样本数:
J ( x , y , θ ) ( M S E ) = 1 2 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) 2 ^{J(x,y,theta )(MSE)=frac{1}{2m}sum_{i=1}^m{left( h_{theta}left( x^{left( i right)} right) -y^{^{left( i right)}} right) ^2}} J(x,y,θ)(MSE)=2m1​∑i=1m​(hθ​(x(i))−y(i))2

梯度下降算法更新公式

根据梯度下降算法,对θ求偏导数,其偏导为:
∂ J ∂ θ j = 1 m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) frac{partial J}{partial theta _j}=frac{1}{m}sum_{i=1}^m{left( h_{theta}left( x^{left( i right)} right) -y^{left( i right)} right)}{x_j}^{left( i right)} ∂θj​∂J​=m1​i=1∑m​(hθ​(x(i))−y(i))xj​(i)

所以其参数更新公式为:
θ j = θ j − α ∂ J ∂ θ j = θ j − α m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x j ( i ) theta _j=theta _j-alpha frac{partial J}{partial theta _j}=theta _j-frac{alpha}{m}sum_{i=1}^m{left( h_{theta}left( x^{left( i right)} right) -y^{left( i right)} right)}{x_j}^{left( i right)} θj​=θj​−α∂θj​∂J​=θj​−mα​i=1∑m​(hθ​(x(i))−y(i))xj​(i)

假设函数可以写成:
h θ ( x ) = θ 0 × x 0 + θ 1 × x 1 = θ 0 × 1 + θ 1 × x 1 h_{theta}left( x right) =theta _0times x_0+theta _1times x_1=theta _0times 1+theta _1times x_1 hθ​(x)=θ0​×x0​+θ1​×x1​=θ0​×1+θ1​×x1​

所以
θ 0 = θ 0 − α ∂ J ∂ θ 0 = θ 0 − α m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) x 0 ( i ) = θ 0 − α m ∑ i = 1 m ( h θ ( x ( i ) ) − y ( i ) ) theta _0=theta _0-alpha frac{partial J}{partial theta _0}=theta _0-frac{alpha}{m}sum_{i=1}^m{left( h_{theta}left( x^{left( i right)} right) -y^{left( i right)} right)}{x_0}^{left( i right)} \ =theta _0-frac{alpha}{m}sum_{i=1}^m{left( h_{theta}left( x^{left( i right)} right) -y^{left( i right)} right)} θ0​=θ0​−α∂θ0​∂J​=θ0​−mα​i=1∑m​(hθ​(x(i))−y(i))x0​(i)=θ0​−mα​i=1∑m​(hθ​(x(i))−y(i))
至此,参数迭代公式完成。

代码
import matplotlib.pyplot as plt
import numpy as np
from sklearn.datasets import make_regression  ##产生一元线性回归数据
m=80   ##80份数据
##产生一元线性回归数据
x_train,y_train=make_regression(n_samples=m,n_features=1,noise=30,bias=20,random_state=17)
theat=np.array([0,0]) #初始化参数
lr=0.003 #学习率
def Y_pred(x,a) #假设函数:
    return x*a[0]+a[1]
def partial_theat(x,y,a):##求偏导
    cost_all=[0,0]
    for i in range(m):
        cost_all[0]=cost_all[0]+(Y_pred(x[i],a)-y[i])*x[i]
        cost_all[1]=cost_all[1]+(Y_pred(x[i],a)-y[i])
    return cost_all
def J(x,y,a):#损失或代价函数
    cost=0
    for i in range(m):
        cost=cost+(Y_pred(x[i],a)-y[i])**2
    return (1/2*m)*cost
iterations=0##迭代次数,初始化为0
theat_list=np.array([0,0])
while(iterations<100): ##参数更新100次
    theat[0]=theat[0]-lr*partial_theat(x_train,y_train,theat)[0]
    theat[1]=theat[1]-lr*partial_theat(x_train,y_train,theat)[1]
    theat_list=np.vstack((theat_list,theat))
    iterations=iterations+1
theat_list=np.array(theat_list)##保留参数更新过程

from matplotlib.animation import FuncAnimation #导入动画图接口
import matplotlib.pyplot as plt 
fig,ax=plt.subplots() #画布申请
##--------初始状态--------------
atext_anti=plt.text(2,2,'',fontsize=15)
btext_anti=plt.text(2,40,'',fontsize=15)
ln,=plt.plot([],[],'red')
#--------------初始状态--------------------
def init():
    ax.set_xlim(np.min(x_train),np.max(x_train))
    ax.set_ylim(np.min(y_train),np.max(y_train))
    return ln,
def upgrad(frame):
    x=x_train
    y=frame[0]*x+frame[1]
    ln.set_data(x,y)
    atext_anti.set_text('a=%.3f'%frame[0])
    btext_anti.set_text('b=%.3f'%frame[1])
    return ln,
  #--------静态图----------------
ax.scatter(x_train,y_train)
#---------静态图-------------
ani=FuncAnimation(fig,upgrad,frames=theat_list,init_func=init)
plt.show()
参数更新可视化

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5073780.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-16
下一篇 2022-11-16

发表评论

登录后才能评论

评论列表(0条)

保存