Package pyvision :: Package ml :: Module knn :: Class KNearestNeighbors
[hide private]
[frames] | no frames]

Class KNearestNeighbors

source code

Basic k nearest neighbors algorithm.

Based on the scipy.spatial.kdtree interface written by Anne M. Archibald 2008.

This class performs a search over a set of D dimensional points and returns the k points that are nearest to a given point.

This class supports by default Minkowski p-norm distance measures and also cosine angle, and correlation similarity measures. The class also supports the uses of user defined distance and similarity measures.

Instance Methods [hide private]
 
__init__(self, points, p=2, is_distance=True)
Construct a nearest neighbor algorithm.
source code
 
query(self, x, k=1, p=None, is_distance=True)
Query the instance for nearest neighbors
source code
Method Details [hide private]

__init__(self, points, p=2, is_distance=True)
(Constructor)

source code 

Construct a nearest neighbor algorithm.

Parameters:
  • data - array of n points with dimensionality d, shape (n,d). The data points to be indexed. This array is not copied, and so modifying this data will result in bogus results.
  • p - float, 1<=p<=infinity 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 Also accepts the keywords "Manhattan", "Euclidean", and "Correlation", or p can also be a user defined function with will compute a distance matrix between two sets of points.
  • is_distance - True or False. Determines if a user defined function is treated as a distance (smaller is better) or a similarity (larger values are better).

query(self, x, k=1, p=None, is_distance=True)

source code 

Query the instance for nearest neighbors

Parameters:
  • x - array-like, last dimension self.k An array of points to query.
  • k - integer The number of nearest neighbors to return.
  • p - float, 1<=p<=infinity 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 Also accepts the keywords "Manhattan", "Euclidean", and "Correlation", or p can also be a user defined function with will compute a distance matrix between two sets of points.
Returns:
a tuple (distances, indexes)