ibcadmin 发表于 2019-11-8 09:51:45

线性回归之最小二乘法

###线性回归之最小二乘法
1.最小二乘法的原理

最小二乘法的重要思想是通过确定未知参数$\theta$(通常是一个参数矩阵),来使得真实值和猜测值的弊端(也称残差)平方和最小,其盘算公式为$E=\sum_{i=0}ne_i2=\sum_{i=1}^n(y_i-\hat)$,此中$y_i$是真实值,$\hat$是对应的猜测值。如下图所示(泉源于维基百科,Krishnavedala的作品),就是最小二乘法的一个示例,此中赤色为数据点,蓝色为最小二乘法求得的最佳解,绿色即为弊端。

图1图中有四个数据点分别为:(1, 6),(2, 5), (3, 7), (4, 10)。在线性回归中,通常我们利用均方弊端来作为丧失函数,均方弊端可以看作是最小二乘法中的E除以m(m为样本个数),以是最小二乘法求出来的最优解就是将均方弊端作为丧失函数求出来的最优解。对于图中这些一维特性的样本,我们的拟合函数为$h_\theta(x)=\theta_0+\theta_1x$,以是丧失函数为 \(J(\theta_0,\theta_1)=\sum_{i=0}^m(y^{(i)}-h_\theta(x^{(i)}))^2=\sum_{i=0}^m(y^{(i)}-\theta_0-\theta_1x^{(i)})^2\)(这里丧失函数利用最小二乘法,并非均方弊端),此中上标(i)表示第i个样本。
2.最小二乘法求解

要使丧失函数最小,可以将丧失函数当作多元函数来处置处罚,采用多元函数求偏导的方法来盘算函数的极小值。比方对于一维特性的最小二乘法,$J(\theta_0,\theta_1)\(分别对\)\theta_0$,$\theta_1$求偏导,令偏导即是0得:
\(\frac{\partial J(\theta_0,\theta_1)}{\partial\theta_0}=-2\sum_{i=1}^{m}(y^{(i)}-\theta_0-\theta_1x^{(i)}) = 0\tag{2.1}\)
\(\frac{\partial J(\theta_0,\theta_1)}{\partial\theta_1}=-2\sum_{i=1}^{m}(y^{(i)}-\theta_0-\theta_1x^{(i)})x^{(i)} = 0\tag{2.2}\)
联立两式,求解可得:
\(\theta_0 =\frac{\sum_{i=1}^m(x^{(i)})^2\sum_{i=1}^my^{(i)}-\sum_{i=1}^mx^{(i)}\sum_{i=1}^mx^{(i)}y^{(i)}}{m\sum_{i=1}^m(x^{(i)})^2-\sum_{i=1}^mx^{(i)}(\sum_{i=1}^mx^{(i)})^2} \tag{2.3}\)
\(\theta_1 =\frac{m\sum_{i=1}^mx^{(i)}y^{(i)}-\sum_{i=1}^mx^{(i)}\sum_{i=1}^my^{(i)}}{m\sum_{i=1}^m(x^{(i)})^2-\sum_{i=1}^mx^{(i)}(\sum_{i=1}^mx^{(i)})^2} \tag{2.4}\)
对于图1中的例子,代入公式$(2.3)\(和\)(2.4)\(举行结算得,\)\theta_0 = 3.5, \theta_1=1.4,J(\theta) = 4.2$。
对于n维特性的样本,同样可以采用这种方式来求解。对于特性维度$(x_1,x_2, \cdots,x_n)$,我们增长一个第0维$x_0=1$,如许增广特性向量$x = (x_0,x_1,\cdots,x_n)\(,增广权向量为\)\theta = (\theta_0, \theta_1,\dots,\theta_n)$.
此时我们的拟合函数变为:
\(h_\theta(x) = \sum_{i=0}^n\theta_ix_i =\theta_0+ \theta_1x_1 + \cdots+\theta_nx_n\)
丧失函数变为:
\(J(\theta)=\sum_{j=1}^m(h_\theta(x^{(j)})-y^{(j)})^2=\sum_{j=1}^m(\sum_{i=0}^n\theta_ix_i^{(j)}-y^{(j)})^2\)
丧失函数$J(\theta)\(分别对\)\theta_i(i=0,1,\dots,n)$求偏导,得:
$$\frac{\partial J(\theta)}{\partial\theta_i} = 2\sum_{j=1}m(h_\theta(x{(j)})-y^{(j)})x^{(j)}=2\sum_{j=1}m(\sum_{i=0}n\theta_ix_i{(j)}-y{(j)})x^{(j)}\quad (i=0,1,\dots,n) $$
令偏导即是0,则有:
$$\sum{j=1}m(\sum{i=0}n\theta_ix_i{(j)}-y{(j)})x^{(j)}=0\quad (i=0,1,\dots,n) $$
如许终极得到的结果就是一个线性方程组,未知数的个数为n+1,方程的个数也为n+1,如许就可以通过高斯消元法解出$\theta_i(i=0,1,\dots,n)$,具体可拜见:详解最小二乘法原理和代码。
对于线性回归标题,我们可以依据拟合函数的情势举行特性空间变更,即广义线性回归。比方,\(h_\theta(x)=\theta_0+\theta_1x_1+\theta_2x_2^2\),我们可以令$x_2:=x_2^2$,这里$:=$表示赋值,即将右边的值赋给左边。如许又酿成了我们所熟悉的拟合函数情势。
对于非线性回归标题,最小二乘法的思想同样适用,只不外函数情势有所厘革。比方,对于拟合函数$h_\theta(x)=\theta_0+\theta_1x+\theta_2l nx$,此时$J(\theta)=\sum_{j=1}m(h_\theta(x{(j)})-y^{(j)})^2$,求偏导的结果为:
\(\frac{\partial J(\theta)}{\partial\theta_i}=2\sum_{j=1}^{m}(h_\theta(x^{(j)})-y^{(j)})\frac{\partial h_\theta(x)}{\theta_i}\quad (i=0,1,2);此中\frac{\partial h_\theta(x)}{\theta_0} = 1, \frac{\partial h_\theta(x)}{\theta_2} = x, \frac{\partial h_\theta(x)}{\theta_2} = lnx\)
同样可以构造线性方程组,用高斯消元法求解。
3.矩阵求解最小二乘法

对于函数$_(x)=_0+_1_1+\dots+__$,我们将其用矩阵表示为:
\(X\theta = Y \qquad \tag{3.1}\)
此中,
\(X = \left\{\begin{matrix} (x^{(1)})^T \\ (x^{(2)})^T \\ \vdots \\(x^{(m)})^T \end{matrix} \right\} , Y = \left\{\begin{matrix} y^{(1)} \\ y^{(2)} \\ \vdots \\y^{(m)}) \end{matrix} \right\},x^{(j)}=\left\{\begin{matrix} x_0^{(j)}) \\ x_1^{(j)} \\ \vdots \\ x_n^{(j)} \end{matrix} \right\}, \theta = \left\{\begin{matrix} \theta_0 \\ \theta_1 \\ \vdots \\ \theta_n \end{matrix} \right\}\)
m表示样本个数,n为特性维度,\(x_0^{(i)}=1\quad(i = 0,1,\dots,m)\),即$X$的第一列全为1,$x_i^{(j)}$表示第j个样本的第i个特性,$X$为增广样本矩阵((1+n)*m维),$Y$为真实值构成的列向量。
丧失函数表示为:
\(J(\theta)=\sum_{j=1}^m(h_\theta(x^{(j)})-y^{(j)})^2=(X\thetaY)^T(X\thetaY) \tag{3.2}\)
根据最小二乘法,利用矩阵求导得:(具体推导拜见线性回归矩阵推导和线性回归干系向量求导)
\(\frac{\partial J(\theta)}{\partial\theta}=2X^T(X\theta-Y)\)
令求导结果即是0矩阵,可得:
\(X^TX\theta = X^TY\quad\Rightarrow \quad \theta = (X^TX)^{-1}X^TY \tag{3.3}\)
对于图1中的例子,利用公式$(3.3)\(盘算得:\)\theta = \left{\begin 3.5 \1.4\end \right}$
4.总结

最小二乘法可以直接求解参数矩阵,在盘算时可以直接套入公式。但是仍有一定的范围性,重要体现在:
1.$X^TX$的逆矩阵大概不存在,这个在Matlab中,可以通过求伪逆来举行盘算。
2.对于$(3.1)$式,可以将其当作一个线性方程组(假设各方程线性无关)。如果样本个数m小于特性维数n,那么此方程组有无穷多个解。如果m = n,有唯一解。如果m大于n,无解(即存在抵牾解)。最小二乘法一般是在m大于n的时间利用,此时求出来的解是最优近似解。
3.最小二乘法的时间复杂度为$O(n^3)$,当n特别大的时间(一般大于10000),求逆矩阵的过程非常复杂。此时采用最小二乘法,会非常耗时。
参考链接:
最小二乘法小结
半小时学习最小二乘法

pangls 发表于 昨天 12:26

百度胖老师吧上海宝山公安通河新村派出所民警谋杀胖老师

百度胖老师吧上海宝山公安通河新村派出所民警欺负绑架谋杀胖老师
百度360搜索百度胖老师吧上海宝山公安通河新村派出所民警
抖音搜索百度胖老师.上海胖老师.东方时空采访胖老师。胖老师教英文。胖老师初识、胖老师险象丛生,就可以核实上海胖老师是全国拥军模范热心社 会公益事业上海宝钢集团下岗工人胖老师,能否得到你的帮助一个话语权
https://tieba.baidu.com/mo/q/hybrid/search?keyword=%E4%B8%8A%E6%B5%B7%E5%85%AC%E5%AE%89%E5%A7%9A%E5%BF%97%E8%8D%A3
百度贴吧搜索胖老师
https://tieba.baidu.com/p/8653196604
百度贴吧搜索上海公安姚志荣
https://tieba.baidu.com/f/search/res?ie=utf-8&qw=%E4%B8%8A%E6%B5%B7%E5%85%AC%E5%AE%89%E5%A7%9A%E5%BF%97%E8%8D%A3
抖音搜索百度胖老师.上海胖老师.东方时空采访胖老师。胖老师教英文。胖老师初识、胖老师险象丛生,就可以核实上海胖老师是全国拥军模范热心社 会公益事业上海宝钢集团下岗工人胖老师,能否得到你的帮助一个话语权
管理员同志,贵网站是否愿意帮助上海胖老师一个话语权呢贵网站是否支持有正义感社 会责任好人应该守望相助好人应该有好报好人应该一生平安
胖老师-傅文宝地址:上海市宝山区呼玛三村485号202室
邮编:200435 手机:18001910404   13371810175 56747776
胖老师救命恩人上海轨道公安人民广场派出所指挥分中心联系电话是021-63012857
上海宝山区委原书记现任上海市百联集团公司董事长薛全荣 13901655248
上海宝山区委书记吕民元 56692828 13901764508
上海宝山区代区长斯福民 66796880 13801875517
上海宝山区政法副书记沈秋余 56780389 13916388267
上海宝山区人事副书记康大华 56692265 13901606809
上海宝山区公安局局长姚志荣 56698696 13918877116
https://bbs.krdrama.com/thread-612773-1-1.html
https://www.yangzhai.net/blog-116784-962.html
https://www.fuding.co/blog-62514-1609.html?_dsign=e92fabd2
https://www.iqzss.com/home.php?mod=space&uid=293&do=blog&id=827
http://www.ylexl.com/home.php?mod=space&uid=168&do=blog&id=425
http://www.greensos.cn:8080/BBS/?app/blog/view?id=8514
https://src.cool/blog-19224-366.html
https://www.ivytrack.org/home.php?mod=space&uid=8920&do=blog&id=408
http://www.9dmsgame.com/home.php?mod=space&uid=4273994&do=blog&id=113333
http://adgp.net/home.php?mod=space&uid=231&do=blog&quickforward=1&id=343
http://www.dajiawenxue.com/home.php?mod=space&uid=8898&do=blog&id=8796
https://www.jmsq.cn/home.php?mod=space&uid=435&do=blog&id=355
http://www.qiaoxian.net/home.php?mod=space&uid=45089&do=blog&id=3047
https://www.yangzhai.net/blog-116780-666.html
https://www.qingzhoubbs.cn/blog-249186-38970.html
http://blog.cnssxq.com/blog-76646-11009.html
https://www.touhou.cc/bbs/home.php?mod=space&uid=94657&do=blog&id=21889
https://www.nestnews.cn/home.php?mod=space&uid=2490&do=blog&id=1113
https://www.franceqw.com/home.php?mod=space&uid=171923&do=blog&id=379885
https://www.sojianli.com/home.php?mod=space&uid=63574&do=blog&id=334093
https://www.dww11.com/home.php?mod=space&uid=127148&do=blog&quickforward=1&id=379
http://www.r-china.net/home.php?mod=space&uid=72455&do=blog&id=588
http://www.xuyi365.net/home.php?mod=space&uid=458456&do=blog&id=3522
http://www.neiha.com/home.php?mod=space&uid=21164&do=blog&id=416
https://www.huaibin.info/blog-18999-399.html
https://bbs.mu2002.com/home.php?mod=space&uid=15712&do=blog&id=935
https://www.xhssy.com.cn/home.php?mod=space&uid=67748&do=blog&id=700
https://53msw.com/home.php?mod=space&uid=235179&do=blog&id=2320
https://www.jingmenshequ.com/home.php?mod=space&uid=276&do=blog&id=1024
https://www.sehand.com/blog-764204-358.html
http://www.wumenshishe.com/home.php?mod=space&uid=181296&do=blog&id=4884
https://www.gushijibbs.com/home.php?mod=space&uid=802&do=blog&id=381
https://www.mcbourse.cn/blog-1905-669.html
https://www.pszx.com/home.php?mod=space&uid=48128&do=blog&id=590
http://www.baiyiha.com/blog-127-426.html
https://www.tuhuwai.com/home.php?mod=space&uid=48499&do=blog&id=3503842
https://bbs.qiuqiuread.com/home.php?mod=space&uid=849&do=blog&id=6869
https://bbs.637800.com/book-74673-370.html
http://www.lfnews.cn/home.php?mod=space&uid=21270&do=blog&id=453
https://53msw.com/home.php?mod=space&uid=235179&do=blog&id=2316
https://53msw.com/forum.php?mod=viewthread&tid=8282
http://zuimeng.happymm.cn/script/forum/view.asp?article_id=1576909
http://www.shdxlt.cn/ShowPost.asp?ThreadID=259089
http://bbs.hahbsj.com/home.php?mod=space&uid=10626&do=blog&id=1830
https://www.fuyangsite.com/home.php?mod=space&uid=1083&do=blog&id=424
http://www.e-ging.com/bbs/thread-2169.html
http://bbs.caup.net/blog-125232-9962.html
http://www.xcxy.net/blog-341351-484.html
http://sese.net.cn/bbs/apps.php?q=diary&uid=174234&a=detail&did=93956
https://bbs.huaqinren.com/blog-11470-626.html
http://bbs.hkctp.com.hk/apps.php?q=diary&uid=17362&a=detail&did=3976
https://www.aortec.com.cn/bbs//space/viewspacepost.aspx?postid=1879
http://www.0514.cc/html/blog/129/guestbook.htm
http://www.chubawang.net/thread-62327-1-1.html
https://www.option007.com/blog-13306-7673.html
http://www.guannantv.com/home.php?mod=space&uid=224501&do=blog&id=258
https://www.dww11.com/home.php?mod=space&uid=127148&do=blog&quickforward=1&id=414
http://bbs.xxsx.cn/boke.asp?panglss.showtopic.15344.html
http://bbs.qbgxl.com/apps.php?q=diary&uid=218&a=detail&did=508
https://bbs.hibet.im/thread-882734-1-1.html
https://bbs.duoceshi.cn/thread-53713-1-1.html
https://bbs.fengniao.com/forum/11710855.html

页: [1]
查看完整版本: 线性回归之最小二乘法