Last active
          January 22, 2016 20:41 
        
      - 
      
- 
        Save fmm/e737b7afdc61fb2e1c25 to your computer and use it in GitHub Desktop. 
Revisions
- 
        Filipe Martins revised this gist Jan 22, 2016 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,9 +32,9 @@ load('q1x.dat'); % m x 2 load('q1y.dat'); % m x 1 [m n] = size(q1x); X = [ones(m,1) q1x]; % m x 3 Y = q1y; % m x 1 itheta = zeros(n+1,1); % 3 x 1 [theta, ll] = newton(itheta,X,Y); figure; hold on; x = min(X(:,2)):.01:max(X(:,2)); 
- 
        Filipe Martins revised this gist Jan 22, 2016 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -36,7 +36,6 @@ Y = q1y; % m x 1 itheta = zeros(n+1,1); % (n+1) x 1 [theta, ll] = newton(itheta,X,Y); figure; hold on; x = min(X(:,2)):.01:max(X(:,2)); y = -theta(2)/theta(3)*x-theta(1)/theta(3); 
- 
        Filipe Martins created this gist Jan 22, 2016 .There are no files selected for viewingThis file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,53 @@ %%%%% sigmoid.m %%%%% function f = sigmoid(z) f = 1 ./ (1 + exp(-z)); end %%%%%%%%%%%%%%%%%%%%%%%%% %%%%% likelihood.m %%%%% function L = likelihood(theta,X,Y) h = sigmoid(X * theta); % m x 1 L = (Y' * log(h)) + ((1 - Y)' * log(1 - h)); end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% newton.m %%%%% function [theta,ll] = newton(itheta,X,Y) theta = itheta; ll(1) = likelihood(itheta,X,Y); max_iter = 50; for i=1:max_iter h = sigmoid(X * theta); % m x 1 grad = (Y - h)' * X; % 1 x n hh = h .* (1 - h); % m x 1 H = (-1 .* (hh .* X))' * X; % n x n theta = theta - inv(H) * grad'; % n x 1 ll(i+1) = likelihood(theta,X,Y); end res = theta; end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%% solve.m %%%%% load('q1x.dat'); % m x 2 load('q1y.dat'); % m x 1 [m n] = size(q1x); X = [ones(m,1) q1x]; % m x (n+1) Y = q1y; % m x 1 itheta = zeros(n+1,1); % (n+1) x 1 [theta, ll] = newton(itheta,X,Y); % y = x * (-theta(2)/theta(3) + (-theta(1)/theta(3)) figure; hold on; x = min(X(:,2)):.01:max(X(:,2)); y = -theta(2)/theta(3)*x-theta(1)/theta(3); plot(x,y,'m'); x1y0 = X(find(Y == 0),2); x2y0 = X(find(Y == 0),3); x1y1 = X(find(Y == 1),2); x2y1 = X(find(Y == 1),3); plot(x1y0,x2y0,'rx'); plot(x1y1,x2y1,'go'); xlabel('x1'); ylabel('x2'); legend('decision boundary'); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
 Filipe Martins
              revised
            
            this gist
            
              Filipe Martins
              revised
            
            this gist