IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 线性回归-从理论到实践 -> 正文阅读

[数据结构与算法]线性回归-从理论到实践

一元一次线性回归

抽象问题

现有一组数据,共有两列,分别为x和y,如下所示

在这里插入图片描述

现将数据用python作散点图,观察其变化趋势

import pandas as pd 
import numpy as np
import matplotlib.pyplot as plt

# 读取数据并转为np.array的格式
train = pd.read_csv('click.csv').values 
# 读取所有行的第0列作为训练数据的x
train_x = train[:, 0] 
# 读取所有行的第1列作为训练数据的y
train_y = train[:, 1]
# 画散点图
plt.plot(train_x, train_y, 'o')
plt.show()

在这里插入图片描述

我们的问题是要找到x和y的对应关系,根据此对应关系,我们就可以在给定任意x的情况下,计算得到y的值。

定义模型

观察上面的散点图可以看出来,随着x的增大,y值也在近似的不断增大。据此,我们推测,这组数据可能符合一次函数的关系,即: y ?? = ?? k x ?? + ?? b y\;=\;kx\;+\;b y=kx+b?

此式可改写为: f θ ( x ) ?? = ?? θ 0 ?? + ?? θ 1 ? x f_\theta(x)\;=\;\theta_0\;+\;\theta_1\cdot x fθ?(x)=θ0?+θ1??x?

其中 f θ ( x ) f_\theta(x) fθ?(x) 表示含有参数 θ θ θ ,与变量 x x x相关的函数。该函数即为:一元一次线性回归模型。

:统计学中,常用 θ θ θ 来表示未知数和推测值,采用 θ θ θ???? 加数字下标的形式,是为了防止当未知数的数量增加时,表达式中大量出现a、b、c、d…这样的符号。这样不但不易理解,还容易出现符号本身不够用的情况。

只要求得了上述公式中的未知参数 θ 0 \theta_0 θ0? θ 1 \theta_1 θ1?,即知道了x和y的关系。

现在问题转化为求未知参数 θ 0 \theta_0 θ0? θ 1 \theta_1 θ1?

求解方法

已知1个自变量 x ( i ) x^{(i)} x(i)????,根据模型公式 f θ ( x ) ?? = ?? θ 0 ?? + ?? θ 1 ? x f_\theta(x)\;=\;\theta_0\;+\;\theta_1\cdot x fθ?(x)=θ0?+θ1??x???,可以求得与其对应的因变量 f θ ( x ( i ) ) f_\theta(x^{(i)}) fθ?(x(i))????。

那么 y ( i ) ? f θ ( x ( i ) ) y^{(i)} - f_\theta(x^{(i)}) y(i)?fθ?(x(i))?表示实际值与模型预测值之间的误差。

我们要求解的模型函数最理想的情况就是使得 f θ ( x ( i ) ) = y ( i ) f_\theta(x^{(i)}) = y^{(i)} fθ?(x(i))=y(i),即误差值 y ( i ) ? f θ ( x ( i ) ) = 0 y^{(i)} - f_\theta(x^{(i)})= 0 y(i)?fθ?(x(i))=0?。

实际中,很难做到让所有点的误差都等于0,因此,我们要做的就是让所有点的误差之和尽可能的小。

我们一般使用误差的平方来表征误差,一方面是去除误差正负抵消的影响,一方面是为了使之后求解微分更加方便。

所有点的误差的平方和可以用如下公式来表示:

E ( θ ) ?? = ?? 1 2 ∑ i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) 2 E(\theta)\;=\;\frac12\sum_{i=1}^n{(y^{(i)}-f_\theta(x^{(i)}))}^2 E(θ)=21?i=1n?(y(i)?fθ?(x(i)))2

这个表达式称为目标函数 E E E????是Error的首字母。

现在,我们的问题转化为:求出使得目标函数 E ( θ ) E(\theta) E(θ)取最小值的参数 θ 0 \theta_0 θ0? θ 1 \theta_1 θ1?

梯度下降法(最速下降法)

现有函数 g ( x ) g(x) g(x)根据导数的意义:

  1. d d x g ( x ) > 0 \frac d{dx}g(x)>0 dxd?g(x)>0????,?即导数为正 g ( x ) g(x) g(x)随x的增大而增大,因此,要使得 g ( x ) g(x) g(x)减小,需要x负移
  2. d d x g ( x ) < 0 \frac d{dx}g(x)<0 dxd?g(x)<0??,即导数为负 g ( x ) g(x) g(x)随x的增大而减小,因此,要使得 g ( x ) g(x) g(x)减小,需要x正移

据此可知,要使得 g ( x ) g(x) g(x)不断减小,只需要将x向与导数符号相反的方向移动即可。用数学表达式表述为:

x ?? : = ?? x ? η d d x g ( x ) x\;:=\;x-\eta\frac d{dx}g(x) x:=x?ηdxd?g(x)

  1. A : = B A :=B A:=B,表示通过B来定义A
  2. η η η??,表示学习率
    1. η η η越小,更新次数越多,速度越慢
    2. η η η越大,更新次数越少,速度越快,但有可能导致不收敛。

梯度下降法的核心:根据导数的符号来决定x移动的方向。

根据梯度下降法的原理,我们知道了怎样更新参数 θ 0 \theta_0 θ0? θ 1 \theta_1 θ1?,就能使目标函数 E ( θ ) E(θ) E(θ)?的值达到最小。

参数更新方法如下

{ θ 0 ?? : = ?? θ 0 ?? ? ?? η ? ? θ 0 E ( θ ) θ 1 ?? : = ?? θ 1 ?? ? ?? η ? ? θ 1 E ( θ ) \left\{\begin{array}{l}\theta_0\;:=\;\theta_0\;-\;\eta\frac\partial{\partial\theta_0}E(\theta)\\\theta_1\;:=\;\theta_1\;-\;\eta\frac\partial{\partial\theta_1}E(\theta)\end{array}\right. {θ0?:=θ0??η?θ0???E(θ)θ1?:=θ1??η?θ1???E(θ)?

现在只要计算出 ? ? θ 0 E ( θ ) \frac\partial{\partial\theta_0}E(\theta) ?θ0???E(θ)?和 ? ? θ 1 E ( θ ) \frac\partial{\partial\theta_1}E(\theta) ?θ1???E(θ)?,代入上式,就可不断更新参数,求得最佳的 θ 0 \theta_0 θ0??和 θ 1 \theta_1 θ1??。

计算上式中的两个偏导数过程如下:

? ? θ 0 E ( θ ) ?? = ?? 1 2 ∑ ?? i = 1 n 2 ? ( y ( i ) ? f θ ( x ( i ) ) ) ? ? ( y ( i ) ? f θ ( x ( i ) ) ) ? θ 0 \frac\partial{\partial\theta_0}E(\theta)\;=\;\frac12\overset n{\underset{i=1}{\sum\;}}2\cdot(y^{(i)}-f_\theta(x^{(i)}))\cdot\frac{\partial(y^{(i)}-f_\theta(x^{(i)}))}{\partial\theta_0} ?θ0???E(θ)=21?i=1?n?2?(y(i)?fθ?(x(i)))??θ0??(y(i)?fθ?(x(i)))?
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? ? ( f θ ( x ( i ) ) ) ? θ 0 =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot\frac{\partial(f_\theta(x^{(i)}))}{\partial\theta_0} =i=1?n?(y(i)?fθ?(x(i)))?(?1)??θ0??(fθ?(x(i)))???
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? ? ( θ 0 ?? + ?? θ 1 ? x ( i ) ) ? θ 0 =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot\frac{\partial(\theta_0\;+\;\theta_1\cdot x^{(i)})}{\partial\theta_0} =i=1?n?(y(i)?fθ?(x(i)))?(?1)??θ0??(θ0?+θ1??x(i))??
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? 1 =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot1 =i=1?n?(y(i)?fθ?(x(i)))?(?1)?1
= ∑ ?? i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) =\overset n{\underset{i=1}{\sum\;}}(f_\theta(x^{(i)})-y^{(i)}) =i=1?n?(fθ?(x(i))?y(i))

同理:

? ? θ 1 E ( θ ) ?? = ?? 1 2 ∑ ?? i = 1 n 2 ? ( y ( i ) ? f θ ( x ( i ) ) ) ? ? ( y ( i ) ? f θ ( x ( i ) ) ) ? θ 1 \frac\partial{\partial\theta_1}E(\theta)\;=\;\frac12\overset n{\underset{i=1}{\sum\;}}2\cdot(y^{(i)}-f_\theta(x^{(i)}))\cdot\frac{\partial(y^{(i)}-f_\theta(x^{(i)}))}{\partial\theta_1} ?θ1???E(θ)=21?i=1?n?2?(y(i)?fθ?(x(i)))??θ1??(y(i)?fθ?(x(i)))??
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? ? ( f θ ( x ( i ) ) ) ? θ 1 =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot\frac{\partial(f_\theta(x^{(i)}))}{\partial\theta_1} =i=1?n?(y(i)?fθ?(x(i)))?(?1)??θ1??(fθ?(x(i)))????
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? ? ( θ 0 ?? + ?? θ 1 ? x ( i ) ) ? θ 1 =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot\frac{\partial(\theta_0\;+\;\theta_1\cdot x^{(i)})}{\partial\theta_1} =i=1?n?(y(i)?fθ?(x(i)))?(?1)??θ1??(θ0?+θ1??x(i))???
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? x ( i ) =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot x^{(i)} =i=1?n?(y(i)?fθ?(x(i)))?(?1)?x(i)
= ∑ ?? i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x ( i ) =\overset n{\underset{i=1}{\sum\;}}(f_\theta(x^{(i)})-y^{(i)})\cdot x^{(i)} =i=1?n?(fθ?(x(i))?y(i))?x(i)

因此,参数更新表达式如下:

{ θ 0 ?? : = ?? θ 0 ?? ? ?? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) θ 1 ?? : = ?? θ 1 ?? ? ?? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x ( i ) \left\{\begin{array}{l}\theta_0\;:=\;\theta_0\;-\;\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\\\theta_1\;:=\;\theta_1\;-\;\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x^{(i)}\end{array}\right. {θ0?:=θ0??η?i=1n?(fθ?(x(i))?y(i))θ1?:=θ1??η?i=1n?(fθ?(x(i))?y(i))?x(i)?

根据该式,不断迭代,直到求得最佳的参数 θ 0 \theta_0 θ0? θ 1 \theta_1 θ1?,即确定了模型表达式 f θ ( x ) ?? = ?? θ 0 ?? + ?? θ 1 ? x f_\theta(x)\;=\;\theta_0\;+\;\theta_1\cdot x fθ?(x)=θ0?+θ1??x

python实现

根据上述的理论及推导,我们知道:

  1. 模型表达式为: f θ ( x ) ?? = ?? θ 0 ?? + ?? θ 1 ? x f_\theta(x)\;=\;\theta_0\;+\;\theta_1\cdot x fθ?(x)=θ0?+θ1??x
  2. 目标函数为: E ( θ ) ?? = ?? 1 2 ∑ i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) 2 E(\theta)\;=\;\frac12\sum_{i=1}^n{(y^{(i)}-f_\theta(x^{(i)}))}^2 E(θ)=21?i=1n?(y(i)?fθ?(x(i)))2
  3. 参数更新表达式为: { θ 0 ?? : = ?? θ 0 ?? ? ?? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) θ 1 ?? : = ?? θ 1 ?? ? ?? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x ( i ) \left\{\begin{array}{l}\theta_0\;:=\;\theta_0\;-\;\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\\\theta_1\;:=\;\theta_1\;-\;\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x^{(i)}\end{array}\right. {θ0?:=θ0??η?i=1n?(fθ?(x(i))?y(i))θ1?:=θ1??η?i=1n?(fθ?(x(i))?y(i))?x(i)?

在进行参数更新之前,应该将数据标准化(也称z-score规范化)。这个步骤非必须,但是做了之后,参数的收敛会更快

z ( i ) = x ( i ) ? μ σ z^{(i)}=\frac{x^{(i)}-\mu}\sigma z(i)=σx(i)?μ?

# 读取数据并转为np.array的格式
train = pd.read_csv('click.csv').values 
train_x = train[:, 0] 
train_y = train[:, 1]

# 对变量train_x进行标准化
mu = train_x.mean()
sigma = train_x.std()
def standardize(x):
    return (x-mu) / sigma
train_x_std = standardize(train_x)

python逐步实现

# 1,随机生成初始参数theta0 和 theta1
theta0, theta1 = np.random.rand(2)

# 2,定义模型函数f(x)
def f(x):
    y = theta0 + theta1 * x
    return y

# 3,定义目标函数E(x, y)
def E(x, y):
    e = 0.5 * np.sum((y-f(x)) ** 2)
    return e 

# 4,初始化学习率η
ETA = 0.001

# 5,初始化误差变化量diff
diff = 1

# 6,初始化更新次数count
count = 0 

# 7,迭代学习
error = E(train_x_std, train_y)    # 计算初始误差
# 开始迭代更新参数,直到误差的变化小于0.01
while diff > 0.01:
    # 更新参数theta0和theta1
    theta0 = theta0 - ETA * np.sum((f(train_x_std) - train_y))
    theta1 = theta1 - ETA * np.sum((f(train_x_std) - train_y) * train_x_std)
    # 计算更新参数后的当前误差
    error_current = E(train_x_std, train_y)
    # 计算原误差与当前误差的差值
    diff = error - error_current
    # 更新误差值
    error = error_current
    # 输出日志
    count += 1
    print(f'第{count}次:theta0 = {theta0:.3f}, theta1 = {theta1:.3f}, 差值 = {diff:.4f}')

部分输出日志如下

在这里插入图片描述

注:若执行多次,会发现迭代次数和误差的差值在每次执行时都不一样,这是因为随机初始化的参数 θ 0 \theta_0 θ0? θ 1 \theta_1 θ1??的不同而导致的。

此时,最终参数为:

print(f'theta0 = {theta0:.3f}')
print(f'theta1 = {theta1:.3f}')

在这里插入图片描述
作图查看拟合结果如下

plt.plot(train_x_std, train_y, 'o')     # 标准化后散点图
x = np.linspace(-3, 3, 100)
plt.plot(x, f(x))                       # 拟合直线
plt.show()

在这里插入图片描述

sklearn方法

在实际使用中,机器学习库sklearn为我们提供了更模块化的方式来进行线性回归,如下所示

from sklearn.linear_model import LinearRegression
# 1, 定义线性回归模型
lr = LinearRegression()
# 2, 训练模型
lr.fit(train_x_std.reshape(-1,1), train_y)

# 查看tehta0 和 theta1
print(f'theta0 = {lr.intercept_}')
print(f'theta1 = {lr.coef_}')

结果如下所示

在这里插入图片描述

和使用python逐步计算得到的参数一致。

np.polyfit(X, y, n)方法

Numpy库也提供了线性拟合方法:Numpy.polyfit(X, y, n),使用方法如下所示:

# 一次线性拟合
z1 = np.polyfit(train_x_std, train_y, 1)
# 线性表示
p1 = np.poly1d(z1)
print(z1)
print(p1)

结果如下所示

在这里插入图片描述

和使用前两种方法得到的结果一致。

一元多次线性回归-多项式回归

观察前面做出的图像,发现一元一次线性模型 f θ ( x ) ?? = ?? θ 0 ?? + ?? θ 1 ? x f_\theta(x)\;=\;\theta_0\;+\;\theta_1\cdot x fθ?(x)=θ0?+θ1??x?的拟合效果并不好。

实际上,对于给定的数据,曲线比直线拟合地更好。因此,我们重新定义模型函数为: f θ ( x ) ?? = ?? θ 0 + ?? θ 1 ? x + θ 2 ? x 2 f_\theta(x)\;=\;\theta_0+\;\theta_1\cdot x+\theta_2\cdot x^2 fθ?(x)=θ0?+θ1??x+θ2??x2?
该式为一元多次线性回归,即多项式拟合模型表达式

如果使用更大次数的表达式,如下所示,就能表示更复杂的曲线了

f θ ( x ) ?? = ?? θ 0 + ?? θ 1 ? x + θ 2 ? x 2 + θ 3 ? x 3 + ? + θ n ? x n f_\theta(x)\;=\;\theta_0+\;\theta_1\cdot x+\theta_2\cdot x^2+\theta_3\cdot x^3+\dots+\theta_n\cdot x^n fθ?(x)=θ0?+θ1??x+θ2??x2+θ3??x3+?+θn??xn

次数越高,拟合的越好,但可能会出现过拟合问题

对于要解决的问题,在找出合适的表达式之前,需要不断地去尝试。

这里以二次拟合函数为例,我们增加了参数 θ 2 θ_2 θ2?,此时:

  1. 模型表达式为: f θ ( x ) ?? = ?? θ 0 + ?? θ 1 ? x + θ 2 ? x 2 f_\theta(x)\;=\;\theta_0+\;\theta_1\cdot x+\theta_2\cdot x^2 fθ?(x)=θ0?+θ1??x+θ2??x2??
  2. 目标函数为: E ( θ ) ?? = ?? 1 2 ∑ i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) 2 E(\theta)\;=\;\frac12\sum_{i=1}^n{(y^{(i)}-f_\theta(x^{(i)}))}^2 E(θ)=21?i=1n?(y(i)?fθ?(x(i)))2?

使用同样的方式,对 E ( θ ) E(θ) E(θ)进行微分,得到参数更新表达式如下:

{ θ 0 ?? : = ?? θ 0 ?? ? ?? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) θ 1 ?? : = ?? θ 1 ?? ? ?? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x ( i ) θ 2 ?? : = ?? θ 2 ?? ? ?? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x ( i ) 2 \left\{\begin{array}{l}\theta_0\;:=\;\theta_0\;-\;\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\\\theta_1\;:=\;\theta_1\;-\;\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x^{(i)}\\\theta_2\;:=\;\theta_2\;-\;\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x^{(i)^2}\end{array}\right. ????θ0?:=θ0??η?i=1n?(fθ?(x(i))?y(i))θ1?:=θ1??η?i=1n?(fθ?(x(i))?y(i))?x(i)θ2?:=θ2??η?i=1n?(fθ?(x(i))?y(i))?x(i)2?

即使增加参数(增加次数),比如有 θ 3 θ_3 θ3???, θ 4 θ_4 θ4??等,依然可以用同样的方法来求出参数更新表达式。

像这样增加函数中多项式的次数,然后再使用函数的分析方法被称为多项式回归

多元线性回归

前述的线性回归都是一元回归,即只有一个变量。

但是,实际中要解决的很多问题是变量超过2个的复杂问题。

例如:有三个变量,分别为 x 1 x_1 x1??, x 2 x_2 x2??, x 3 x_3 x3????。此时:

模型表达式为: f θ ( x 1 , x 2 , x 3 ) = ?? θ 0 + ?? θ 1 ? x 1 + θ 2 ? x 2 + θ 3 ? x 3 f_\theta(x_1,x_2,x_3)=\;\theta_0+\;\theta_1\cdot x_1+\theta_2\cdot x_2+\theta_3\cdot x_3 fθ?(x1?,x2?,x3?)=θ0?+θ1??x1?+θ2??x2?+θ3??x3?

将此式推广到n个变量的情况,此时

模型表达式为: f θ ( x 1 , … , x n ) = ?? θ 0 + ?? θ 1 ? x 1 + θ 2 ? x 2 + ? + θ n ? x n f_\theta(x_1,\dots,x_n)=\;\theta_0+\;\theta_1\cdot x_1+\theta_2\cdot x_2+\dots+\theta_n\cdot x_n fθ?(x1?,,xn?)=θ0?+θ1??x1?+θ2??x2?+?+θn??xn?

我们可以把参数 θ θ θ和变量 x x x看作向量,用黑体表示

θ = [ θ 0 θ 1 θ 2 ? θ n ] , ?? x = [ x 0 x 1 x 2 ? x n ] \theta=\begin{bmatrix}\theta_0\\\theta_1\\\theta_2\\\vdots\\\theta_n\end{bmatrix},\;x=\begin{bmatrix}\begin{array}{c}x_0\\x_1\\x_2\end{array}\\\vdots\\x_n\end{bmatrix} θ=????????θ0?θ1?θ2??θn??????????,x=????????x0?x1?x2???xn??????????

其中 x 0 = 1 x_0 = 1 x0?=1

则: θ T x ?? = ?? θ 0 ? x 0 + ?? θ 1 ? x 1 + θ 2 ? x 2 + ? + θ n ? x n \theta^Tx\;=\;\theta_0\cdot x_0+\;\theta_1\cdot x_1+\theta_2\cdot x_2+\dots+\theta_n\cdot x_n θTx=θ0??x0?+θ1??x1?+θ2??x2?+?+θn??xn??

因此: f θ ( x ) = θ T x f_\theta(x)=\theta^Tx fθ?(x)=θTx

对目标函数 E ( θ ) ?? = ?? 1 2 ∑ i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) 2 E(\theta)\;=\;\frac12\sum_{i=1}^n{(y^{(i)}-f_\theta(x^{(i)}))}^2 E(θ)=21?i=1n?(y(i)?fθ?(x(i)))2 θ j θ_j θj? 的偏导如下:

? ? θ j E ( θ ) ?? = ?? 1 2 ∑ ?? i = 1 n 2 ? ( y ( i ) ? f θ ( x ( i ) ) ) ? ? ( y ( i ) ? f θ ( x ( i ) ) ) ? θ j \frac\partial{\partial\theta_j}E(\theta)\;=\;\frac12\overset n{\underset{i=1}{\sum\;}}2\cdot(y^{(i)}-f_\theta(x^{(i)}))\cdot\frac{\partial(y^{(i)}-f_\theta(x^{(i)}))}{\partial\theta_j} ?θj???E(θ)=21?i=1?n?2?(y(i)?fθ?(x(i)))??θj??(y(i)?fθ?(x(i)))???

= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? ? ( f θ ( x ( i ) ) ) ? θ j =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot\frac{\partial(f_\theta(x^{(i)}))}{\partial\theta_j} =i=1?n?(y(i)?fθ?(x(i)))?(?1)??θj??(fθ?(x(i)))?????
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? ? ( θ 0 ? x 0 ( i ) + θ 1 ? x 1 ( i ) + θ 2 ? x 2 ( i ) + ? + θ n ? x n ( i ) ) ? θ j =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot\frac{\partial(\theta_0\cdot x_0^{(i)}+\theta_1\cdot x_1^{(i)}+\theta_2\cdot x_2^{(i)}+\dots+\theta_n\cdot x_n^{(i)})}{\partial\theta_j} =i=1?n?(y(i)?fθ?(x(i)))?(?1)??θj??(θ0??x0(i)?+θ1??x1(i)?+θ2??x2(i)?+?+θn??xn(i)?)??
= ∑ ?? i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) ? ( ? 1 ) ? x j ( i ) =\overset n{\underset{i=1}{\sum\;}}(y^{(i)}-f_\theta(x^{(i)}))\cdot(-1)\cdot x_j^{(i)} =i=1?n?(y(i)?fθ?(x(i)))?(?1)?xj(i)?
= ∑ ?? i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x j ( i ) =\overset n{\underset{i=1}{\sum\;}}(f_\theta(x^{(i)})-y^{(i)})\cdot x_j^{(i)} =i=1?n?(fθ?(x(i))?y(i))?xj(i)?
综上:

  1. 模型表达式为:

    f θ ( x ) = θ T x ?? = ?? θ 0 ? x 0 + ?? θ 1 ? x 1 + θ 2 ? x 2 + ? + θ n ? x n f_\theta(x)=\boldsymbol\theta^{\mathbf T}\boldsymbol x\;=\;\theta_0\cdot x_0+\;\theta_1\cdot x_1+\theta_2\cdot x_2+\dots+\theta_n\cdot x_n fθ?(x)=θTx=θ0??x0?+θ1??x1?+θ2??x2?+?+θn??xn?

  2. 目标函数为: E ( θ ) ?? = ?? 1 2 ∑ i = 1 n ( y ( i ) ? f θ ( x ( i ) ) ) 2 E(\theta)\;=\;\frac12\sum_{i=1}^n{(y^{(i)}-f_\theta(x^{(i)}))}^2 E(θ)=21?i=1n?(y(i)?fθ?(x(i)))2

  3. 参数更新表达式为: θ j : = θ j ? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x j ( i ) \theta_j:=\theta_j-\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x_j^{(i)} θj?:=θj??η?i=1n?(fθ?(x(i))?y(i))?xj(i)?

像这样包含了多个变量的回归称为多重回归。

采用矩阵求解参数

由于训练集数据有很多,所以我们把1行数据当作一个训练数据,以矩阵的形式来处理更方便。

n个变量( x 0 x_0 x0??????, x 1 x_1 x1??????, x 2 x_2 x2??????,…, x n x_n xn??????),n个参数( θ 0 θ_0 θ0??????, θ 1 θ_1 θ1??????, θ 2 θ_2 θ2??????,…, θ n θ_n θn????????),n个训练数据的模型表达式如下:

f θ ( x ) = X ? θ = [ x 0 x 1 ???? x 2 ?? ? ???? x n ] ? [ θ 0 θ 1 ? θ n ] = [ x 0 ( 1 ) x 1 ( 1 ) ? x n ( 1 ) x 0 ( 2 ) x 1 ( 2 ) ? x n ( 2 ) x 0 ( 3 ) x 1 ( 3 ) ? x n ( 3 ) ? x 0 ( n ) x 1 ( n ) ? x n ( n ) ] ? [ θ 0 θ 1 ? θ n ] f_\theta(x)=X\cdot\theta=\begin{bmatrix}{\mathbf x}_{\mathbf0}&{\mathbf x}_{\mathbf1}\;\;{\mathbf x}_{\mathbf2}\;\cdots\;\;{\mathbf x}_{\mathbf n}\end{bmatrix}\boldsymbol\cdot\begin{bmatrix}\theta_0\\\theta_1\\\vdots\\\theta_n\end{bmatrix}=\begin{bmatrix}x_0^{(1)}&x_1^{(1)}&\cdots&x_n^{(1)}\\x_0^{(2)}&x_1^{(2)}&\cdots&x_n^{(2)}\\x_0^{(3)}&x_1^{(3)}&\cdots&x_n^{(3)}\\&&\vdots&\\x_0^{(n)}&x_1^{(n)}&\cdots&x_n^{(n)}\end{bmatrix}\cdot\begin{bmatrix}\theta_0\\\theta_1\\\vdots\\\theta_n\end{bmatrix} fθ?(x)=X?θ=[x0??x1?x2??xn??]???????θ0?θ1??θn????????=?????????x0(1)?x0(2)?x0(3)?x0(n)??x1(1)?x1(2)?x1(3)?x1(n)????????xn(1)?xn(2)?xn(3)?xn(n)??????????????????θ0?θ1??θn????????

? = [ θ 0 ? x 0 ( 1 ) + θ 1 ? x 1 ( 1 ) + ? + θ n ? x n ( 1 ) θ 0 ? x 0 ( 2 ) + θ 1 ? x 1 ( 2 ) + ? + θ n ? x n ( 2 ) θ 0 ? x 0 ( 3 ) + θ 1 ? x 1 ( 3 ) + ? + θ n ? x n ( 3 ) ? θ 0 ? x 0 ( n ) + θ 1 ? x 1 ( n ) + ? + θ n ? x n ( n ) ] =\begin{bmatrix}\theta_0\cdot x_0^{(1)}+\theta_1\cdot x_1^{(1)}+\cdots+\theta_n\cdot x_n^{(1)}\\\theta_0\cdot x_0^{(2)}+\theta_1\cdot x_1^{(2)}+\cdots+\theta_n\cdot x_n^{(2)}\\\theta_0\cdot x_0^{(3)}+\theta_1\cdot x_1^{(3)}+\cdots+\theta_n\cdot x_n^{(3)}\\\vdots\\\theta_0\cdot x_0^{(n)}+\theta_1\cdot x_1^{(n)}+\cdots+\theta_n\cdot x_n^{(n)}\end{bmatrix} =?????????θ0??x0(1)?+θ1??x1(1)?+?+θn??xn(1)?θ0??x0(2)?+θ1??x1(2)?+?+θn??xn(2)?θ0??x0(3)?+θ1??x1(3)?+?+θn??xn(3)??θ0??x0(n)?+θ1??x1(n)?+?+θn??xn(n)???????????

对于参数更新表达式: θ j : = θ j ? η ? ∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x j ( i ) \theta_j:=\theta_j-\eta\cdot\sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x_j^{(i)} θj?:=θj??η?i=1n?(fθ?(x(i))?y(i))?xj(i)?,当 j = 0 j=0 j=0时,求和项可展开为:

∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x 0 ( i ) = ( f θ ( x ( 1 ) ) ? y ( 1 ) ) ? x 0 ( 1 ) + ( f θ ( x ( 2 ) ) ? y ( 2 ) ) ? x 0 ( 2 ) + ? + ( f θ ( x ( n ) ) ? y ( n ) ) ? x 0 ( n ) \sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x_0^{(i)}=(f_\theta(x^{(1)})-y^{(1)})\cdot x_0^{(1)}+(f_\theta(x^{(2)})-y^{(2)})\cdot x_0^{(2)}+\cdots+(f_\theta(x^{(n)})-y^{(n)})\cdot x_0^{(n)} i=1n?(fθ?(x(i))?y(i))?x0(i)?=(fθ?(x(1))?y(1))?x0(1)?+(fθ?(x(2))?y(2))?x0(2)?+?+(fθ?(x(n))?y(n))?x0(n)?

令:

f = [ f θ ( x ( 1 ) ) ? y ( 1 ) f θ ( x ( 2 ) ) ? y ( 2 ) ? f θ ( x ( n ) ) ? y ( n ) ] , ?? x 0 = [ x 0 ( 1 ) x 0 ( 2 ) ? x 0 ( n ) ] \boldsymbol f=\begin{bmatrix}f_\theta(x^{(1)})-y^{(1)}\\f_\theta(x^{(2)})-y^{(2)}\\\vdots\\f_\theta(x^{(n)})-y^{(n)}\end{bmatrix},\;{\boldsymbol x}_{\mathbf0}=\begin{bmatrix}x_0^{(1)}\\x_0^{(2)}\\\vdots\\x_0^{(n)}\end{bmatrix} f=??????fθ?(x(1))?y(1)fθ?(x(2))?y(2)?fθ?(x(n))?y(n)???????,x0?=???????x0(1)?x0(2)??x0(n)?????????

则:

∑ i = 1 n ( f θ ( x ( i ) ) ? y ( i ) ) ? x 0 ( i ) = f T ? x 0 \sum_{i=1}^n(f_\theta(x^{(i)})-y^{(i)})\cdot x_0^{(i)}=\boldsymbol f^T\cdot{\boldsymbol x}_{\mathbf0} i=1n?(fθ?(x(i))?y(i))?x0(i)?=fT?x0?

Python逐步实现

对于多项式回归:

x 0 = [ 1 1 ? 1 ] , ?? x 1 = [ x ( i ) x ( i ) ? x ( i ) ] , ?? x 2 = [ x ( i ) 2 x ( i ) 2 ? x ( i ) 2 ] , ?? X = [ x 0 ?? x 1 ?? x 2 ] = [ 1 x ( i ) x ( i ) 2 1 x ( i ) x ( i ) 2 ? 1 x ( i ) x ( i ) 2 ] x_0=\begin{bmatrix}1\\1\\\vdots\\1\end{bmatrix},\;x_1=\begin{bmatrix}x^{(i)}\\x^{(i)}\\\vdots\\x^{(i)}\end{bmatrix},\;x_2=\begin{bmatrix}x^{(i)^2}\\x^{(i)^2}\\\vdots\\x^{(i)^2}\end{bmatrix},\;X=\lbrack{\boldsymbol x}_{\mathbf0}\;{x}_{\mathbf1}\;{\boldsymbol x}_{\mathbf2}\rbrack=\begin{bmatrix}1&x^{(i)}&x^{(i)^2}\\1&x^{(i)}&x^{(i)^2}\\&\vdots&\\1&x^{(i)}&x^{(i)^2}\end{bmatrix} x0?=??????11?1???????,x1?=??????x(i)x(i)?x(i)???????,x2?=???????x(i)2x(i)2?x(i)2????????,X=[x0?x1?x2?]=???????111?x(i)x(i)?x(i)?x(i)2x(i)2x(i)2????????

# 1,创建训练数据的矩阵X
def to_matrix(x):
    return np.vstack([np.ones(x.shape[0]), x, x**2]).T
X = to_matrix(train_x_std)

# 2,随机初始化参数Theta
theta = np.random.rand(3)

# 3,定义预测函数
def f(x):
    return np.dot(x, theta)

# 4, 初始化参数
diff = 1
ETA = 0.001

# 5, 迭代更新参数
error = E(X, train_y)
while diff > 0.01:
    theta = theta - ETA * np.dot(f(X)-train_y, X)
    current_error = E(X, train_y)
    diff = error - current_error
    error = current_error

运行结束后,得到参数如下

在这里插入图片描述

将结果绘图展示如下:

x = np.linspace(-3, 3, 100)

plt.plot(train_x_std, train_y, 'o') 	# 原数据散点图
plt.plot(x, f(to_matrix(x)))             # 多项式拟合曲线

plt.show()

在这里插入图片描述

np.polyfit(X, y, n)方法

z2 = np.polyfit(train_x_std, train_y, 2)
p2 = np.poly1d(z2)
print(z2)
print(p2)

结果如下所示
在这里插入图片描述
与python逐步实现结果一致

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-07-29 11:53:53  更:2021-07-29 11:56:05 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/5 15:20:49-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码