Normal Equation(正规方程)

Normal Equation

Note: [8:00 to 8:44 - The design matrix X (in the bottom right side of the slide) given in the example should have elements x with subscript 1 and superscripts varying from 1 to m because for all m training sets there are only 2 features x0 and x1. 12:56 - The X matrix is m by (n+1) and NOT n by n. ]

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:

θ=(XTX)?1XTy

There is no need to do feature scaling with the normal equation.

The following is a comparison of gradient descent and the normal equation:

Gradient Descent Normal Equation
Need to choose alpha No need to choose alpha
Needs many iterations No need to iterate
O (kn2) O (n3), need to calculate inverse of XTX
Works well when n is large Slow if n is very large

With the normal equation, computing the inversion has complexity O(n3). 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.

Normal Equation Noninvertibility

When implementing the normal equation in octave we want to use the ‘pinv‘ function rather than ‘inv.‘ The ‘pinv‘ function will give you a value of θ even if XTX is not invertible.

If XTX is noninvertible, the common causes might be having :

  • Redundant features, where two features are very closely related (i.e. they are linearly dependent)
  • Too many features (e.g. m ≤ n). In this case, delete some features or use "regularization" (to be explained in a later lesson).

Solutions to the above problems include deleting a feature that is linearly dependent with another or deleting one or more features when there are too many features.

时间: 2024-10-12 11:43:50

Normal Equation(正规方程)的相关文章

Linear regression with multiple variables(多特征的线型回归)算法实例_梯度下降解法(Gradient DesentMulti)以及正规方程解法(Normal Equation)

%第一列为 size of House(feet^2),第二列为 number of bedroom,第三列为 price of House 1 2104,3,399900 2 1600,3,329900 3 2400,3,369000 4 1416,2,232000 5 3000,4,539900 6 1985,4,299900 7 1534,3,314900 8 1427,3,198999 9 1380,3,212000 10 1494,3,242500 11 1940,4,239999 1

梯度下降算法与Normal equation

Normal equation: Method to solve for θ analytically 正规方程:分析求解θ的方法 对于损失函数 \[J\left( {{\theta _0},{\theta _1},...,{\theta _n}} \right) = \frac{1}{{2m}}\sum\limits_{i = 1}^m {{{\left( {{h_\theta }\left( {{x^{\left( i \right)}}} \right) - {y^{\left( i \r

[ML]简单的Normal Equation对数据点进行线性回归

注明:本文仅作为记录本人的日常学习历程而存在. Normal Equation和上篇介绍的方法相比,简单许多.具体公式见吴恩达老师的coursera视频 1. generate_data用来生成实验中所用到的数据,数据总体分布在斜率为10-30之间随机取值,截距为200-5000之间随机取值的直线上 compute函数用来计算出目标直线参数: import numpy as np import matplotlib.pyplot as plt def compute(X,Y): return (

用线性代数理解 Normal Equation

在之前的博客中,我们通过矩阵求导的方式推导了 normal equation.这篇博客中,我们将通过线性代数的角度再次回顾 normal equation. Normal Equation 解决的问题 Normal equation 要解决的问题如下:给出由 $n$ 个未知数组成的 $m$ 个方程,这个方程组用 $Ax=b$ 表示,我们要求出这个方程组的“最佳解”. 根据线性代数的知识我们知道,这个方程组的解可能有零个.一个或无穷多个.如果这个方程组恰有一个解,那么这个解就是“最佳解”:如果这个

(三)Normal Equation

继续考虑Liner Regression的问题,把它写成如下的矩阵形式,然后即可得到θ的Normal Equation. Normal Equation: θ=(XTX)-1XTy 当X可逆时,(XTX)-1XTy = X-1,(XTX)-1XTy其实就是X的伪逆(Pseudo inverse).这也对应着Xθ = y ,θ = X-1y 考虑特殊情况 XTX 不可逆 解决办法: 1)考虑是否有冗余的特征,例如特征中有平方米,还有平方厘米,这两个特征就是冗余的,解决办法是去掉冗余 2)再有就是n

【转】Derivation of the Normal Equation for linear regression

I was going through the Coursera "Machine Learning" course, and in the section on multivariate linear regression something caught my eye. Andrew Ng presented the Normal Equation as an analytical solution to the linear regression problem with a l

转载:Normal Equation证明及应用

转载:原文地址为http://www.cnblogs.com/elaron/archive/2013/05/20/3088894.html 作者:elar Many Thanks ---------------------------------------------------------------------------------------------------- Normal Equations 的由来 假设我们有m个样本.特征向量的维度为n.因此,可知样本为{(x(1),y(1

机器学习笔记02:多元线性回归、梯度下降和Normal equation

在<机器学习笔记01>中已经讲了关于单变量的线性回归以及梯度下降法.今天这篇文章作为之前的扩展,讨论多变量(特征)的线性回归问题.多变量梯度下降.Normal equation(矩阵方程法),以及其中需要注意的问题. 单元线性回归 首先来回顾一下单变量线性回归的假设函数: Size(feet2) Price($1000) 2104 460 1416 232 1534 315 852 178 - - 我们的假设函数为 hθ(x)=θ0+θ1x 多元线性回归 下面介绍多元线性回归(Linear R

正规方程(Normal Equation)——对于线性回归问题的一种快速解法

对于某些线性回归问题,正规方程方法可能更加简单高效. 正规方程推导过程如下: 梯度下降法和正规方程的比较: 总结: 只要特征数量并不是特别大,对于线性回归问题正规方程是一个比梯度下降算法更快的替代算法.但是当特征数量非常多的时候或者模型更复杂的时候(比如logistic regression等),正规方程就不再适用了.而梯度下降方法都可以使用.另外,当XTX是奇异矩阵(也称退化矩阵,不可逆)时,正规方程也不能使用,但是这种情况很少会发生(m≤n或者存在有依赖关系的特征).