Advertisement
Guest User

Untitled

a guest
May 19th, 2013
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.67 KB | None | 0 0
  1. % Create a grid. Set granularity to 100/1000, for low/high quality plots.
  2. granularity = 1000;
  3. a = -5;
  4. b = 5;
  5. domain = linspace (a, b, granularity);
  6. [X,Y] = meshgrid (domain, domain);
  7. x = X(:);
  8. y = Y(:);
  9. n = size(X,1);
  10.  
  11. % Generate 2D data from normal distributions.
  12. mu = [-1 2.5;
  13.       1 -2.5];
  14. sig = [0.5 0; 0 0.5];
  15. points_per_class = 20;
  16. X_data = [mvnrnd(mu(1,:), sig, points_per_class);          
  17.           mvnrnd(mu(2,:), sig, points_per_class);];
  18.      
  19. % Prepare the training input.
  20. X_train = [ones(1,size(X_data,1)); X_data'];
  21. w = [zeros(points_per_class,1); ones(points_per_class,1)];
  22. var_prior = 6;
  23. X_test = [ones(1,granularity*granularity); x'; y'];
  24.  
  25. % Fit a logistic regression model.
  26. initial_phi = [1; 1; 1];
  27. [predictions, phi] = fit_logr (X_train, w, var_prior, X_test, initial_phi);
  28.  
  29. % Plot the results.
  30. figure;
  31. Z = reshape(predictions,n,n);
  32. pcolor(X,Y,Z);
  33. shading interp;
  34. colormap(hot(4096));
  35. set(gca,'YDir','normal');
  36. axis([a,b,a,b]);
  37. set(gca,'YTick',[a,0,b], 'XTick',[a,0,b], 'FontSize', 16);
  38. xlabel('x', 'FontSize', 16);
  39. ylabel('y', 'FontSize', 16);
  40. title('(b)', 'FontSize', 16);
  41.  
  42. % Plot the data points on top.
  43. hold on;
  44. selector = 1:points_per_class;
  45. scatter(X_data(selector,1), X_data(selector,2), 100, 'fill',...
  46.        'MarkerEdgeColor','k','MarkerFaceColor','b','LineWidth',1);
  47.  
  48. hold on;
  49. selector = points_per_class+1 : 2*points_per_class;
  50. scatter(X_data(selector,1), X_data(selector,2), 100, 'fill',...
  51.        'MarkerEdgeColor','k','MarkerFaceColor','g','LineWidth',1);
  52.  
  53. hold on;
  54. decision_boundary = -(phi(1) + phi(2)*domain) / phi(3);
  55. plot(domain,decision_boundary,'LineWidth',2,'LineSmoothing','on',...
  56.     'Color','c');
  57. hold off;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement