Skip to main content

knn()

Find the approximate k nearest neighbors of every row with NNDescent.

Usage

Source

knn(
    *features,
    k=15,
    metric="euclidean",
    random_state=None,
    null_policy="error",
    n_threads=None,
    gpu=False,
    verbose=False
)

This is the neighbor search UMAP uses internally, a Rust port of PyNNDescent.

Parameters

*features: IntoExpr | list[IntoExpr]

See umap().

k: int = 15

Number of neighbors per row. As in PyNNDescent, the row itself counts as one of them: it is normally the first neighbor, at distance 0.

metric: Metric = "euclidean"

Distance metric.

random_state: int | None = None

Seed for the random number generator.

null_policy: NullPolicy = "error"

See umap(). Ignored rows get a null result and never appear as neighbors.

n_threads: int | None = None

See umap().

gpu: int | None = None

See umap().

verbose: int | None = None
See umap().

Returns

pl.Expr
A Struct expression with fields indices (Array(UInt32, k), row numbers in the input) and distances (Array(Float32, k)), sorted by ascending distance. When there are fewer than k rows, the missing neighbors are null.