首先是求线性回归的代价函数(包括正则化的线性回归):
ta = [0;theta(2:end)];
J = ((X*theta)-y)'*((X*theta)-y)/(2*m) + lambda/(2*m)*(ta'*ta);grad = (((X*theta)-y)'*X)'/m+lambda/m*ta;
求学习曲线(注意此时λ为0):
for i = 1:m
[theta] = trainLinearReg([ones(i,1) X(1:i,:)],y(1:i,:), lambda);
theta = theta(:);
[error_train(i),grad] = linearRegCostFunction([ones(i,1) X(1:i,:)],y(1:i,:),theta,0);
[error_val(i),grad] = linearRegCostFunction([ones(size(Xval,1),1) Xval],yval,theta,0);
end
其中linearRegCostFunction是上面求代价函数。
求多项式:
m = numel(X);
for i = 1:m
for j = 1:p
X_poly(i,j) = X(i).^j;
end
end
验证λ与误差(注意此时λ为0)
m = numel(lambda_vec);
for i=1:m
[theta] = trainLinearReg([ones(size(X,1),1) X],y, lambda_vec(i));
[error_train(i),grad] = linearRegCostFunction([ones(size(X,1),1) X],y,theta,0);
[error_val(i),grad] = linearRegCostFunction([ones(size(Xval,1),1) Xval],yval,theta,0);
end