Advertisement
ErVih

LinearRegression.m

May 20th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Octave 1.10 KB | None | 0 0
  1. %Remember to load X and y before running
  2.    
  3.   %init XX,yy
  4.     XX = X;
  5.     yy = y;
  6.    
  7.     m = size(XX,1);
  8.     n = size(XX,2);
  9.    
  10.   %Make it a sqrt function
  11.     XX(:,1) = sqrt(XX(:,1));  
  12.    
  13.   %Normalize X
  14.   %[XX, mu, sigma] = normalize(XX);
  15.    
  16.   %Add bias term
  17.     XX = [ones(m, 1), XX];
  18.    
  19.   %Linear regression
  20.     lambda = 0;
  21.    
  22.     costFunction = @(t) costGrad(XX,yy,t,lambda);
  23.    
  24.     options = optimset('MaxIter', 2000, 'GradObj', 'on');
  25.    
  26.     initial_theta = zeros(n+1,1);
  27.    
  28.     [theta, J] = fminunc(costFunction, initial_theta, options);
  29.    
  30.     theta
  31.    
  32. %Display the entire surface equation in an easily copy pastable form
  33.  
  34.     %no sqrt
  35.     %disp([num2str(theta(1)), " + ", num2str(theta(2)), " * x + ", num2str(theta(3)), " * y"]);
  36.    
  37.     %sqrt(x)
  38.     disp([num2str(theta(1)), " + ", num2str(theta(2)), " * sqrt(x) + ", num2str(theta(3)), " * y"]);
  39.    
  40.     %normalization
  41.     %disp([num2str(theta(1)), " + ", num2str(theta(2)), " * (x-", num2str(mu(1)), ")/", num2str(sigma(1)) ," + ", num2str(theta(3)), " * (y-", num2str(mu(2)), ")/", num2str(sigma(2))]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement