Toggle Menu
Prev Up Next

scikit-learn 1.3.2
Other versions

Please cite us if you use the software.

# `sklearn.neighbors`.radius_neighbors_graph

sklearn.neighbors.radius_neighbors_graph(X, radius, *, mode='connectivity', metric='minkowski', p=2, metric_params=None, include_self=False, n_jobs=None)

  Compute the (weighted) graph of Neighbors for points in X.

  Neighborhoods are restricted the points at a distance lower than radius.

  Read more in the User Guide.

  Parameters:
    X array-like of shape (n_samples, n_features) or BallTree

      Sample data, in the form of a numpy array or a precomputed `BallTree`.

    radius float

      Radius of neighborhoods.

    mode{‘connectivity’, ‘distance’}, default=’connectivity’

      Type of returned matrix: ‘connectivity’ will return the connectivity matrix with ones and zeros, and ‘distance’ will return the distances between neighbors according to the given metric.

    metric str, default=’minkowski’

      Metric to use for distance computation. Default is “minkowski”, which results in the standard Euclidean distance when p = 2. See the documentation of scipy.spatial.distance and the metrics listed in `distance_metrics` for valid metric values.

    p float, default=2

      Power parameter for the Minkowski metric. When p = 1, this is equivalent to using manhattan_distance (l1), and euclidean_distance (l2) for p = 2. For arbitrary p, minkowski_distance (l_p) is used.

    metric_params dict, default=None

      Additional keyword arguments for the metric function.

    include_self bool or ‘auto’, default=False

      Whether or not to mark each sample as the first nearest neighbor to itself. If ‘auto’, then True is used for mode=’connectivity’ and False for mode=’distance’.

    n_jobs int, default=None

      The number of parallel jobs to run for neighbors search. `None` means 1 unless in a `joblib.parallel_backend` context. `-1` means using all processors. See Glossary for more details.

  Returns:
    A sparse matrix of shape (n_samples, n_samples)

      Graph where A[i, j] is assigned the weight of edge that connects i to j. The matrix is of CSR format.

  See also

  `kneighbors_graph`

    Compute the weighted graph of k-neighbors for points in X.

  Examples

      >>> X = [[0], [3], [1]]
      >>> from sklearn.neighbors import radius_neighbors_graph
      >>> A = radius_neighbors_graph(X, 1.5, mode='connectivity',
      ...                            include_self=True)
      >>> A.toarray()
      array([[1., 0., 1.],
             [0., 1., 0.],
             [1., 0., 1.]])