机器学习- 一元线性回归

线性回归(一元):

① 、一元线性回归是分析只有一个自变量线性相关关系的方法,此处可以理解为:线性回归法是一种求最佳拟合线 y=a+bx 的数学方法(也叫“最小二乘回归法”)。

②、误差平方和(SSE): 该名称定义为“各个(y的实际值与 通过最佳拟合线得出的y的预测值 的差值)²的和“

SSE为最小时,便是最小拟合线。

过程推导:

假设有函数     h_{\Theta }(x)=\Theta _{0}+\Theta _{1}x   (初始对二者随机赋值)

损失函数:   J(\Theta _{0},\Theta _{1})=\frac{1}{2m}\sum _{_{}^{}\textrm{i=1}}^{m}(h_{\Theta }(x^{(i)})-y^{(i)})^{2}

y^{(i)}是样本点的实际值,h_{\Theta }(x^{(i)})是预测值

然后利用梯度下降求出\Theta _{0} \Theta _{1} 为何值时损失函数 J(\Theta _{0},\Theta_{1} ) 的值最小

分别对  \Theta _{0} 和 \Theta _{1} 求偏导:

\frac{\partial }{\partial \Theta _{0}}J(\Theta _{0},\Theta _{1})=\frac{1}{m}\sum _{i=1}^{m}\textrm{}(h_{\Theta }(x)^{(i)}-y^{(i)})

\frac{\partial }{\partial \Theta _{1}}J(\Theta _{0},\Theta _{1})=\frac{1}{m}\sum _{i=1}^{m}\textrm{}(h_{\Theta }(x)^{(i)}-y^{(i)})*x^{(i)}

迭代:

\Theta _{0}=\Theta _{0}-\alpha *\frac{\partial }{\partial \Theta _{0}}J(\Theta _{0},\Theta _{1})=\Theta _{0}-\alpha *\frac{1}{m}\sum _{i=1}^{m}\textrm{}(h_{\Theta }(x)^{(i)}-y^{(i)})

\Theta _{1}=\Theta _{1}-\alpha *\frac{\partial }{\partial \Theta _{1}}J(\Theta _{0},\Theta _{1})=\Theta _{1}-\alpha *\frac{1}{m}\sum _{i=1}^{m}\textrm{}(h_{\Theta }(x)^{(i)}-y^{(i)})*x^{(i)}

代入J(\Theta _{0},\Theta _{1})=\frac{1}{2m}\sum _{_{}^{}\textrm{i=1}}^{m}(h_{\Theta }(x^{(i)})-y^{(i)})^{2}求出每次迭代后的损失函数的值

不断迭代直到损失函数的值小于所规定的\varepsilon,将此时的\Theta _{0}\Theta _{1} 代入

h_{\Theta }(x)=\Theta _{0}+\Theta _{1}x

即可得到所求直线。

例题示例:

假设我们的数据集如下:

xy
12
24
36

4

8
510

我们随机初始化 $\Theta_{0}=0$$\Theta_{1}=0$,学习率 $\alpha=0.01$,目标是让模型以最小化平方误差的方式来预测未知房屋的价格。

步骤 1:计算当前模型的预测值和实际值之间的误差。

$J(\Theta _{0},\Theta _{1}) = \frac{1}{2m}\sum_{i=1}^{m}(h_{\Theta}(x^{(i)}) - y^{(i)})^{2}$

将模型中的参数$\Theta_0$$\Theta_1$带入到 $h_\Theta(x)$中,得到:

$J(\Theta _{0},\Theta _{1}) = \frac{1}{2m}\sum_{i=1}^{m}(\Theta_0 + \Theta_1x^{(i)} - y^{(i)})^{2}$

将样本数据代入上式,得到误差函数:

$\begin{aligned} J(\Theta _{0},\Theta _{1}) &= \frac{1}{2*5}\big[(\Theta_0 + \Theta_1\cdot1 - 2)^2 + (\Theta_0 + \Theta_1\cdot2 - 4)^2 + (\Theta_0 + \Theta_1\cdot3 - 6)^2 \\ &\quad + (\Theta_0 + \Theta_1\cdot4 - 8)^2 + (\Theta_0 + \Theta_1\cdot5 - 10)^2\big]\\ &= \frac{1}{10}[(\Theta_0 + \Theta_1 - 2)^2 + (\Theta_0 + 2\Theta_1 - 4)^2 + (\Theta_0 + 3\Theta_1 - 6)^2 \\ &\quad + (\Theta_0 + 4\Theta_1 - 8)^2 + (\Theta_0 + 5\Theta_1 - 10)^2] \end{aligned}$

步骤2:计算梯度

利用误差函数求导的公式,我们可以得到:

$\begin{aligned} J(\Theta _{0},\Theta _{1}) &= \frac{1}{10}\big[2(\Theta_0 + \Theta_1 - 2) + 2(\Theta_0 + 2\Theta_1 - 4) + 2(\Theta_0 + 3\Theta_1 - 6) \\ &\quad + 2(\Theta_0 + 4\Theta_1 - 8) + 2(\Theta_0 + 5\Theta_1 - 10)\big] \\ &= \frac{1}{5}\sum_{i=1}^{m}(\Theta_0 + \Theta_1x^{(i)} - y^{(i)}) \end{aligned}$

$\begin{aligned}J(\Theta _{0},\Theta _{1}) &= \frac{1}{10}\big[2(\Theta_0 + \Theta_1 - 2)\cdot1 + 2(\Theta_0 + 2\Theta_1 - 4)\cdot2 + 2(\Theta_0 + 3\Theta_1 - 6)\cdot3 \\ &\quad + 2(\Theta_0 + 4\Theta_1 - 8)\cdot4 + 2(\Theta_0 + 5\Theta_1 - 10)\cdot5\big] \\ &= \frac{1}{5}\sum_{i=1}^{m}(\Theta_0 + \Theta_1x^{(i)} - y^{(i)})x^{(i)} \end{aligned}$

步骤3:使用梯度更新参数

我们根据梯度下降的公式来更新参数:

$\begin{aligned} \Theta_{0} &= \Theta_{0} - \alpha *\frac{1}{5}\sum_{i=1}^{5}(\Theta_0 + \Theta_1x^{(i)} - y^{(i)}) \\ \Theta_{1} &= \Theta_{1} - \alpha*\frac{1}{5}\sum_{i=1}^{5}(\Theta_0 + \Theta_1x^{(i)} - y^{(i)})x^{(i)} \end{aligned}$

根据上述公式,我们可以进行参数的更新:

$\begin{aligned} \Theta_{0} &= \Theta_{0} - \alpha*\frac{1}{5}\big[(0+0*1-2)+(0+0*2-4)+(0+0*3-6) \\ &\quad +(0+0*4-8)+(0+0*5-10)\big] \\ &= \Theta_{0} - 0.01*\frac{1}{5}\big[-2-4-6-8-10\big] \\ &= 0.02 \\ \Theta_{1} &= \Theta_{1} - \alpha*\frac{1}{5}\big[(0+0*1-2)\cdot1+(0+0*2-4)*2+(0+0*3-6)*3 \\ &\quad +(0+0*4-8)*4+(0+0*5-10)*5\big] \\ &= \Theta_{1} - 0.01*\frac{1}{5}\big[-2*1-4*2-6*3-8*4-10*5\big] \\ &= 0.4 \end{aligned}$

因此,本例子在第一轮迭代之后,更新后的参数值为 $\Theta_{0} = 0.02$$\Theta_{1} = 0.4$

继续迭代直到达到所规定的终止条件。

代码:

import numpy as np
import matplotlib.pyplot as plt


# 初始化参数
theta_0 = 0
theta_1 = 0
alpha = 0.01
num = 1000 # 迭代次数
X = [1, 2, 3, 4, 5]
Y = [2, 4, 6, 8, 10]


# 损失函数
def J(theta_0, theta_1, X, Y):
    error = 0
    m = len(X)
    for i in range(m):
        error += (theta_0 + theta_1 * X[i] - Y[i]) ** 2
    return error / (2 * m)


# 梯度函数
def gradient_descent(theta_0, theta_1, X, Y, alpha, num):
    m = float(len(X)) # m用作除数
    for i in range(num):
        sum_theta_0 = 0
        sum_theta_1 = 0
        for j in range(len(X)):
            sum_theta_0 += theta_0 + theta_1*X[j] - Y[j]
            sum_theta_1 += (theta_0 + theta_1*X[j] - Y[j])*X[j]
        theta_0 = theta_0 - alpha * sum_theta_0 / m
        theta_1 = theta_1 - alpha * sum_theta_1 / m
    return theta_0, theta_1


# 运行梯度下降算法并打印结果
theta_0, theta_1 = gradient_descent(theta_0, theta_1, X, Y, alpha, num)
print("theta_0 = ", theta_0)
print("theta_1 = ", theta_1)


# 可视化训练数据和拟合直线
plt.scatter(X, Y, color='r')
plt.plot(X, [theta_0 + theta_1*x for x in X])
plt.xlabel('X')
plt.ylabel('Y')
plt.show()

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值