代码之家  ›  专栏  ›  技术社区  ›  K.WANG

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

  •  2
  • K.WANG  · 技术社区  · 8 年前

    我尝试了正常方程,结果是正确的。 然而,当我使用梯度下降时,结果是错误的。我参考了在线资源,但没有找到问题所在。我不认为下面的代码有什么特殊之处。

    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
    
    1 回复  |  直到 8 年前
        1
  •  1
  •   user2900180 user2900180    8 年前

    您的代码是正确的。问题是迭代次数少。 可以取num_ iters=5000;并且看到θ收敛到右值([10;2])。