February 11, 2012
Visualizing k-nearest neighbors with Gephi
Tags: GUC  Machine learning  Gephi  

In the course IMT4612 Machine Learning and Pattern Recognition 1 at GUC we were given an assignment to classify some objects into one of two classes using the k-nearest neighbors algorithm. We were given a training set consisting of 420 examples with class labels. The the first value of each sample was the class label and the rest was attributes:

...
1 0.500000 0.250000 0.000000 0.250000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.250000
2 0.666667 0.666667 0.000000 0.000000 0.000000 0.333333 0.000000 0.000000 0.000000 0.000000 0.000000
1 0.000000 0.000000 0.000000 0.333333 0.333333 0.666667 1.000000 0.000000 0.000000 0.000000 0.000000 ...

I wanted to visualize the training set in Gephi, so I wrote a small plugin that added an edge to each of the k-nearest neighbors for every sample in the training set. The process that added the edges had no knowledge of the class lables. I must admit that I consider the result to be pretty awsome!

Using k=3:

The color of a node indicates its class label. Below are examples using other k-values.

k = 0

k = 1

k = 30