Skip to content

glassbox.models.neighbors.index._base

Abstract BaseIndex interface for nearest-neighbor lookup.


BaseIndex

BaseIndex(metric)

Bases: ABC

Initialize the abstract index class.

Parameters:

Name Type Description Default
metric DistanceMetric

The distance metric to use.

required
Source code in glassbox/models/neighbors/index/_base.py
def __init__(self, metric: DistanceMetric) -> None:
    """
    Initialize the abstract index class.

    Parameters
    ----------
    metric : DistanceMetric
        The distance metric to use.
    """
    self.metric: DistanceMetric = metric

build abstractmethod

build(X)

Build the index structure from the training data.

Parameters:

Name Type Description Default
X ndarray

Training data, shape (n_samples, n_features).

required
Source code in glassbox/models/neighbors/index/_base.py
@abstractmethod
def build(self, X: np.ndarray) -> None:
    """
    Build the index structure from the training data.

    Parameters
    ----------
    X : np.ndarray
        Training data, shape (n_samples, n_features).
    """
    raise NotImplementedError

query abstractmethod

query(x, k)

Query the index for the k nearest neighbors.

Parameters:

Name Type Description Default
x ndarray

Query point(s), shape (n_features,) or (n_queries, n_features).

required
k int

Number of nearest neighbors to retrieve.

required

Returns:

Type Description
ndarray

Indices of the k nearest neighbors.

Source code in glassbox/models/neighbors/index/_base.py
@abstractmethod
def query(self, x: np.ndarray, k: int) -> np.ndarray:
    """
    Query the index for the k nearest neighbors.

    Parameters
    ----------
    x : np.ndarray
        Query point(s), shape (n_features,) or (n_queries, n_features).
    k : int
        Number of nearest neighbors to retrieve.

    Returns
    -------
    np.ndarray
        Indices of the k nearest neighbors.
    """
    raise NotImplementedError