LinearRegression
LinearRegression
简介
线性回归(linear regression)是线性模型的一种典型方法。
基本原理
给定一组输入\(x\)和输出\(y\)构成的数据集\(D = \{(x_1,y_1),...,(x_m,y_m) \}\),其中\(y_{i}\in \mathbb{R}\)。线性回归模型假设数据线性分布,因此想要学习一个线性模型使得预测值和真实值之间的误差最小。假设线性回归中输出和输入有如下的关系 \[ y = wx_i+b \] 利用均方误差最小化有: \[ \begin{aligned} (w^*,b^*)=\arg\min\sum_{i=1}^{m}(y-y_i)^2=\arg\min\sum_{i=1}^{m}(wx_i+b-y_i)^2 \end{aligned}\tag{1}\label{eq1} \] 从表达式\(\eqref{eq1}\)中可以求得参数\(w^*\)和\(b^*\)的解析解。 \[ \begin{aligned} \frac{\partial L(w, b)}{\partial w} & =\frac{\partial}{\partial w}\left[\sum_{i=1}^{m}\left(w x_{i}+b-y_{i}\right)^{2}\right] =\sum_{i=1}^{m} \frac{\partial}{\partial w}\left[\left(y_{i}-w x_{i}-b\right)^{2}\right] \\ & =\sum_{i=1}^{m}\left[2 \cdot\left(y_{i}-w x_{i}-b\right) \cdot\left(-x_{i}\right)\right] =\sum_{i=1}^{m}\left[2 \cdot\left(w x_{i}^{2}-y_{i} x_{i}+b x_{i}\right)\right] \\ & =2 \cdot\left(w \sum_{i=1}^{m} x_{i}^{2}-\sum_{i=1}^{m} y_{i} x_{i}+b \sum_{i=1}^{m} x_{i}\right) \end{aligned} \]
\[ \begin{aligned} \frac{\partial L(w, b)}{\partial b} & =\frac{\partial}{\partial b}\left[\sum_{i=1}^{m}\left(w x_{i}+b-y_{i}\right)^{2}\right] =\sum_{i=1}^{m} \frac{\partial}{\partial b}\left[\left(y_{i}-w x_{i}-b\right)^{2}\right] \\ & =\sum_{i=1}^{m}\left[2 \cdot\left(y_{i}-w x_{i}-b\right) \cdot(-1)\right] =\sum_{i=1}^{m}\left[2 \cdot\left(-y_{i}+w x_{i}+b\right)\right] \\ & =2 \cdot\left(-\sum_{i=1}^{m} y_{i}+\sum_{i=1}^{m} w x_{i}+\sum_{i=1}^{m} b\right) =2 \cdot\left(m b-\sum_{i=1}^{m}\left(y_{i}-w x_{i}\right)\right) \end{aligned} \] 最终得到权重\(w^*\)和偏置\(b^*\)的表达式 \[ \begin{aligned} w^{*}&=\frac{\sum_{i=1}^{m} y_{i}\left(x_{i}-\bar{x}\right)}{\sum_{i=1}^{m} x_{i}^{2}-\frac{1}{m}\left(\sum_{i=1}^{m} x_{i}\right)^{2}} \\ b^{*}&=\frac{1}{m} \sum_{i=1}^{m}\left(y_{i}-w x_{i}\right) \end{aligned} \] 形式\(\eqref{eq1}\)是向量形式的线性回归,在实际中通常利用矩阵化将线性回归表达为矩阵形式,首先将输入\(x\)拼接成矩阵形式: \[ \boldsymbol{X}=\left(\begin{array}{ccccc} x_{11} & x_{12} & \cdots & x_{1 d} & 1 \\ x_{21} & x_{22} & \cdots & x_{2 d} & 1 \\ \vdots & \vdots & \ddots & \vdots & \vdots \\ x_{m 1} & x_{m 2} & \cdots & x_{m d} & 1 \end{array}\right)=\left(\begin{array}{cc} x_{1}^{\mathrm{T}} & 1 \\ \vdots & \vdots \\ x_{m}^{\mathrm{T}} & 1 \end{array}\right) \] 将输出向量\(y\)相应拼接为\(\mathbf{y}=(y_1;y_2;...;y_m)\),最终可以得到优化目标函数的矩阵化表达形式: \[ w^*=\arg\min(\mathbf{y}-\mathbf{X}\hat{w}^T)(\mathbf{y}-\mathbf{X}\hat{w}) \] 对权重矩阵和偏置矩阵求偏导数可得: \[ \begin{aligned} \frac{\partial L}{\partial w}&=-\mathbf{X}^T\mathbf{y}-\mathbf{X}^T\mathbf{y}+(\mathbf{X^TX}+\mathbf{X^TX})\hat{w}\\ \frac{\partial L}{\partial b}&=2\mathbf{X}^T(\mathbf{X}\hat{w}-\mathbf{y}) \end{aligned} \] 根据梯度可以直接求得解析解: \[ \hat{w}=(\mathbf{X^{T}X})^{-1} \mathbf{X}^{T}\mathbf{y} \]
代码实现
基于numpy的算法实现
1 |
|
基于sklearn的线性回归模型
1 |
|