线性回归的原理及Python实现

ml20170502 2023-06-11 15:43:39

提到线性回归相信大家应该都不会觉得陌生(不陌生你点进来干嘛[捂脸]),本文就线性回归的基本原理进行讲解,并手把手、肩并肩地带您实现这一算法。

完整实现代码请参考本人的p...哦不是...github: regression_base.py linear_regression.py linear_regression_example.py

1. 原理篇

我们用人话而不是大段的数学公式来讲讲线性回归是怎么一回事。

1.1 线性方程组

上小学或者中学的时候,很多人就接触过线性方程组了。举个栗子,如果x + y = 2且2x + y = 3,那么3x + 4y = ?。我们可以轻松地得出结论,解线性方程组得到x = 1且y = 1,所以3x + 4y = 3 + 4 = 7。

1.2 超定方程组

对于方程组Ra=y,R为n×m矩阵,如果R列满秩,且n>m。则方程组没有精确解,此时称方程组为超定方程组。翻译成人话就是方程组里方程的个数n太多了,比要求解的变量数m还多,这个方程是没办法求出精确解的。比如x + y = 2, 2x + y = 3且x + 2y = 4,那么我们是无法求出x和y能够同时满足这三个等式的。

1.3 线性回归问题

1.4 最小二乘法

如何求解这个超定方程组呢?当当当当,最小二乘法闪亮登场了。假设n个同事有m个特征(年龄、职级等),收集这些特征组成m行n列的矩阵X,同事的工资为m行1列的矩阵Y,且满足m > n。我们要求解n个未知数W = [w1, w2...wn]和1个未知数b,满足方程组W * X + b = Y。 令预测值为 \hat Y ,那么有 MSE = \large\frac{1}{m}\normalsize\sum_{1}^{m}(Y_{i} - \hat Y_{i})^2

当我们的预测值完全等于真实值的时候,MSE等于0。根据上面的讲解,显然我们不太可能找到满足方程的精确解W,也就不可能准确地预测出Y,所以MSE不可能为0。但是我们想办法找出方程的近似解让MSE最小,这就是最小二乘法。

1.5 求近似解

如何求让MSE为零的近似解W呢?前方小段数学公式低能预警。

1. 使用MSE作为损失函数L L = \large\frac{1}{m}\normalsize\sum_{1}^{m}(Y_{i} - \hat Y_{i})^2 2. 已知 \hat Y=WX + b

3. 对w求偏导,得 \large\frac{\mathrm{d}L}{\mathrm{d}W}\normalsize= -\large\frac{2}{m}\normalsize\sum_{1}^{m}(Y_{i} - WX_{i} - b)X_{i}

4. 对b求偏导,得 \large\frac{\mathrm{d}L}{\mathrm{d}b}\normalsize= -\large\frac{2}{m}\normalsize\sum_{1}^{m}(Y_{i} - WX_{i} - b)

所以,参数W的梯度就是式3,参数b的梯度就是式4。

1.6 梯度下降法

请参考我的另一篇文章,在这里就不赘述。链接如下:

1.7 批量梯度下降

遍历数据集中所有的样本,计算梯度并更新参数,记做1个epoch。经过若干个epochs之后,算法收敛或终止,计算量较大。

1.8 随机梯度下降

使用数据集中随机的一个样本,计算梯度并更新参数,直至算法收敛或终止,计算量较小。

2. 实现篇

本人用全宇宙最简单的编程语言——Python实现了线性回归算法,没有依赖任何第三方库,便于学习和使用。简单说明一下实现过程,更详细的注释请参考本人github上的代码。

2.1 创建RegressionBase类

class RegressionBase(object):    def __init__(self):        self.bias = None        self.weights = None

每一个HTML文档中,都有一个不可或缺的标签:<head>,在几乎所有的HTML文档里, 我们都可以看到类似下面这段代码:

html{color:#000;overflow-y:scroll;overflow:-moz-scrollbars}
body,button,input,select,textarea{font-size:12px;font-family:Arial,sans-serif}
h1,h2,h3,h4,h5,h6{font-size:100%}
em{font-style:normal}
small{font-size:12px}
ol,ul{list-style:none}
a{text-decoration:none}
a:hover{text-decoration:underline}
legend{color:#000}
fieldset,img{border:0}
button,input,select,textarea{font-size:100%}
table{border-collapse:collapse;border-spacing:0}
img{-ms-interpolation-mode:bicubic}
textarea{resize:vertical}
.left{float:left}
.right{float:right}
.overflow{overflow:hidden}
.hide{display:none}
.block{display:block}
.inline{display:inline}
.error{color:red;font-size:12px}
button,label{cursor:pointer}
.clearfix:after{content:'\20';display:block;height:0;clear:both}
.clearfix{zoom:1}
.clear{clear:both;height:0;line-height:0;font-size:0;visibility:hidden;overflow:hidden}
.wordwrap{word-break:break-all;word-wrap:break-word}
.s-yahei{font-family:arial,'Microsoft Yahei','微软雅黑'}
pre.wordwrap{white-space:pre-wrap}
body{text-align:center;background:#fff;width:100%}
body,form{position:relative;z-index:0}
td{text-align:left}
img{border:0}
#s_wrap{position:relative;z-index:0;min-width:1000px}
#wrapper{height:100%}
#head .s-ps-islite{_padding-bottom:370px}
#head_wrapper.s-ps-islite{padding-bottom:370px}#head_wrapper.s-ps-islite #s_lm_wrap{bottom:298px;background:0 0!important;filter:none!important}#head_wrapper.s-ps-islite .s_form{position:relative;z-index:1}#head_wrapper.s-ps-islite .fm{position:absolute;bottom:0}#head_wrapper.s-ps-islite .s-p-top{position:absolute;bottom:40px;width:100%;height:181px}#head_wrapper.s-ps-islite #s_lg_img,#head_wrapper.s-ps-islite#s_lg_img_aging,#head_wrapper.s-ps-islite #s_lg_img_new{position:static;margin:33px auto 0 auto}.s_lm_hide{display:none!important}#head_wrapper.s-down #s_lm_wrap{display:none}.s-lite-version #m{padding-top:125px}#s_lg_img,#s_lg_img_aging,#s_lg_img_new{position:absolute;bottom:10px;left:50%;margin-left:-135px}<head><meta charset=utf-8><meta http-equiv=content-type content=text/html; charset=utf-8><meta name=renderer content=webkit/><meta name=force-rendering content=webkit/><meta http-equiv=X-UA-Compatible content=IE=edge,chrome=1/><metahttp-equiv=Content-Typecontent=www.czjy.cn;charset=gb2312><meta name=viewport content=width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no></head>.s-ps-sug table{width:100%;background:#fff;cursor:default}.s-ps-sug td{color:#000;font:14px arial;height:25px;line-height:25px;padding:0 8px}.s-ps-sug td b{color:#000}.s-ps-sug .mo{background:#ebebeb;cursor:pointer}.s-ps-sug .ml{background:#fff}.s-ps-sug td.sug_storage{color:#7a77c8}.s-ps-sug td.sug_storage b{color:#7a77c8}.s-ps-sug .sug_del{font-size:12px;color:#666;text-decoration:underline;float:right;cursor:pointer;display:none}.s-ps-sug .sug_del{font-size:12px;color:#666;text-decoration:underline;float:right;cursor:pointer;display:none}.s-ps-sug .mo .sug_del{display:block}
.s-ps-sug .sug_ala{border-bottom:1px solid #e6e6e6}

head标签作为一个容器,主要包含了用于描述 HTML 文档自身信息(元数据)的标签,这些标签一般不会在页面中被显示出来。

...全文
66 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

199

社区成员

发帖
与我相关
我的任务
社区描述
GAE相关讨论
社区管理员
  • GAE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧