Processing math: 100%

2017年9月18日 星期一

Week2

Multiple Features

  • 有多個變數的Linear Regression稱為multivariate linear regression
  • 符號說明如下:
x(i)j=value of feature j in the ith training examplex(i)=the input (features) of the ith training examplem=the number of training examplesn=the number of features
  • 其hypothesis function的形式為:
hθ(x)=θ0+θ1x1+θ2x2+θ3x3++θnxn
  • 也可用matrix的形式來表示:
hθ(x)=[θ0θ1...θn][x0x1xn]=θTx


Gradient Descent For Multiple Variables

  • 演算法如下:

repeat until convergence:{θ0:=θ0α1mmi=1(hθ(x(i))y(i))x(i)0θ1:=θ1α1mmi=1(hθ(x(i))y(i))x(i)1θ2:=θ2α1mmi=1(hθ(x(i))y(i))x(i)2}


  • Feature Scaling
    • 當Feature的range很大時,Descent的速度會很慢
    • Input Variable的理想range
      • −1 ≤ x(i) ≤ 1
      • 大約就好,太大太小都不好
    • Mean normalization
      • μi 是feature (i)的平均值
      • si 是the range of values (max - min)或標準差
      • xi:=xiμisi

Normal Equation

  • 為了得到Cost Function J的最小值,可以針對各個θi做偏微分後設成0,就可以求出θ的最佳解如下:
θ=(XTX)1XTy


  • X, y的例子如下


  • 使用Normal Equation就不需要做Feature Scaling
  • Gradient Descent跟Normal Equation的比較如下:

Gradient DescentNormal Equation
Need to choose alphaNo need to choose alpha
Needs many iterationsNo need to iterate
O (kn2)O (n3), need to calculate inverse of XTX
Works well when n is largeSlow if n is very large

  • 實作上當n超過10000的時候就會考慮用Gradient Descent

Normal Equation Noninvertibility

  • 如果X^T X是不可逆的,可能的原因如下:
    • 有重複的Feature出現 (如線性相關的兩個Feature(ft/m))
    • Feature數量太多 (m≤ n)
      • 刪除一些Feature或做Regularization







沒有留言:

張貼留言