Nearest Neighbors¶
This module provides nearest-neighbor backends used during roadmap growth. The backend is selected by solver infrastructure and can be swapped without changing solver logic.
Typical usage¶
from discopygal.solvers_infra.nearest_neighbors import NearestNeighbors_sklearn
nn = NearestNeighbors_sklearn()
nn.fit(points)
nearest = nn.k_nearest(sample, k=10)
See also¶
- class discopygal.solvers_infra.nearest_neighbors.NearestNeighbors(metric: Metric)¶
Abstract class represeting the interface a nearest neighbors algorithm should comply.
- Parameters:
metric (
Metric) – metric to compute nearest neighbors
- fit(points: list[Point_2 | Point_d])¶
Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.
- k_nearest(point: list[Point_2 | Point_d], k: int) list[Point_2 | Point_d]¶
Given a point, return the k-nearest neighbors to that point.
- class discopygal.solvers_infra.nearest_neighbors.NearestNeighborsCached(nn: NearestNeighbors, max_cache: int = 100)¶
Wrapper for a nearest neighbor object that also uses cache, and allows for insertion of points in real time. This allows adding points lazily, and only when cache is full then rebuilding the underlying data structure.
- Example:
>>> nn = NearestNeighborsCached(NearestNeighbors_sklearn(metric=Metric_Euclidean)) >>> nn.add_point(Point_d(8, [...]))
- Parameters:
nn (
NearestNeighbors) – nearest neigbors object to wrapmax_cache – maximum cache size
- fit(points: list[Point_2 | Point_d])¶
Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.
- get_points()¶
Return the list of inner points
- k_nearest(point: list[Point_2 | Point_d], k: int) list[Point_2 | Point_d]¶
Given a point, return the k-nearest neighbors to that point.
- class discopygal.solvers_infra.nearest_neighbors.NearestNeighbors_CGAL(metric: ~discopygal.solvers_infra.metrics.Metric = <class 'discopygal.solvers_infra.metrics.Metric_Euclidean'>)¶
CGAL implementation of nearest neighbors
- Parameters:
metric (
Metric) – metric to compute nearest neighbors
- fit(points: list[Point_2 | Point_d])¶
Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.
- class discopygal.solvers_infra.nearest_neighbors.NearestNeighbors_sklearn(metric: ~discopygal.solvers_infra.metrics.Metric = <class 'discopygal.solvers_infra.metrics.Metric_Euclidean'>)¶
Sklearn implementation of nearest neighbors
- Parameters:
metric (
Metric) – metric to compute nearest neighbors
- fit(points)¶
Get a list of points (in CGAL Point_2 or Point_d format) and fit some kind of data structure on them.