Step 7. Perceptron

This program implements a Perceptron finding a line to separate two groups of points.

Note the video has a screenshot of the exercise as it appears in an M.Sc. course. You do not have access to this course. See the exercise below instead.



Perceptron to separate points.


  • On each run, there is a set of points that is linearly separable.
  • We achieve this by defining a random (blue) line on each run.
    Points above the line (green) are one class (output should be 1).
    Points below the line (pink) are another class (output should be -1).

  • Starts with random perceptron, i.e. random (white) line.
  • Perceptron learns by getting output wrong to change until all outputs are right.


  • Port of Perceptron from here from "The Nature of Code" (by the "Coding Train").
  • See videos parts one and two.


Clone and Edit the World.
  1. Change the number of points.
  2. Change the line definition.
  3. Change the Learning Constant. Make it very large or very small. What happens?

  4. Q. Why does it stop before perfectly matching the line?

  5. Change the code to detect when it has reached error zero on all points and stop.
  6. Change learning rate to start high and decline.

Tweet this step: