风过蔷薇

导航

 

linearRegCostFunction.m

J = (sum((X*theta - y).^2)+(sum(theta.^2) - theta(1).^2)*lambda)/(2*m);
theta1 = [0 ; theta(2:size(theta), :)];
grad = (X'*(X*theta - y) + lambda*theta1)/m;

 

learningCurve.m

n = size(Xval, 1);
for i=1:m
    [theta] = trainLinearReg(X(1:i, :), y(1:i), lambda);
    error_train(i) = sum((X(1:i, :)*theta - y(1:i)).^2)/(2 * i);
    error_val(i) = sum((Xval*theta - yval).^2)/(2*n);
end

 

polyFeatures.m

for i=1:p
    X_poly(:, i) = X.^i;
end

 

validationCurve.m

for i=1:length(lambda_vec)  
    lambda=lambda_vec(i);  
    theta=trainLinearReg(X,y, lambda);  
    [error_train(i),grad]=linearRegCostFunction(X, y, theta, 0);   
    [error_val(i),  grad]=linearRegCostFunction(Xval, yval, theta, 0);   
end

 

 

 

posted on 2018-02-15 17:24  风过蔷薇  阅读(189)  评论(0编辑  收藏  举报