Normal Equation
正规方程 (Normal Equation) 对于某些线性回归问题 ,提供了一种求 θ 的解析解法 ,不用使用迭代算法, 我们可以直接一次性求解θ的最优值
Gradient descent gives one way of minimizing J. Let’s discuss a second way of doing so, this time performing the minimization explicitly and without resorting to an iterative algorithm.
In the "Normal Equation" method, we will minimize J by explicitly taking its derivatives with respect to the θj ’s, and setting them to zero. This allows us to find the optimum theta without iteration. The normal equation formula is given below:
There is no need to do feature scaling with the normal equation.
The following is a comparison of gradient descent and the normal equation:
With the normal equation, computing the inversion has complexity O(n^3) . So if we have a very large number of features, the normal equation will be slow. In practice, when n exceeds 10,000 it might be a good time to go from a normal solution to an iterative process.
如果你用矩阵 X 和向量 y 来计算, 这个 θ 等于 X 转置乘以 X 的逆 乘以 X 转置 乘以 y , 这样就得到能够使得代价函数最小化的 θ . 课程没有证明这个式子, 但是数学上是可以证明的 , 这个式子会给出最优的 θ 值 . 就是说如果令 θ 等于这个 , 这个 θ 值会最小化这个线性回归的代价函数 J(θ)
总结一下,只要特征变量的数目并不大, 正规方程是一个很好的计算参数 θ 的替代方法.
具体地说,只要特征变量数量小于一万,通常使用正规方程法,而不使用梯度下降。
随着我们要讲的学习算法越来越复杂 例如 当讲到分类算法 像逻辑回归算法 我们会看到 实际上对于那些算法 并不能使用正规方程法。 对于那些更复杂的学习算法, 我们将不得不仍然使用梯度下降法。
因此,梯度下降法是一个非常有用的算法, 可以用在有大量特征变量的线性回归问题 。因为, 标准方程法不适合或者不能用在它们上, 但对于这个特定的线性回归模型, 正规方程法是一个 比梯度下降法更快的替代算法。 所以, 根据具体的问题 ,以及你的特征变量的数量, 这两算法都是值得学习的。
来源:coursera 斯坦福 吴恩达 机器学习