package scipy

  1. Overview
  2. Docs
Legend:
Library
Module
Module type
Parameter
Class
Class type
type tag = [
  1. | `KDTree
]
type t = [ `KDTree | `Object ] Obj.t
val of_pyobject : Py.Object.t -> t
val to_pyobject : [> tag ] Obj.t -> Py.Object.t
val create : ?leafsize:int -> data:Py.Object.t -> unit -> t

kd-tree for quick nearest-neighbor lookup

This class provides an index into a set of k-dimensional points which can be used to rapidly look up the nearest neighbors of any point.

Parameters ---------- data : (N,K) array_like The data points to be indexed. This array is not copied, and so modifying this data will result in bogus results. leafsize : int, optional The number of points at which the algorithm switches over to brute-force. Has to be positive.

Raises ------ RuntimeError The maximum recursion limit can be exceeded for large data sets. If this happens, either increase the value for the `leafsize` parameter or increase the recursion limit by::

>>> import sys >>> sys.setrecursionlimit(10000)

See Also -------- cKDTree : Implementation of `KDTree` in Cython

Notes ----- The algorithm used is described in Maneewongvatana and Mount 1999. The general idea is that the kd-tree is a binary tree, each of whose nodes represents an axis-aligned hyperrectangle. Each node specifies an axis and splits the set of points based on whether their coordinate along that axis is greater than or less than a particular value.

During construction, the axis and splitting point are chosen by the 'sliding midpoint' rule, which ensures that the cells do not all become long and thin.

The tree can be queried for the r closest neighbors of any given point (optionally returning only those within some maximum distance of the point). It can also be queried, with a substantial gain in efficiency, for the r approximate closest neighbors.

For large dimensions (20 is already large) do not expect this to run significantly faster than brute force. High-dimensional nearest-neighbor queries are a substantial open problem in computer science.

The tree also supports all-neighbors queries, both with arrays of points and with other kd-trees. These do use a reasonably efficient algorithm, but the kd-tree is not necessarily the best data structure for this sort of calculation.

val count_neighbors : ?p:[ `T1_p_infinity of Py.Object.t | `F of float ] -> other:Py.Object.t -> r:[ `Ndarray of [> `Ndarray ] Np.Obj.t | `F of float ] -> [> tag ] Obj.t -> Py.Object.t

Count how many nearby pairs can be formed.

Count the number of pairs (x1,x2) can be formed, with x1 drawn from self and x2 drawn from ``other``, and where ``distance(x1, x2, p) <= r``. This is the 'two-point correlation' described in Gray and Moore 2000, 'N-body problems in statistical learning', and the code here is based on their algorithm.

Parameters ---------- other : KDTree instance The other tree to draw points from. r : float or one-dimensional array of floats The radius to produce a count for. Multiple radii are searched with a single tree traversal. p : float, 1<=p<=infinity, optional Which Minkowski p-norm to use

Returns ------- result : int or 1-D array of ints The number of pairs. Note that this is internally stored in a numpy int, and so may overflow if very large (2e9).

val query : ?k:int -> ?eps:Py.Object.t -> ?p:[ `T1_p_infinity of Py.Object.t | `F of float ] -> ?distance_upper_bound:Py.Object.t -> x: [ `Ndarray of [> `Ndarray ] Np.Obj.t | `Last_dimension_self_m of Py.Object.t ] -> [> tag ] Obj.t -> Py.Object.t * Py.Object.t

Query the kd-tree for nearest neighbors

Parameters ---------- x : array_like, last dimension self.m An array of points to query. k : int, optional The number of nearest neighbors to return. eps : nonnegative float, optional Return approximate nearest neighbors; the kth returned value is guaranteed to be no further than (1+eps) times the distance to the real kth nearest neighbor. p : float, 1<=p<=infinity, optional Which Minkowski p-norm to use. 1 is the sum-of-absolute-values 'Manhattan' distance 2 is the usual Euclidean distance infinity is the maximum-coordinate-difference distance distance_upper_bound : nonnegative float, optional Return only neighbors within this distance. This is used to prune tree searches, so if you are doing a series of nearest-neighbor queries, it may help to supply the distance to the nearest neighbor of the most recent point.

Returns ------- d : float or array of floats The distances to the nearest neighbors. If x has shape tuple+(self.m,), then d has shape tuple if k is one, or tuple+(k,) if k is larger than one. Missing neighbors (e.g. when k > n or distance_upper_bound is given) are indicated with infinite distances. If k is None, then d is an object array of shape tuple, containing lists of distances. In either case the hits are sorted by distance (nearest first). i : integer or array of integers The locations of the neighbors in self.data. i is the same shape as d.

Examples -------- >>> from scipy import spatial >>> x, y = np.mgrid0:5, 2:8 >>> tree = spatial.KDTree(list(zip(x.ravel(), y.ravel()))) >>> tree.data array([0, 2], [0, 3], [0, 4], [0, 5], [0, 6], [0, 7], [1, 2], [1, 3], [1, 4], [1, 5], [1, 6], [1, 7], [2, 2], [2, 3], [2, 4], [2, 5], [2, 6], [2, 7], [3, 2], [3, 3], [3, 4], [3, 5], [3, 6], [3, 7], [4, 2], [4, 3], [4, 4], [4, 5], [4, 6], [4, 7]) >>> pts = np.array([0, 0], [2.1, 2.9]) >>> tree.query(pts) (array( 2. , 0.14142136), array( 0, 13)) >>> tree.query(pts0) (2.0, 0)

val query_ball_point : ?p:float -> ?eps:Py.Object.t -> x: [ `Ndarray of [> `Ndarray ] Np.Obj.t | `Shape_tuple_self_m_ of Py.Object.t ] -> r:float -> [> tag ] Obj.t -> Py.Object.t

Find all points within distance r of point(s) x.

Parameters ---------- x : array_like, shape tuple + (self.m,) The point or points to search for neighbors of. r : positive float The radius of points to return. p : float, optional Which Minkowski p-norm to use. Should be in the range 1, inf. eps : nonnegative float, optional Approximate search. Branches of the tree are not explored if their nearest points are further than ``r / (1 + eps)``, and branches are added in bulk if their furthest points are nearer than ``r * (1 + eps)``.

Returns ------- results : list or array of lists If `x` is a single point, returns a list of the indices of the neighbors of `x`. If `x` is an array of points, returns an object array of shape tuple containing lists of neighbors.

Notes ----- If you have many points whose neighbors you want to find, you may save substantial amounts of time by putting them in a KDTree and using query_ball_tree.

Examples -------- >>> from scipy import spatial >>> x, y = np.mgrid0:5, 0:5 >>> points = np.c_x.ravel(), y.ravel() >>> tree = spatial.KDTree(points) >>> tree.query_ball_point(2, 0, 1) 5, 10, 11, 15

Query multiple points and plot the results:

>>> import matplotlib.pyplot as plt >>> points = np.asarray(points) >>> plt.plot(points:,0, points:,1, '.') >>> for results in tree.query_ball_point((2, 0, 3, 3), 1): ... nearby_points = pointsresults ... plt.plot(nearby_points:,0, nearby_points:,1, 'o') >>> plt.margins(0.1, 0.1) >>> plt.show()

val query_ball_tree : ?p:float -> ?eps:float -> other:Py.Object.t -> r:float -> [> tag ] Obj.t -> Py.Object.t

Find all pairs of points whose distance is at most r

Parameters ---------- other : KDTree instance The tree containing points to search against. r : float The maximum distance, has to be positive. p : float, optional Which Minkowski norm to use. `p` has to meet the condition ``1 <= p <= infinity``. eps : float, optional Approximate search. Branches of the tree are not explored if their nearest points are further than ``r/(1+eps)``, and branches are added in bulk if their furthest points are nearer than ``r * (1+eps)``. `eps` has to be non-negative.

Returns ------- results : list of lists For each element ``self.datai`` of this tree, ``resultsi`` is a list of the indices of its neighbors in ``other.data``.

val query_pairs : ?p:float -> ?eps:float -> r:float -> [> tag ] Obj.t -> Py.Object.t

Find all pairs of points within a distance.

Parameters ---------- r : positive float The maximum distance. p : float, optional Which Minkowski norm to use. `p` has to meet the condition ``1 <= p <= infinity``. eps : float, optional Approximate search. Branches of the tree are not explored if their nearest points are further than ``r/(1+eps)``, and branches are added in bulk if their furthest points are nearer than ``r * (1+eps)``. `eps` has to be non-negative.

Returns ------- results : set Set of pairs ``(i,j)``, with ``i < j``, for which the corresponding positions are close.

val sparse_distance_matrix : ?p:float -> other:Py.Object.t -> max_distance:float -> [> tag ] Obj.t -> Py.Object.t

Compute a sparse distance matrix

Computes a distance matrix between two KDTrees, leaving as zero any distance greater than max_distance.

Parameters ---------- other : KDTree

max_distance : positive float

p : float, optional

Returns ------- result : dok_matrix Sparse matrix representing the results in 'dictionary of keys' format.

val to_string : t -> string

Print the object to a human-readable representation.

val show : t -> string

Print the object to a human-readable representation.

val pp : Format.formatter -> t -> unit

Pretty-print the object to a formatter.

OCaml

Innovation. Community. Security.