Previous topic

Neighbours (medpy.neighbours)

Next topic

medpy.neighbours.knn.pdist

This Page

medpy.neighbours.knn.mkneighbors_graph

medpy.neighbours.knn.mkneighbors_graph(observations, n_neighbours, metric, mode='connectivity', metric_params=None)[source]

Computes the (weighted) graph of mutual k-Neighbors for observations.

Parameters:

observations : sequence

Sequence of observations.

n_neighbours : int

Maximum number of neighbours for each sample.

metric : function

The distance metric taking two observations and returning a numeric value > 0.

mode : {‘connectivity’, ‘distance’, ‘both’}, optional

Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, in ‘distance’ the edges are distances between points, while ‘both’ returns a (connectivity, distance) tuple.

metric_params : dict, optional (default = None)

Additional keyword arguments for the metric function.

Returns:

mkneighbors_graph : ndarray

Sparse matrix in CSR format, shape = [n_observations, n_observations]. mkneighbors_graph[i, j] is assigned the weight of edge that connects i to j. Might contain numpy.inf values.

Notes

The distance between an observation and itself is never computed and instead set to numpy.inf. I.e. only in the case of k>=n_observations or when the metric returns numpy.inf, the returned graph can contain loops.