带你在3分钟内掌握线性回归,一元

发表时间:2020-11-15
  • 一元线性回归模型:

y = h θ ( x ) = θ 0 + θ 1 x y=h_\theta(x)=\theta _0 +\theta _1 x y = h θ ( x ) = θ 0 + θ 1 x

输入只包含一个单独的特征。

  • 对于样本 ( x ( i ) , y ( i ) ) (x^{(i)},y^{(i)}) ( x ( i ) , y ( i ) ) ,模型预测值为: y ^ ( i ) = θ 0 + θ 1 x ( i ) {\hat y}^{(i)}=\theta _0 +\theta _1 x^{(i)} y ^ ( i ) = θ 0 + θ 1 x ( i )

  • 误差/残差:样本真实值与预测值之差

e ( i ) = y ( i ) − y ^ ( i ) = y ( i ) − θ 0 − θ 1 x ( i ) e^{(i)}=y^{(i)}-{\hat{y}}^{(i)}=y^{(i)}-\theta _0 -\theta _1 x^{(i)} e ( i ) = y ( i ) y ^ ( i ) = y ( i ) θ 0 θ 1 x ( i )

  • 给定训练集 D = { ( x i , y i ) } D=\{(x_i,y_i)\} D = { ( x i , y i ) } ,找到一条直线(模型)
    y = h θ ( x ) = θ 0 + θ 1 x y=h_{\theta}(x)=\theta _0 +\theta _1 x y = h θ ( x ) = θ 0 + θ 1 x
    使得所有样本尽可能落在它的附近。

  • 损失函数(Loss function):最小化均方误差,从而寻找最优的参数。
    m i n θ 1 , θ 0 L ( θ 1 , θ 0 ) = m i n θ 1 , θ 0 ∑ i = 1 m ( y ( i ) − θ 0 − θ 1 x ( i ) ) 2 min_{\theta_1,\theta_0}L(\theta_1,\theta_0)=min_{\theta_1,\theta_0}{\sum_{i=1}^{m}{(y^{(i)}-\theta _0 -\theta _1 x^{(i)}})^2} m i n θ 1 , θ 0 L ( θ 1 , θ 0 ) = m i n θ 1 , θ 0 i = 1 m ( y ( i ) θ 0 θ 1 x ( i ) ) 2

  • 闭式解

    将目标函数 L ( θ 1 , θ 0 ) L(\theta_1,\theta_0) L ( θ 1 , θ 0 ) 求偏导:
    ∂ L ∂ θ 0 = ∑ i = 1 m 2 ( y ( i ) − θ 0 − θ 1 x ( i ) ) ( − 1 ) = 0 ∂ L ∂ θ 1 = ∑ i = 1 m 2 ( y ( i ) − θ 0 − θ 1 x ( i ) ) ( − x i ) = 0 \frac{\partial L}{\partial \theta_0 }=\sum_{i=1}^{m}{2(y^{(i)}-\theta _0 -\theta _1 x^{(i)})(-1)}=0\\ \frac{\partial L}{\partial \theta_1 }=\sum_{i=1}^{m}{2(y^{(i)}-\theta _0 -\theta _1 x^{(i)})(-x_i)}=0 θ 0 L = i = 1 m 2 ( y ( i ) θ 0 θ 1 x ( i ) ) ( 1 ) = 0 θ 1 L = i = 1 m 2 ( y ( i ) θ 0 θ 1 x ( i ) ) ( x i ) = 0
    可以解得:

    θ 1 ^ = ∑ i = 1 m x ( i ) y ( i ) − m x ‾ y ‾ ∑ i = 1 m ( x ( i ) ) 2 − m x ‾ 2 \hat{\theta_1}=\frac{\sum_{i=1}^m{x^{(i)}y^{(i)}-m\overline x \overline y }}{\sum_{i=1}^m{(x^{(i)})^2-m{\overline x}^2}} θ 1 ^ = i = 1 m ( x ( i ) ) 2 m x 2 i = 1 m x ( i ) y ( i ) m x y

    θ 0 ^ = y ‾ − θ 1 x ‾ \hat{\theta_0}=\overline{y}-\theta_1 \overline x θ 0 ^ = y θ 1 x

  • 代码

import numpy as np
import matplotlib.pyplot as plt
# 以下两行代码解决jupyter notebook显示图片模糊问题
%matplotlib inline
%config InlineBackend.figure_format = 'svg'
# 支持中文
plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False  # 用来正常显示负号
##  测试数据
X = np.arange(0,10,0.1)
Y = 3*X+np.random.random([X.size])*5

def f(x,a):
    X = np.ones((len(x),2))
    X[:,1]=x
    return X.dot((a.T))
a = np.zeros((2))
a[1] = ((X*Y).sum()- X.sum()*Y.sum()/len(X))/((X*X).sum()-X.sum()*X.sum()/len(X))
a[0] = Y.sum()/len(Y)- a[1]*X.sum()/len(X)

Y_Test = f(X,a)
plt.scatter(X,Y,label="原始数据",c='b',alpha=0.5)
plt.plot(X,Y_Test,label="回归线",c='r')
plt.title('一元线性回归')
plt.xlabel("X")
plt.ylabel("Y")
plt.legend()
plt.show()

在这里插入图片描述

文章来源互联网,如有侵权,请联系管理员删除。邮箱:417803890@qq.com / QQ:417803890

微配音

Python Free

邮箱:417803890@qq.com
QQ:417803890

皖ICP备19001818号
© 2019 copyright www.pythonf.cn - All rights reserved

微信扫一扫关注公众号:

联系方式

Python Free