Skip to content

NearestNeighbors

Exact nearest neighbors search data structure.

Parameters

  • window_size

    Typeint

    Size of the sliding window use to search neighbors with.

  • min_distance_keep

    Typefloat

    The minimum distance (similarity) to consider adding a point to the window. E.g., a value of 0.0 will add even exact duplicates.

  • distance_func

    TypeDistanceFunc | FunctionWrapper

    A distance function which accepts two input items to compare.

Methods

append

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

Parameters

  • item'typing.Any'
  • extra'typing.Any | None' — defaults to None

find_nearest

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

Parameters

  • item'typing.Any'
  • n_neighbors'int' — defaults to 1

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'typing.Any'
  • n_neighbors'int' — defaults to 1
  • extra'typing.Any | None' — 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.