# 参数计算分析——Computing Parameters Analytically

==================

## 正规方程——Normal Equation

$\theta \in {R}^{n+1},J\left({\theta }_{0},{\theta }_{1},...,{\theta }_{m}\right)=\frac{1}{2m}\sum _{i=1}^{m}\left({h}_{\theta }\left({x}^{\left(i\right)}\right)-{y}^{\left(i\right)}{\right)}^{2}$$\theta \in R^{n+1}, J(\theta_0,\theta_1,...,\theta_m)= \frac{1}{2m}\sum_{i=1}^m(h_\theta(x^{(i)})-y^{(i)})^2$

$\cfrac{\partial}{\partial\theta}J(\theta)=···=0 \space \space (for \space every\space j)$

**Example：**m=4

Size (feet*feet) Number of bedrooms Number of floors Age of home(years) Price (\$1000)
${x}_{0}$$x_0$ ${x}_{1}$$x_1$ ${x}_{2}$$x_2$ ${x}_{3}$$x_3$ ${x}_{4}$$x_4$ $y$$y$
1 2104 5 1 45 460
1 1416 3 2 40 232
1 1534 3 2 30 315
1 852 2 1 36 178

$X=\left[\begin{array}{cccccc}1& 2104& 5& 1& 45& 460\\ 1& 1416& 3& 2& 40& 232\\ 1& 1534& 3& 2& 30& 315\\ 1& 852& 2& 1& 36& 178\end{array}\right]$

$y=\left[\begin{array}{c}460\\ 232\\ 315\\ 178\end{array}\right]$

$\theta =\left({X}^{T}X{\right)}^{-1}{X}^{T}y$

$m$$m$ example $\left({x}^{\left(1\right)},{y}^{\left(1\right)}\right),\left({x}^{\left(2\right)},{y}^{\left(2\right)}\right),\left({x}^{\left(m\right)},{y}^{\left(m\right)}\right)$$(x^{(1)},y^{(1)}),(x^{(2)},y^{(2)}),(x^{(m)},y^{(m)})$; $n$$n$feature.
${x}^{\left(i\right)}=\left[\begin{array}{c}{x}_{0}^{\left(i\right)}\\ {x}_{1}^{\left(i\right)}\\ .\\ .\\ .\\ {x}_{n}^{\left(i\right)}\end{array}\right]\in {R}^{n+1}$$x^{(i)}=\begin{bmatrix}x_0^{(i)}\\x_1^{(i)}\\.\\.\\.\\x_n^{(i)}\end{bmatrix}\in R^{n+1}$

${x}^{\left(i\right)}=\left[\begin{array}{c}—\left({x}^{\left(1\right)}{\right)}^{T}—\\ —\left({x}^{\left(2\right)}{\right)}^{T}—\\ .\\ .\\ .\\ —\left({x}^{\left(m\right)}{\right)}^{T}—\end{array}\right]\in {R}^{n+1}$$x^{(i)}=\begin{bmatrix}—(x^{(1)})^T—\\—(x^{(2)})^T—\\.\\.\\.\\—(x^{(m)})^T—\end{bmatrix}\in R^{n+1}$

$\theta =\left({X}^{T}X{\right)}^{-1}{X}^{T}y$

$m$$m$ 个训练样本，$n$$n$ 个特征。

$O\left(k{n}^{2}\right)$$O(kn^2)$ $O\left({n}^{3}\right)$$O(n^3)$