scikits.learn.neighbors.kneighbors_graph¶
- scikits.learn.neighbors.kneighbors_graph(X, n_neighbors, mode='connectivity', reg=0.001)¶
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’}, optional
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 the weights that best reconstruncts the point from its nearest neighbors.
reg : float, optional
Amount of regularization when solving the least-squares problem. Only relevant if mode=’barycenter’. If None, use the default.
Returns : A : sparse matrix in CSR format, 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.]])