学习总结:机器学习(四)
规范方程(The normal equations)
这一部分主要讲述怎样用矩阵的形式描述线性回归。
首先定义几个概念:
(1)A为m*n的矩阵,f(A)是关于A的函数,它将矩阵A映射为一个实数,
f关于A的导数可以定义为:
(2)A为n*n的矩阵,矩阵的轨迹定义为:
即:矩阵的轨迹为其对角元素的和
矩阵的轨迹有如下的一些性质:


综合以上的两个概念,我们可以得到如下的一些性质:

根据以上的概念和性质,我们可以用矩阵的方式来描述最小二乘法。
训练集可用输入矩阵X定义如下:

X的每一行是一个训练样本的特征
目标向量可用m维的向量
定义如下:

有:

经以下推导,有:

为了最小化J(θ),必须使其导数为0,因此有:

上面这个方程称为规范方程。
由此,可以得到θ的表达式:

matlab代码实现:
%normalEquation function is used to train data for linear regression.
%FEATURE is the matrix that composed of features of the training examples.
%VALUE is the matrix that composed of output values of the training
%examples.
%THETA is the parameter of the hypotheses function.
function [theta] = normalEquation(feature,value)
num = size(feature,1);
features = [ones(num,1) feature];
invertMat = (features'*features)^-1;
theta = invertMat*features'*value;
hypvalue = features*theta;
plot(feature,hypvalue,'rx-',feature,value,'bo');
end