正规方程推导(Normal equation)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/welcom_/article/details/84311400

微积分差不多都还回去了

  1. 法一:
    m θ m为样例数目,\theta为列向量
    h θ ( x ) = θ 0 + θ 1 x + . . . θ n x J ( θ 0 , θ 1 . . . + θ n ) = 1 2 m i = 1 m ( h θ ( x ) ( i ) y ( i ) ) 2 h_\theta(x)=\theta_0+\theta_1x+...\theta_nx,J(\theta_0,\theta_1...+\theta_n)=\frac {1}{2m}\sum_{i=1}^m(h_\theta(x)^{(i)}-y^{(i)})^2 δ δ θ j J ( θ 0 , θ 1 + . . . θ n ) = 1 m i = 1 m ( h θ ( i ) ( x ) y ( i ) ) x j ( i ) j = 0 1 . . . n \frac{\delta}{\delta\theta_j}J(\theta_0,\theta_1+...\theta_n)=\frac {1}{m}\sum_{i=1}^m(h_\theta^{(i)}(x)-y^{(i)})x^{(i)}_j(j=0,1,...n) 1 m i = 1 m ( h θ ( i ) ( x ) y ( i ) ) x j ( i ) = x j T ( X θ y ) 其中任意偏导数可表示为\frac {1}{m}\sum_{i=1}^m(h_\theta^{(i)}(x)y^{(i)})x^{(i)}_j=x_j^T(X\theta-y) x j 0 (x_j为相应列向量)然后让全部偏导数为0,综合可得到: X T ( X θ y ) = 0 X^T*(X\theta-y)=0 X T X θ = X T y X^TX\theta=X^Ty θ = ( X T X ) 1 X T y \theta=(X^TX)^{-1}X^Ty

  2. 法二: normal equation 推导思路
    m = 2 X = [ 1 x 1 1 1 x 1 2 ] , Y = [ y 1 1 y 1 2 ] , θ = [ θ 1 1 θ 1 2 ] 先推导,用例数目m=2时,X=\begin{bmatrix} 1 & x_1^1 \\ 1 & x_1^2 \\ \end{bmatrix},Y= \begin{bmatrix} y_1^1 \\ y_1^2 \\ \end{bmatrix},\theta=\begin{bmatrix} \theta_1^1 \\ \theta_1^2 \\ \end{bmatrix}
    h θ ( x ) = θ 0 + θ 1 x , J ( θ 0 , θ 1 ) = 1 2 m i = 1 m ( h θ ( i ) ( x ) y ( i ) ) 2 h_\theta(x)=\theta_0+\theta_1x,J(\theta_0,\theta_1)=\frac {1}{2m}\sum_{i=1}^m(h_\theta^{(i)}(x)-y^{(i)})^2 X , Y , θ J ( θ 0 , θ 1 ) θ 0 , θ 1 将X,Y,\theta代入J(\theta_0,\theta_1)然后对\theta_0,\theta_1分别求偏导数,让 δ δ θ j J ( θ 0 , θ 1 ) = 0 j = 0 1 \frac{\delta}{\delta\theta_j}J(\theta_0,\theta_1)=0(j=0,1)
    X T X θ = X T y > θ = X T X ) 1 X T y 再对两个等式相加,经过整理就会发现,X^TX\theta=X^Ty-->\theta=(X^TX)^{-1}X^Ty
    广 m = n X T X θ = X T y > θ = X T X ) 1 X T y 可想而知,经过推广,m=n时,也可以得出X^TX\theta=X^Ty-->\theta=(X^TX)^{-1}X^Ty

  3. normal equation vs gradient descent
    正规方程:一步到位,算法复杂度为 O ( n 3 ) O(n^3) ,所以特征维度<10000时,使用normal equation。
    梯度下降: 选择 α \alpha 并调试它(很耗时间),多次迭代(很耗时间),特征参数很大时也ok。

猜你喜欢

转载自blog.csdn.net/welcom_/article/details/84311400