Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- clear all; close all; clc;
- alpha = 0.01;
- num_iters = 1000;
- %% Plotting data
- x1 = linspace(0,3,50);
- mqtrue = 5;
- cqtrue = 30;
- dat1 = mqtrue*x1+5*randn(1,50);
- x2 = linspace(7,10,50);
- dat2 = mqtrue*x2 + (cqtrue + 5*randn(1,50));
- x = [x1 x2]'; % X
- subplot(2,2,1);
- dat = [dat1 dat2]'; % Y
- scatter(x1, dat1); hold on;
- scatter(x2, dat2, '*'); hold on;
- classdata = (dat>40);
- % Setup the data matrix appropriately, and add ones for the intercept term
- [m, n] = size(x);
- % Add intercept term to x and X_test
- x = [ones(m, 1) x];
- % Initialize fitting parameters
- theta = zeros(n + 1, 1);
- %initial_theta = [0.2; 0.2];
- J_history = zeros(num_iters, 1);
- plot_x = [min(x(:,2))-2, max(x(:,2))+2]
- for iter = 1:num_iters
- % Compute and display initial cost and gradient
- [cost, grad] = logistic_costFunction(theta, x, classdata);
- theta = theta - alpha * grad;
- J_history(iter) = cost;
- fprintf('Iteration #%d - Cost = %d... rn',iter, cost);
- subplot(2,2,2);
- hold on; grid on;
- plot(iter, J_history(iter), '.r'); title(sprintf('Plot of cost against number of iterations. Cost is %g',J_history(iter)));
- xlabel('Iterations')
- ylabel('MSE')
- drawnow
- subplot(2,2,3);
- grid on;
- plot3(theta(1), theta(2), J_history(iter),'o')
- title(sprintf('Tita0 = %g, Tita1=%g', theta(1), theta(2)))
- xlabel('Tita0')
- ylabel('Tita1')
- zlabel('Cost')
- hold on;
- drawnow
- subplot(2,2,1);
- grid on;
- % Calculate the decision boundary line
- plot_y = theta(2).*plot_x + theta(1); % <--- Boundary line
- % Plot, and adjust axes for better viewing
- plot(plot_x, plot_y)
- hold on;
- drawnow
- end
- fprintf('Cost at initial theta (zeros): %fn', cost);
- fprintf('Gradient at initial theta (zeros): n');
- fprintf(' %f n', grad);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement