k-Nearest Neighbor demo
This java applet lets you experiment with kNN
Follow step 1 through 3, fill in the numbers and push the
- This is a binary classification problem defined on a N by N
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!)
- Once you pick an N, you need to assign a true classification to
in the field. The applet does this for you automatically by
a random assignment with certain complexity. All you have to do
to specify 'complexity' and press the 'Create Truth'
button in step 1. (the applet actually performs 'complexity' times of
half-plane XOR operations) The true classification map will be
on the lower left.
- Now you can generate random samples. Specify the number of 'samples'
and press 'Generate Samples' in step 2. The applet
select the specified number of points as samples. (Note: our sampling
noise-free) You can see the samples as circles and crosses on the
true classification map.
- Finally you can perform kNN classification for each point in the
given the samples as training data. Specify 'kNN', the
of nearest neighbors to consider, and press 'Classify' in step
The classification result map will be displayed on the lower
The true error will be displayed to the right of the ''Classify'
(Note: if you specified an even number, there will be ties. We
- Each step can be repeated individually with different
For example, you can specify different k in step 3 while
the true classification and samples fixed. Or you can compare
sample sizes in step 2.