LFD Book Forum

LFD Book Forum (http://book.caltech.edu/bookforum/index.php)
-   Homework 1 (http://book.caltech.edu/bookforum/forumdisplay.php?f=130)
-   -   PLA: Let share nice visualizations (http://book.caltech.edu/bookforum/showthread.php?t=3831)

vakhov 01-11-2013 08:03 AM

PLA: Let share nice visualizations

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?


Yann222 01-11-2013 10:00 AM

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.

Anne Paulson 01-11-2013 12:40 PM

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.


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");
      # otherwise plot the hypothesis & real line
      plot(hypx, hypy, 'r', truex, truey,'g');
      legend("old hypothesis", "new hypothesis", "location", "southeast");
  axis([-1 1 -1 1]);

  hold on;

  # 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');

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];

chocobo_rider 01-11-2013 01:11 PM

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

vikram360 01-14-2013 11:31 PM

Re: PLA: Let share nice visualizations
Please, good sir, do share it :)

theTB 01-15-2013 11:14 AM

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.

yaser 01-15-2013 11:19 AM

Re: PLA: Let share nice visualizations

Originally Posted by theTB (Post 8682)
Here is a visualization of the algorithm with n=50 points.

Cool. :)

gah44 01-15-2013 03:28 PM

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.

Yann222 01-17-2013 09:26 AM

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.

Barath 01-17-2013 11:05 AM

Re: PLA: Let share nice visualizations

All times are GMT -7. The time now is 10:45 PM.

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2022, 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.