代码改变世界

谈谈NG视频里面单线性回归、多线性回归、逻辑回归等等

2015-10-30 00:38  Yan消云散_  阅读(231)  评论(0编辑  收藏  举报

  明天第一节课8.55才上,还是把今天看的东西整理一下吧。

  今天主要是看了NG前几章讲的单线性回归、多线性回归、逻辑回归的matlab实现,之前觉得那些东西理解还好,但是写代码好难的样子,但是今天看了大牛的代码发现真的很easy... 但是是很有技巧的用的矩阵去实现。

  比如单线性回归里面的j=0和j=1这两种情况,直接把x转换成x = [ones(m, 1) x] , 第一列全是1了,刚好可以把j=0时x=1代入去运算,这样子梯度 grad = (1/m).* x' * ((x * theta) - y) , theta = theta - alpha .* grad ,外面加个循环就可以求出theta0 和 theta 1 (其实都在theta矩阵里)。

clear all; close all; clc
x = load('ex2x.dat'); y = load('ex2y.dat');

m = length(y); % number of training examples


% Plot the training data
figure; % open a new figure window
plot(x, y, 'o');
ylabel('Height in meters')
xlabel('Age in years')

% Gradient descent
x = [ones(m, 1) x]; % Add a column of ones to x
theta = zeros(size(x(1,:)))'; % initialize fitting parameters
MAX_ITR = 1500;
alpha = 0.07;

for num_iterations = 1:MAX_ITR
    % This is a vectorized version of the 
    % gradient descent update formula
    % It's also fine to use the summation formula from the videos

    % Here is the gradient
    %代价函数的导数:刚好把j=0和j=1的情况巧妙的用矩阵的方法归为一个式子
    %grad = 1/m * (h-y) 或 1/m * (h-y)*x
    grad = (1/m).* x' * ((x * theta) - y);

    % Here is the actual update
    theta = theta - alpha .* grad;

    % Sequential update: The wrong way to do gradient descent
    % grad1 = (1/m).* x(:,1)' * ((x * theta) - y);
    % theta(1) = theta(1) + alpha*grad1;
    % grad2 = (1/m).* x(:,2)' * ((x * theta) - y);
    % theta(2) = theta(2) + alpha*grad2;
end
% print theta to screen
theta

% Plot the linear fit
hold on; % keep previous plot visible
plot(x(:,2), x*theta, '-')
legend('Training data', 'Linear regression')%标出图像中各曲线标志所代表的意义
hold off % don't overlay any more plots on this figure,指关掉前面的那幅图

  

  还有就是牛顿法直接求解参数,一句w=inv(x'*x)*x'*y 就直接求出了theta0 和 theta 1 ,看来还需看看最优化这方面的东西啊。

  

%%方法一
x = load('ex2x.dat');
y = load('ex2y.dat');
plot(x,y,'*')
xlabel('height')
ylabel('age')
x = [ones(size(x,1),1),x];
w=inv(x'*x)*x'*y
hold on
%plot(x,0.0639*x+0.7502) 
plot(x(:,2),0.0639*x(:,2)+0.7502)%更正后的代码

  后面就是画代价函数的三维图,一个surf函数直接可以画出三维图像。

 

  可参考:http://huzhyi21.blog.163.com/blog/static/1007396201061052214302/

% Calculate J matrix

% Grid over which we will calculate J
theta0_vals = linspace(-3, 3, 100);
theta1_vals = linspace(-1, 1, 100);

% initialize J_vals to a matrix of 0's
J_vals = zeros(length(theta0_vals), length(theta1_vals));

for i = 1:length(theta0_vals)
      for j = 1:length(theta1_vals)
      t = [theta0_vals(i); theta1_vals(j)];    
      J_vals(i,j) = (1/2*m) .* (x * t - y)' * (x * t - y);
      %J_vals就是代价函数: 1/2m * (h-y)^2
    end
end

% Because of the way meshgrids work in the surf command, we need to 
% transpose J_vals before calling surf, or else the axes will be flipped
J_vals = J_vals';
% Surface plot
figure;
surf(theta0_vals, theta1_vals, J_vals)
xlabel('\theta_0'); ylabel('\theta_1');