线性回归代码

Linear Regression Code

我正在学习 Andrew Ng class 机器学习 并实现线性回归算法。

我的代码有什么问题?

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); 
J_history = zeros(num_iters, 1);
h = (X*theta)
for iter = 1:num_iters
    theta(1,1) = theta(1,1)-(alpha/m)*sum((h-y).*X(:,1));
    theta(2,1) = theta(2,1)-(alpha/m)*sum((h-y).*X(:,2));  
    J_history(iter) = computeCost(X, y, theta);
end
end

成本函数为:

function J = computeCost(X, y, theta)
m = length(y); 
h = (X*theta)
J = (1/(2*m))*sum((h-y).^2)
end

J_history的值不断增加。它给出了非常不正常的(大值),即比它应该的多 1000 倍。

您需要更新 for 循环中的 htheta,如下所示

function [theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)
m = length(y); 
J_history = zeros(num_iters, 1);

for iter = 1:num_iters
    h = ((X*theta)-y)'*X;
    theta = theta - alpha*(1/m)*h';
    J_history(iter) = computeCost(X, y, theta);
end
end