NearestNeighbourAlgorithm¶

class
NearestNeighbourAlgorithm
(*args)¶ Nearest neighbour lookup.
Base class to define an algorithm to search for nearest neighbours of a list of points.
 Available constructors:
 NearestNeighbourAlgorithm(sample)
Parameters:  sample :
Sample
Input points.
Notes
Two algorithms can be selected in any dimension:
NaiveNearestNeighbour
loops over all points of the sample to find the closest one.KDTree
builds a binary tree.
Two algorithms are specific to 1D spatial dimension, and much more efficient:
RegularGridNearestNeighbour
is the most efficient algorithm when points corresponds to aRegularGrid
,query()
works in constant time.NearestNeighbour1D
looks for nearest neighbour by dichotomy in 1D.
It is recommended to use derived classes in order to select the best algorithm according to your data. If you create a generic
NearestNeighbourAlgorithm
, here is how the derived class is selected: If points correspond to a
RegularGrid
,RegularGridNearestNeighbour
algorithm is selected.  If spatial dimension is 1,
NearestNeighbour1D
is selected.  Otherwise,
KDTree
is selected.
Examples
>>> import openturns as ot >>> sample = ot.Normal(2).getSample(10) >>> finder = ot.NearestNeighbourAlgorithm(sample) >>> neighbour = sample[finder.query([0.1, 0.2])]
Methods
getClassName
()Accessor to the object’s name. getId
()Accessor to the object’s id. getImplementation
(*args)Accessor to the underlying implementation. getName
()Accessor to the object’s name. getSample
()Get the points which have been used to build this nearest neighbour algorithm. query
(*args)Get the index of the nearest neighbour of the given point. queryK
(x, k[, sorted])Get the indices of nearest neighbours of the given point. setName
(name)Accessor to the object’s name. setSample
(sample)Build a NearestNeighbourAlgorithm from these points. 
__init__
(*args)¶ Initialize self. See help(type(self)) for accurate signature.

getClassName
()¶ Accessor to the object’s name.
Returns:  class_name : str
The object class name (object.__class__.__name__).

getId
()¶ Accessor to the object’s id.
Returns:  id : int
Internal unique identifier.

getImplementation
(*args)¶ Accessor to the underlying implementation.
Returns:  impl : Implementation
The implementation class.

getName
()¶ Accessor to the object’s name.
Returns:  name : str
The name of the object.

getSample
()¶ Get the points which have been used to build this nearest neighbour algorithm.
Returns:  sample :
Sample
Input points.
 sample :

query
(*args)¶ Get the index of the nearest neighbour of the given point.
 Available usages:
query(point)
query(sample)
Parameters:  point : sequence of float
Given point.
 sample : 2d sequence of float
Given points.
Returns:  index : int
Index of the nearest neighbour of the given point.
 indices :
openturns.Indices
Index of the nearest neighbour of the given points.

queryK
(x, k, sorted=False)¶ Get the indices of nearest neighbours of the given point.
Parameters:  x : sequence of float
Given point.
 k : int
Number of indices to return.
 sorted : bool, optional
Boolean to tell whether returned indices are sorted according to the distance to the given point.
Returns:  indices : sequence of int
Indices of the k nearest neighbours of the given point.

setName
(name)¶ Accessor to the object’s name.
Parameters:  name : str
The name of the object.