4. Multiple features

Multiple features

Multiple features

Notation:

  • n = number of features
  • x^{(i)} = input (features) of i^{th} training example.
  • x^{(i)}_j = value of feature j in i^{th} training example.

h_\Theta (x)=\Theta_0+\Theta_1x_1+\Theta_2x_2+...+\Theta_nx_n
For convenience of notation, define x_0=1
x= \left[ \begin{matrix} x_0 \\ x_1 \\ x_2 \\ ... \\ x_3 \\ \end{matrix} \right] $$ \Theta = \left[ \begin{matrix} \Theta_0 \\ \Theta_1 \\ \Theta_2 \\ ... \\ \Theta_3 \\ \end{matrix} \right]
h_\Theta(x)=\Theta^Tx

向量內(nèi)積
Multivariate linear regression

Gradient descent for multiple variables

Hypothesis: h_\Theta (x)=\Theta_0+\Theta_1x_1+\Theta_2x_2+...+\Theta_nx_n
Parameters: \Theta(a n+1 dimensional vector)
Cost function: j(\Theta_0,\Theta_1,...,\Theta_n)=\frac{1}{2m}\sum^m_{i=1}(h_\Theta(x^{(i)}-y^{(i)})^2

Gradient descent:
Repeat {
\Theta_j:=\Theta_j-\alpha\frac{\partial}{\partial\Theta_j}J(\Theta_0,...,\Theta_n)
}

Feature Scaling

Idea:Make sure features are on a similar scale

一般認(rèn)為在-3\le x\le +3就可以認(rèn)為是可以的
Feature Scaling:Get every feature into approximately a -1\le x_i\le 1 range.
Mean normalization:Replace x_i with x_i-\mu_i to make features have approximately zero mean.(Do not apply to x_0=1)

Learning rate

  1. Make sure gradient descent is working correctly.
    J(\theta) should decrease on every iteration.But if \alpha is too small, gradient descent can be slow to converge.

Features and Polynomial Regression

Normal Equation

Normal equation: Method to solve for \theta ananlytically.

\Theta =(X^TX)^{-1}X^Ty
Octave:pinv(x'*x)*x'*y

m training examples, n features

Gradient Descent

  • Need to choose \alpha
  • Needs many iterations
  • Works well even when n is large

Normal Equation

  • No need to choose \alpha
  • Don't need to iterate
  • Need to compute (X^TX)^{-1}
  • Slow if n is very large(less than 10000)

Normal Equation Nonivertibility (Optional)

What if X^TX is non-invertible?

  • Redundant features (linearly dependent)
  • Too many features(e.g. m\le n)
    這種情況極少出現(xiàn)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容