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:
observationssequence

Sequence of observations.

n_neighboursint

Maximum number of neighbours for each sample.

metricfunction

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_paramsdict, optional (default = None)

Additional keyword arguments for the metric function.

Returns:
mkneighbors_graphndarray

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.