LFD Book Forum  

Go Back   LFD Book Forum > Course Discussions > Online LFD course > Homework 1

Reply
 
Thread Tools Display Modes
  #1  
Old 01-11-2013, 07:03 AM
vakhov vakhov is offline
Junior Member
 
Join Date: Jan 2013
Location: Russia, Moscow
Posts: 2
Default PLA: Let share nice visualizations

Hi!

I think all of us plotted different pictures. Some of them are very nice. For example I get this for PLA learning on N=20. What nice visualisations do you have?

Reply With Quote
  #2  
Old 01-11-2013, 09:00 AM
Yann222 Yann222 is offline
Member
 
Join Date: Jan 2013
Location: SoCal
Posts: 16
Default Re: PLA: Let share nice visualizations

I don't think it's sharing answers to homework if we exchange code for plotting. Am new to Octave and not having the best luck getting nice-looking plots so far. Would be thrilled to get just the snippet of code people use for visualizing different solutions.
Reply With Quote
  #3  
Old 01-11-2013, 11:40 AM
Anne Paulson Anne Paulson is offline
Senior Member
 
Join Date: Jan 2013
Location: Silicon Valley
Posts: 52
Default Re: PLA: Let share nice visualizations

I'm just going to paste the Octave code right here. Hope that's OK.

This is code to display the current state of the perceptron, displaying the true separating line, the current hypothesis and all the points. Wrongly classified points are big; rightly classified points are little. Optionally, you can specify the new hypothesis and the currently selected misclassified point, and then they too will be displayed.

When I call this function, I put a pause statement afterwards, so I can step through the algorithm watching it move the hypothesis line and reclassify all the points.

For some reason I'm getting a divide by zero warning for the statements with && in them. I ignore it.

Code:
function draw_percept(points,hypothesis, predictions, true_line, target, bad, new_h)

# points (number of points, weights) is the data points. the bias weight is LAST
# hypothesis is the hypothesis. bias term is LAST. 
# predictions(1, N) is what the hypothesis predicts for the points. 
# true_line is the true separating line. bias term is LAST
# target is the correct classifications for the points
# bad is optional; it's the misclassified point
# new_h is optional; it's the new hypothesis

   [hypx hypy] = pts_from_hypothesis(hypothesis);
   [truex truey] = pts_from_hypothesis(true_line);
   
   rightly_on = find(predictions == target & predictions == 1);
   wrongly_on = find(predictions != target & predictions ==1);
   rightly_off = find(predictions == target & predictions == -1);
   wrongly_off = find(predictions!= target & predictions == -1);


   hold off;
   # if we are displaying old & new hypotheses, 
   # display true line, old hypothesis, new hypothesis
   if nargin >6
      [newx newy] = pts_from_hypothesis(new_h);
      plot(hypx, hypy, 'r', truex, truey,'g',newx, newy,':b');
      legend("old hypothesis red", "true line green", "new hypothesis blue", "location", "southeast");
   else   
       # otherwise plot the hypothesis & real line
       plot(hypx, hypy, 'r', truex, truey,'g');
       legend("old hypothesis", "new hypothesis", "location", "southeast");
   end
   
   axis([-1 1 -1 1]);

   hold on;
   title("Perceptron");

   # plot the points
   scatter(points(rightly_on, 1),points(rightly_on, 2), 'g');
   hold on;
   scatter(points(rightly_off, 1),points(rightly_off, 2), 'b');
   hold on;
   scatter(points(wrongly_on, 1),points(wrongly_on, 2),15, 'r');
   hold on;
   scatter(points(wrongly_off, 1),points(wrongly_off, 2),15,  'c'); 

   # if we're displaying the selected bad point, display it
   if nargin >3
      hold on;
      scatter(bad(1), bad(2), 20, 'b');
   end
end


function [xs ys] = pts_from_hypothesis(hypothesis)
# hypothesis is a, b, c where line is ax + by + c> 0

# so y = -(a/b)x - c/b
# and x =-(b/a)y - c/a

# we need to find the the intercepts for y=-1, x=-1, y=1 and
# x = 1. Two of those points will be in the [-1,1],[-1,1] box
  

  a = hypothesis(1); 
  b = hypothesis(2);
  c= hypothesis(3); 
  xm1int = (b-c)/a;
  ym1int = (a-c)/b;
  x1int = (-b-c)/a;
  y1int = (-a-c)/b;

  xs = [-1 1 xm1int x1int];
  ys = [ym1int y1int -1 1];
  
  
end
Reply With Quote
  #4  
Old 01-11-2013, 12:11 PM
chocobo_rider chocobo_rider is offline
Junior Member
 
Join Date: Jan 2013
Posts: 7
Default Re: PLA: Let share nice visualizations

Wow, nice figure. I didn't visualize it like that. I chose to make an animation of the process
Reply With Quote
  #5  
Old 01-14-2013, 10:31 PM
vikram360 vikram360 is offline
Junior Member
 
Join Date: Apr 2012
Posts: 5
Default Re: PLA: Let share nice visualizations

Please, good sir, do share it
Reply With Quote
  #6  
Old 01-15-2013, 10:14 AM
theTB theTB is offline
Junior Member
 
Join Date: Jan 2013
Posts: 1
Default Re: PLA: Let share nice visualizations

Here is a visualization of the algorithm with n=50 points. The point in the circle is the chosen misclassified point in the current iteration. The final yellow line is the selected hypothesis.


Reply With Quote
  #7  
Old 01-15-2013, 10:19 AM
yaser's Avatar
yaser yaser is offline
Caltech
 
Join Date: Aug 2009
Location: Pasadena, California, USA
Posts: 1,477
Default Re: PLA: Let share nice visualizations

Quote:
Originally Posted by theTB View Post
Here is a visualization of the algorithm with n=50 points.
Cool.
__________________
Where everyone thinks alike, no one thinks very much
Reply With Quote
  #8  
Old 01-15-2013, 02:28 PM
gah44 gah44 is offline
Invited Guest
 
Join Date: Jul 2012
Location: Seattle, WA
Posts: 153
Default Re: PLA: Let share nice visualizations

I never did any plots.

I did a few drawings with a few points on them to understand the geometry, and once I understood that, believed in the equations.

But sometimes plots are nice to see what it is really doing.
Reply With Quote
  #9  
Old 01-17-2013, 08:26 AM
Yann222 Yann222 is offline
Member
 
Join Date: Jan 2013
Location: SoCal
Posts: 16
Default Re: PLA: Let share nice visualizations

Thanks Anne for sharing your code. Helped me pick up a few things that I was able to use to produce my own plots.
Reply With Quote
  #10  
Old 01-17-2013, 10:05 AM
Barath Barath is offline
Member
 
Join Date: Jan 2013
Posts: 10
Default Re: PLA: Let share nice visualizations

Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:22 AM.


Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
The contents of this forum are to be used ONLY by readers of the Learning From Data book by Yaser S. Abu-Mostafa, Malik Magdon-Ismail, and Hsuan-Tien Lin, and participants in the Learning From Data MOOC by Yaser S. Abu-Mostafa. No part of these contents is to be communicated or made accessible to ANY other person or entity.