Clustering, Neighbors & NB
RadiusNeighborsClassifier
Radius-based nearest neighbors classifier with configurable outlier handling and hardware-accelerated distance queries.
Overview & Backend Support
RadiusNeighborsClassifier predicts class labels by voting across all training points located within a fixed distance radius of each query point. Queries with no training points inside radius are handled via outlier_label (either raising an error when None, assigning a fixed numeric label, or falling back to "most_frequent").
- Backend primitives:
N3(pairwise_l2_sq) across CPU (Rayon), CUDA,wgpu, and Google TPU for Euclidean / Minkowski (p=2) queries;"manhattan"/"l1","chebyshev","cosine", and general Minkowskipare also supported. - Incremental learning (
partial_fit): Not supported. - Persistence: Supports binary
.ditserialization viasave(path),RadiusNeighborsClassifier.load(path, device=...),ochreml.save(), andochreml.load().
Import & Signature
from ochreml.neighbors import RadiusNeighborsClassifier
# or lazily from top-level:
from ochreml import RadiusNeighborsClassifier
RadiusNeighborsClassifier(
radius: float = 1.0,
*,
weights: str = "uniform",
metric: str = "minkowski",
p: float = 2.0,
outlier_label: float | int | str | None = None,
device: str | None = None,
)Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
radius | float | 1.0 | Maximum distance threshold for neighbor inclusion in classification and radius_neighbors queries. |
weights | {"uniform", "distance"} | "uniform" | Weight function for neighbor voting ("uniform" or inverse "distance"). |
metric | {"minkowski", "euclidean", "l2", "manhattan", "l1", "chebyshev", "cosine"} | "minkowski" | Distance metric used for neighbor search. |
p | float | 2.0 | Power parameter for the Minkowski metric. |
outlier_label | float | int | "most_frequent" | None | None | Label assigned to query points with no neighbors within radius. If None, encountering an empty neighborhood raises an error. |
device | str | None | None | Target compute device ("auto", "cpu", "cuda:0", "wgpu", "tpu:0"). Defaults to ochreml.get_default_device(). |
Attributes
| Attribute | Type | Description |
|---|---|---|
classes_ | ndarray of shape (n_classes,) | None | Sorted unique class labels (float64) known to the classifier. |
outlier_label_ | float | None | Resolved numeric outlier label used during prediction (or None). |
n_features_in_ | int | None | Number of features seen during fit. |
n_samples_fit_ | int | None | Number of indexed training samples. |
device_ | str | None | Resolved hardware device string used by the underlying Rust model. |
Methods
| Method | Signature | Description |
|---|---|---|
fit | fit(X, y) -> RadiusNeighborsClassifier | Index training features X and 1D class targets y. |
predict | predict(X) -> ndarray | Predict class labels (float64, shape (n_queries,)) for X. |
predict_proba | predict_proba(X) -> ndarray | Return class probability estimates of shape (n_queries, n_classes). |
score | score(X, y) -> float | Return mean classification accuracy on (X, y). |
radius_neighbors | radius_neighbors(X=None, radius=None, return_distance=True, sort_results=False) | Return (distances, indices) or indices as object arrays of per-query 1D arrays. If X is None, queries the training set excluding each point itself. |
save | save(path: str) -> None | Save the fitted classifier to a binary .dit file. |
load | RadiusNeighborsClassifier.load(path: str, device: str | None = None) -> RadiusNeighborsClassifier | Load a fitted classifier from a binary .dit file. |
to | to(device: str) -> RadiusNeighborsClassifier | Move estimator compute target to another hardware device. |
Example
import numpy as np
from ochreml.neighbors import RadiusNeighborsClassifier
X = np.array([[0.0, 0.0], [0.2, 0.1], [3.0, 3.0], [3.2, 2.9]], dtype=np.float64)
y = np.array([0, 0, 1, 1], dtype=np.float64)
clf = RadiusNeighborsClassifier(radius=1.0, outlier_label="most_frequent")
clf.fit(X, y)
X_query = np.array([[0.1, 0.1], [10.0, 10.0]], dtype=np.float64)
print("Predictions:", clf.predict(X_query))
dists, idxs = clf.radius_neighbors(X_query, sort_results=True)
print("Neighbor indices per query:", idxs)