k-Nearest Neighbor demo

This java applet lets you experiment with kNN classification.  Follow step 1 through 3, fill in the numbers and push the buttons.


  1. This is a binary classification problem defined on a N by N field.  You can specify N, the 'field size', in step 1. (This gives a 'grid  size' - if the grid is too small, like 1 or 2, you won't be able to see it!)
  2. Once you pick an N, you need to assign a true classification to each point in the field.  The applet does this for you automatically by creating a random assignment with certain complexity.  All you have to do is to specify 'complexity' and press the 'Create Truth' button in step 1. (the applet actually performs 'complexity' times of random half-plane XOR operations)  The true classification map will be displayed on the lower left.
  3. Now you can generate random samples.  Specify the number of 'samples' and press 'Generate Samples' in step 2.  The applet uniformly select the specified number of points as samples. (Note: our sampling is noise-free)  You can see the samples as circles and crosses on the true classification map.
  4. Finally you can perform kNN classification for each point in the field, given the samples as training data.  Specify 'kNN', the number of nearest neighbors to consider, and press 'Classify' in step 3.  The classification result map will be displayed on the lower right.  The true error will be displayed to the right of the ''Classify' button.  (Note: if you specified an even number, there will be ties.  We break ties randomly.)
  5. Each step can be repeated individually with different parameters.  For example, you can specify different k in step 3 while keeping the true classification and samples fixed.  Or you can compare different sample sizes in step 2.