Finds the neighbors within a given radius of a point or points.
Return the indices and distances of each point from the dataset lying in a ball with size ``radius`` around the points of the query array. Points lying on the boundary are included in the results.
The result points are *not* necessarily sorted by distance to their query point.
Parameters ---------- X : array-like, (n_samples, n_features), optional The query point or points. If not provided, neighbors of each indexed point are returned. In this case, the query point is not considered its own neighbor.
radius : float Limiting distance of neighbors to return. (default is the value passed to the constructor).
return_distance : boolean, optional. Defaults to True. If False, distances will not be returned.
sort_results : boolean, optional. Defaults to False. If True, the distances and indices will be sorted before being returned. If False, the results will not be sorted. If return_distance == False, setting sort_results = True will result in an error.
.. versionadded:: 0.22
Returns ------- neigh_dist : array, shape (n_samples,) of arrays Array representing the distances to each point, only present if return_distance=True. The distance values are computed according to the ``metric`` constructor parameter.
neigh_ind : array, shape (n_samples,) of arrays An array of arrays of indices of the approximate nearest points from the population matrix that lie within a ball of size ``radius`` around the query points.
Examples -------- In the following example, we construct a NeighborsClassifier class from an array representing our data set and ask who's the closest point to 1, 1, 1
:
>>> import numpy as np >>> samples = [0., 0., 0.], [0., .5, 0.], [1., 1., .5]
>>> from sklearn.neighbors import NearestNeighbors >>> neigh = NearestNeighbors(radius=1.6) >>> neigh.fit(samples) NearestNeighbors(radius=1.6) >>> rng = neigh.radius_neighbors([1., 1., 1.]
) >>> print(np.asarray(rng0
0
)) 1.5 0.5
>>> print(np.asarray(rng1
0
)) 1 2
The first array returned contains the distances to all points which are closer than 1.6, while the second array returned contains their indices. In general, multiple points can be queried at the same time.
Notes ----- Because the number of neighbors of each point is not necessarily equal, the results for multiple query points cannot be fit in a standard data array. For efficiency, `radius_neighbors` returns arrays of objects, where each object is a 1D array of indices or distances.