回归算法学习笔记——线性回归、随机梯度(SGD、BGD)、逻辑回归(牛顿法)、Softmax回归算法、L1/L2正则化、Ridge、Lasso、ElasticNet

线性回归

  • 回归分析
  • 目标函数:线性回归方程 y = w x + b y = wx + b y=wx+b
  • 一个或多个自变量和因变量之间的关系进行建模(其中 θ i \theta_i θi为权重, θ 0 \theta_0 θ0为bias偏置值):
    • 一维特征: h θ ( x ) = θ 0 + θ 1 x 1 h_\theta(x)=\theta_0+\theta_1x_1 hθ(x)=θ0+θ1x1
    • 二维特征: h θ ( x ) = θ 0 + θ 1 x 1 + θ 2 x 2 h_\theta(x)=\theta_0+\theta_1x_1+\theta_2x_2 hθ(x)=θ0+θ1x1+θ2x2
    • N维特征: ∑ i = 0 N θ i x i = θ T X \sum_{i=0}^N\theta_ix_i=\theta^TX i=0Nθixi=θTX

梯度下降算法

构建损失函数

  • 未得到目标线性方程,得到上述公式中的 θ T \theta^T θT参数
  • 为确定所选的 θ T \theta_T θT效果好坏使用损失函数(loss function)来评估 h ( x ) h(x) h(x)函数的好坏
  • 损失函数如下(MSE误差平方和):
    J ( θ ) = 1 2 m ∑ i = 1 m ( h ( θ T x i ) − y i ) 2 (1) J(\theta)=\frac{1}{2m}\sum_{i=1}^m(h(\theta^Tx_i)-y_i)^2 \tag{1} J(θ)=2m1i=1m(h(θTxi)yi)2(1)

梯度下降法

  • 使用了导数的概念,对点 x 0 x_0 x0的导数反映了函数在该点处的瞬时变化速率
    f ′ ( x 0 ) = lim ⁡ △ x → ∞ = △ y △ x = lim ⁡ △ x → ∞ = f ( x 0 + △ x ) − f ( x 0 ) △ x (2) f^\prime(x_0)=\lim_{\vartriangle x \to \infty} =\frac{\vartriangle y}{\vartriangle x}=\lim_{\vartriangle x \to \infty} =\frac{f(x_0+\vartriangle x)-f(x_0)}{\vartriangle x} \tag{2} f(x0)=xlim=xy=xlim=xf(x0+x)f(x0)(2)
  • 推广到多维函数中,梯度反映了多维图形中变化速率最快的方向
  • 起始时导数为正, θ \theta θ减小后并以新的 θ \theta θ为基点重新求导,一直迭代就会找到最小的 J ( θ ) J(\theta) J(θ)
    在这里插入图片描述
  • StochasticGradientDescent(SGD)随机梯度下降
    Repeat { ⋯ θ j = θ j − α ( ( h ( θ T x i ) − y i ) x i ) ⋯ } \text{Repeat} \{ \\ \dotsb\\ \theta_j = \theta_j -\alpha((h(\theta^Tx_i)-y_i)x_i)\\ \dotsb\\ \} Repeat{ θj=θjα((h(θTxi)yi)xi)}
    其中: x i x_i xi为随机的样本点
  • 代码实现
import random
import matplotlib.pyplot as plt

x = [(0.,3), (1.,3) ,(2.,3), (3.,2), (4.,4), (0.,3) , (1.,3.1) ,(2.,3.5), (3.,2.1) , (4.,4.2)]
#y[i] is the output of y = theta0 * x[0] + theta1 * x[1] +theta2 * x[2]
y = [95.364,97.217205,75.195834,60.105519,49.342380, 100.364,100.217205,100.195834,100.105519,12.342380]

epsilon = 0.001
#learning rate
alpha = 0.001
diff = [0,0]
error1 = 0
error0 =0
m = len(x)

#init the parameters to zero
theta0 = 0
theta1 = 0
theta2 = 0
epoch = 0

error_array = []
epoch_array = []
while True:
    #calculate the parameters
    # 线性回归:h(x) = theta0  + theta1 * x[i][0] + theta2 * x[i][1] 
    # 损失函数(评测指标MSE):累和 (1/2) *  (y - h(x)) ^ 2
    # d((1/2) *  (y - h(x)) ^ 2) / dtheta0 = (y - h(x)) * (-1) * (1) 
    # theta0 = theta0 - (-alpha * (y - h(x))* 1 )
    # theta1 = theta1 - (-alpha * (y - h(x))* x[i][0])
    # theta2 = theta2 - (-alpha * (y - h(x))* x[i][1])
    # 1. 随机梯度下降算法在迭代的时候,每迭代一个新的样本,就会更新一次所有的theta参数。
    i = random.randint(0, m - 1)

    # (y - h(x))
    diff[0] = y[i]-( theta0 * 1 + theta1 * x[i][0] + theta2 * x[i][1] )
    # - (y - h(x))x
    gradient0 = - diff[0]* 1
    gradient1 = - diff[0]* x[i][0]
    gradient2 = - diff[0]* x[i][1]
    # theta = theta - (  - alpha * (y - h(x))x )
    theta0 = theta0 - alpha * gradient0
    theta1 = theta1 - alpha * gradient1
    theta2 = theta2 - alpha * gradient2
    #theta3
    #calculate the cost function
    error1 = 0
    # 此处error为一个相对的Error值。
    for i in range(len(x)):
        error1 += (y[i]-( theta0 * 1 + theta1 * x[i][0] + theta2 * x[i][1]))**2 
    
    error1 = error1 / m    
    print("delta  error {}".format(abs(error1-error0)))
    error_array.append(error1)
    epoch_array.append(epoch)
    if epoch == 500:
        break
    else:
        error0 = error1
    epoch += 1
    print(' theta0 : %f, theta1 : %f, theta2 : %f, sgd error1 : %f, epoch : % f'%(theta0,theta1,theta2,error1, epoch))

print('Done: theta0 : %f, theta1 : %f, theta2 : %f'%(theta0,theta1,theta2))

plt.plot(epoch_array, error_array, color='blue', linewidth=3)
plt.xlabel('epochs')
plt.ylabel('errors')
plt.show()

在这里插入图片描述

  • BatchGradientDescent(BGD)批量随机梯度下降
    Repeat { ⋯ θ j = θ j − α ( 1 m ∑ i = 0 m ( h ( θ T x i ) − y i ) x i ) ⋯ } \text{Repeat} \{ \\ \dotsb\\ \theta_j = \theta_j -\alpha(\frac{1}{m}\sum_{i=0}^m(h(\theta^Tx_i)-y_i)x_i)\\ \dotsb\\ \} Repeat{ θj=θjα(m1i=0m(h(θTxi)yi)xi)}
  • 代码实现
import matplotlib.pyplot as plt

#Training data set
#each element in x represents (x0,x1,x2)
x = [(0.,3) , (1.,3) ,(2.,3), (3.,2) , (4.,4), (0.,3) , (1.,3.1) ,(2.,3.5), (3.,2.1) , 
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值