简单线性回归的解析解(最小二乘法)

  这里所说的简单线性指的是一元线性回归,即特征个数为1,并且样本个数为m个。符号表示中 y ^ i \hat y^i y^i表示的是对第i个样本的预测值。

  所以损失函数 L ( w , b ) = ∑ i = 1 m ( y i − y ^ i ) 2 = ∑ i = 1 m ( y i − ( w x i + b ) ) 2 L(w,b)=\sum \limits _{i=1} ^m(y^i-\hat y^i)^2=\sum \limits _{i=1} ^m(y^i-(wx^i+b))^2 L(w,b)=i=1m(yiy^i)2=i=1m(yi(wxi+b))2,则所求为
L ( w , b ) = argmin ⁡ w , b ∑ i = 1 m ( y i − y ^ i ) 2 = argmin ⁡ w , b ∑ i = 1 m ( y i − ( w x i + b ) ) 2 L(w,b)= \underset{w,b}{\operatorname{argmin}} \sum \limits _{i=1 } ^m(y^i-\hat y^i)^2= \underset{w,b}{\operatorname{argmin}} \sum \limits _{i=1} ^m(y^i-(wx^i+b))^2 L(w,b)=w,bargmini=1m(yiy^i)2=w,bargmini=1m(yi(wxi+b))2

  由于此函数是凸函数,所以极值点则为最小值点。则使
∂ L ∂ w = 0 ( 1 ) \frac{\partial L}{\partial w} =0 \quad(1) wL=0(1) ∂ L ∂ b = 0 ( 2 ) \frac{\partial L}{\partial b} =0 \quad(2) bL=0(2)

  先来求解公式(2),推导过程如下:
∂ L ∂ b = 0 ⇒ ∑ ( − 2 ) ∗ ( y i − ( w x i + b ) ) = 0 \frac{\partial L}{\partial b} =0 \Rightarrow \sum (-2)*(y^i-(wx^i+b))=0 bL=0(2)(yi(wxi+b))=0
∑ b = ∑ ( y i − w x i ) \sum b = \sum (y^i-wx^{i}) b=(yiwxi)
b = y ‾ − w x ‾ b = \overline { y }-w\overline { x} b=ywx

  再来求解公式(1),推导过程如下:
∂ L ∂ w = 0 ⇒ ∑ ( − 2 x i ) ∗ ( y i − ( w x i + b ) ) = 0 \frac{\partial L}{\partial w} =0 \Rightarrow \sum (-2x^{i})*(y^i-(wx^i+b))=0 wL=0(2xi)(yi(wxi+b))=0
  先左右两边同时除以-2,则公式如下所示:
∑ ( x i ) ∗ ( y i − ( w x i + b ) ) = 0 ⇒ ∑ x i y i − w ( x i ) 2 − x i b = 0 \sum (x^{i})*(y^i-(wx^i+b))=0 \Rightarrow \sum x^{i}y^{i}-w(x^{i})^2-x^{i}b=0 (xi)(yi(wxi+b))=0xiyiw(xi)2xib=0
  将 b = y ‾ − w x ‾ b = \overline { y }-w\overline { x} b=ywx带入上式,如下所示:
∑ x i y i − w ( x i ) 2 − x i ( y ‾ − w x ‾ ) = 0 ⇒ ∑ w ( x i ) 2 − w x ‾ x i = ∑ x i y i − x i y ‾ \sum x^{i}y^{i}-w(x^{i})^2-x^{i}( \overline { y }-w\overline { x})=0 \Rightarrow \sum w(x^i)^2-w\overline { x}x^i = \sum x^i y^i-x^i\overline { y} xiyiw(xi)2xi(ywx)=0w(xi)2wxxi=xiyixiy

   w = ∑ x i y i − x i y ‾ ∑ ( x i ) 2 − x ‾ x i w= \frac{ \sum x^iy^i-x^i \overline{y}}{\sum (x^i)^2-\overline { x}x^i} w=(xi)2xxixiyixiy

你可能感兴趣的:(经典算法,机器学习)