Estimator base¶
Shared fit / kneighbors / save plumbing. You never instantiate these
directly, but every estimator inherits the methods documented here.
_base
¶
Shared estimator behaviour.
Every index here is immutable once built, so the scikit-learn shape (parameters
in __init__, data in fit, results from kneighbors) is the honest one:
the FAISS-style add() would be a method callable exactly once.
get_params and set_params introspect the subclass __init__, which is
all sklearn.base.BaseEstimator does. Doing it here keeps scikit-learn out of
the install requirements while clone, GridSearchCV and Pipeline still
work by duck-typing.
BaseAnnIndex
¶
Common fit / kneighbors plumbing for every index.
Subclasses supply an __init__ that stores its parameters verbatim, a
_build hook, a _search_kwargs hook naming the algorithm's
search-time knobs, and the handle class from the compiled core.
get_params
¶
Parameters this estimator was constructed with.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
deep
|
bool
|
Accepted for scikit-learn compatibility; these estimators hold no nested estimators, so it makes no difference. |
True
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Constructor parameters, keyed by name. |
set_params
¶
set_params(**params: Any) -> BaseAnnIndex
Set constructor parameters, invalidating any fitted index.
Returns:
| Type | Description |
|---|---|
BaseAnnIndex
|
self. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a name is not a parameter of this estimator. |
fit
¶
fit(X: Any, y: Any = None) -> BaseAnnIndex
Build the index over X.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Any
|
Array-like of shape |
required |
y
|
Any
|
Ignored, present for scikit-learn pipeline compatibility. |
None
|
Returns:
| Type | Description |
|---|---|
BaseAnnIndex
|
self. |
kneighbors
¶
kneighbors(
X: Any = None,
n_neighbors: int | None = None,
*,
return_distance: bool = True,
**overrides: Any,
) -> tuple[ndarray, ndarray] | ndarray
Find the nearest neighbours of X among the fitted points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Any
|
Query points of shape |
None
|
n_neighbors
|
int | None
|
Neighbours per query. Defaults to |
None
|
return_distance
|
bool
|
Whether to return distances alongside indices. This saves the copy into numpy but not the distance computation, which the core does either way. |
True
|
**overrides
|
Any
|
Per-call values for this algorithm's search-time knobs,
for example |
{}
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | ndarray
|
|
tuple[ndarray, ndarray] | ndarray
|
|
tuple[ndarray, ndarray] | ndarray
|
A query that found fewer than |
tuple[ndarray, ndarray] | ndarray
|
indices and infinite distances. |
kneighbors_graph
¶
kneighbors_graph(
X: Any = None,
n_neighbors: int | None = None,
mode: str = "distance",
**overrides: Any,
) -> Any
Build the sparse neighbourhood graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
X
|
Any
|
Query points, or |
None
|
n_neighbors
|
int | None
|
Neighbours per query. Defaults to |
None
|
mode
|
str
|
|
'distance'
|
**overrides
|
Any
|
Per-call search-time knobs, as for |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
A |
Any
|
|
Any
|
can hold fewer than |
Raises:
| Type | Description |
|---|---|
ImportError
|
If scipy is not installed. |
ValueError
|
If |
transform
¶
Neighbourhood graph of X, for use as a KNeighborsTransformer.
save
¶
Write the fitted index to a directory.
The directory holds the core's own bundle plus a small JSON sidecar with
the estimator parameters, so load reproduces the whole object rather
than a bare handle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Target directory. Created if it does not exist. |
required |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If this index cannot be serialised. The GPU
indices hold device buffers and sit outside the crate's
|
load
classmethod
¶
load(path: str | Path) -> BaseAnnIndex
Read an index written by save.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Directory holding the bundle. |
required |
Returns:
| Type | Description |
|---|---|
BaseAnnIndex
|
The reconstructed estimator. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the directory was written by a different index type. |
NotImplementedError
|
If this index cannot be serialised. |
ExtractKnnMixin
¶
Read-back of a graph the index already built, for the descent indices.
NN-Descent and its GPU counterpart converge on a kNN graph and keep it, so
kneighbors(None) searches for something already sitting in the handle.
No other index here has such a graph, hence a mixin rather than a method on
BaseAnnIndex.
extract_knn
¶
extract_knn(
n_neighbors: int | None = None,
*,
include_self: bool = True,
return_distance: bool = True,
) -> tuple[ndarray, ndarray] | ndarray
Return the graph the descent built, without searching it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_neighbors
|
int | None
|
Total row length, the self-edge included when
|
None
|
include_self
|
bool
|
Whether row |
True
|
return_distance
|
bool
|
Whether to return distances alongside indices. |
True
|
Returns:
| Type | Description |
|---|---|
tuple[ndarray, ndarray] | ndarray
|
|
tuple[ndarray, ndarray] | ndarray
|
|
tuple[ndarray, ndarray] | ndarray
|
padded with |
tuple[ndarray, ndarray] | ndarray
|
paths never produce. |
NotFittedError
¶
Bases: ValueError, AttributeError
Raised when a query is attempted before fit.
Inherits from both ValueError and AttributeError to match
sklearn.exceptions.NotFittedError, so code catching either still works.