Nearest-neighbor graphs

Building graphs from the Iris dataset using \(k\)-nearest neighbors.

[1]:
from IPython.display import SVG
[2]:
import pickle
[3]:
from sknetwork.embedding import Spring
from sknetwork.utils import KNNDense, CNNDense
from sknetwork.visualization import svg_graph

Nearest neighbors

[4]:
iris = pickle.load(open('iris.p', 'rb'))
[5]:
data = iris['data']
[6]:
labels = iris['labels']
[7]:
knn = KNNDense(n_neighbors=3, undirected=True)
adjacency = knn.fit_transform(data)
[8]:
image = svg_graph(adjacency, labels=labels, display_edge_weight=False)
[9]:
SVG(image)
[9]:
../../_images/tutorials_data_knn_11_0.svg

Component-wise nearest neighbors

Here the \(k\) nearest neighbors are searched per component. Thus if data has dimension \(N\), there are at most \(k\times N\) nearest neighbors per sample.

[10]:
cnn = CNNDense(n_neighbors=2, undirected=True)
adjacency = cnn.fit_transform(data)
[11]:
image = svg_graph(adjacency, labels=labels, display_edge_weight=False)
[12]:
SVG(image)
[12]:
../../_images/tutorials_data_knn_15_0.svg