Warning: This documentation is for scikits.learn version 0.7.1. — Latest stable version

Contents

6.5.4. scikits.learn.neighbors.kneighbors_graph

scikits.learn.neighbors.kneighbors_graph(X, n_neighbors, mode='connectivity')

Computes the (weighted) graph of k-Neighbors for points in X

Parameters :

X : array-like, shape = [n_samples, n_features]

Coordinates of samples. One sample per row.

n_neighbors : int

Number of neighbors for each sample.

mode : {‘connectivity’, ‘distance’, ‘barycenter’}

Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are euclidian distance between points. In ‘barycenter’ they are barycenter weights estimated by solving a linear system for each point.

Returns :

A : CSR sparse matrix, shape = [n_samples, n_samples]

A[i,j] is assigned the weight of edge that connects i to j.

Examples

>>> X = [[0], [3], [1]]
>>> from scikits.learn.neighbors import kneighbors_graph
>>> A = kneighbors_graph(X, 2)
>>> A.todense()
matrix([[ 1.,  0.,  1.],
        [ 0.,  1.,  1.],
        [ 1.,  0.,  1.]])