在coursera上看了Andrew Ng的《Machine Learning》课程,里面讲到了正规方程(Normal Equation),现在在此记录一下推导过程。
假设函数(Hypothesis Function)为:
h_theta(x)=theta_0 theta_1x_1 theta_2x_2 cdots theta_nx_n此处我们可以令
x_0=1.
代价函数(Cost Function):
J(theta)=J(theta_0,ldots,theta_n)=frac {1} {2m} sum_{i=1}^{m} {(h_theta(x^{(i)})-y^{(i)})^2} 我们想要代价函数的最小解,对代价函数进行求导。因为对于向量我们有
z^Tz=sum_{i} z_i^2,所以:
J(theta)=frac {1}{2m}(Xtheta-y)^T(Xtheta-y)因为
frac {1} {2m}部分对最终的解没影响,为了便于书写和计算,我们可以先将这部分舍去。对方程的转置进行化简:
J(theta)=(theta^TX^T-y^T)(Xtheta-y)J(theta)=theta^TX^TXtheta-(Xtheta)^Ty-y^TXtheta y^Ty因为
xtheta和y都是矢量,所以这两者相乘先后顺序没有关系,所以可以化简成:
J(theta)=theta^TX^TXtheta-2(Xtheta)^Ty y^Ty 接着方程
J(theta)对theta进行求导:
frac {partial}{partialtheta}J(theta)=2X^TXtheta-2X^Ty=01
frac {partial}{partialtheta}J(theta)=0时,得到最合适thetaX^TXtheta=X^Ty两边同时乘以
X^TX的逆矩阵,得:
theta=(X^TX)^{-1}X^Ty 此即为正规方程。当
theta=(X^TX)^{-1}X^Ty时,代价方程有最优解。
- 关于矩阵、向量、标量的一些求导https://blog.csdn.net/xidianliutingting/article/details/51673207 ↩