我不知道我的线性回归代码有什么问题

I don't know what's wrong with my linear regression code

我试过正规方程,结果是正确的。 然而,当我使用梯度下降时,结果发现这个数字是错误的。我参考了在线资源,但未能找出问题所在。我不认为下面的代码有什么特别之处。

clear;
clc;
m = 100; % generate 100 points
noise = randn(m,1); % 100 noise of normal distribution
x = rand(m, 1) * 10; % generate 100 x's ranging from 0 to 10
y = 10 + 2 * x + noise; 
plot (x, y, '.');
hold on;


X = [ones(m, 1) x];
theta = [0; 0];
plot (x, X * theta, 'y');
hold on;

% Method 1 gradient descent
alpha = 0.02; % alpha too big will cause going far away from the result
num_iters = 5;
[theta, J_history] = gradientDescent(X, y, theta, alpha, num_iters)

% Method 2 normal equation
% theta = (pinv(X' * X )) * X' * y

plot (x, X * theta, 'r');



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,
        theta = theta - alpha * (1/m) * (X' * (X * theta - y));

        % plot (X(:, 2), X * theta, 'g');
        % hold on;

        J_history(iter) = costFunction(X, y, theta);
    end
end

function J = costFunction( X, y, theta )
    m = length(y);  
    predictions = X * theta; % prediction on all m examples 
    sqrErrors = (predictions - y).^2; % Squared errors
    J = 1/(2*m) * sum(sqrErrors); 
end

您的代码是正确的。问题是迭代次数少。 一个可以拿 num_iters = 5000;并看到 theta 收敛到正确的值 ([10; 2]).