Skip to content

NearestNeighbors

A basic data structure to hold nearest neighbors.

Parameters

  • n_neighbors (int) – defaults to 5

    Number of neighbors to use.

  • window_size (int) – defaults to 1000

    Size of the sliding window use to search neighbors with.

  • min_distance_keep (float) – defaults to 0.0

    The minimum distance (similarity) to consider adding a point to the window. E.g., a value of 0.0 will add even exact duplicates. Default is 0.05 to add similar but not exactly the same points.

  • distance_func (Callable[[Any, Any], float]) – defaults to None

    An required distance function that accept two input items to compare and optional parameters. It's recommended to use functools.partial.

Methods

append

Add a point to the window, optionally with extra metadata.

Parameters

  • item (Any)
  • extra ([typing.Tuple, ]) – defaults to None
find_nearest

Find the n_neighbors closest points to x, along with their distances.

This function assumes the x is a tuple or list with x[0] having relevant data for the distance calculation.

Parameters

  • item (Any)
  • n_neighbors – defaults to 1
    Number of neighbors to use.
reset

Reset window

update

Update the window with a new point, only added if > min distance.

If min distance is 0, we do not need to do the calculation. The item (and extra metadata) will not be added to the window if it is too close to an existing point.

Parameters

  • item (Any)
  • n_neighbors – defaults to 1
    Number of neighbors to use.
  • extra ([typing.Tuple, ]) – defaults to None

Returns

A boolean (true/false) to indicate if the point was added.

Notes

Updates are by default stored by the FIFO (first in first out) method, which means that when the size limit is reached, old samples are dumped to give room for new samples. This is circular, meaning that older points are dumped first. This also gives the implementation a temporal aspect, because older samples are replaced with newer ones.

The parameter min_dinstance_keep controls the addition of new items to the window - items that are far enough away (> min_distance_keep) are added to the window. Thus a value of 0 indicates that we add all points, and increasing from 0 makes it less likely we will keep a new item.