Skip to content

knn

knn(
    x: array,
    y: array,
    k: int,
    batch_x: Optional[array] = None,
    batch_y: Optional[array] = None,
    cosine: bool = False,
    num_graphs: Optional[int] = None,
) -> array

Finds for each point of y the k nearest points of x.

Only points of the same graph are matched when batch vectors are given. Graphs with fewer than k points of x give fewer neighbors.

Parameters:

Name Type Description Default
x array

Points to search in, of shape [N, num_features].

required
y array

Query points, of shape [M, num_features].

required
k int

The number of neighbors.

required
batch_x Optional[array]

Graph of each point of x, of shape [N]. Default: None (a single graph).

None
batch_y Optional[array]

Graph of each point of y, of shape [M]. Default: None (a single graph).

None
cosine bool

If True, uses the cosine distance instead of the euclidean distance. Default: False

False
num_graphs Optional[int]

Number of graphs. Default: None (inferred from the batch vectors).

None

Returns:

Type Description
array

An array of shape [2, num_pairs] whose first row holds the index of

array

each query point of y and the second row the index of its neighbor

array

in x. Pairs are sorted by query, then by increasing distance.

Example:

import mlx.core as mx
from mlx_graphs.utils import knn

x = mx.array([[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]])
y = mx.array([[-1.0, 0.0], [1.0, 0.0]])
assign_index = knn(x, y, 2)
# [[0, 0, 1, 1], [0, 1, 2, 3]] (up to the order of equal distances)
Source code in mlx_graphs/utils/spatial.py
def knn(
    x: mx.array,
    y: mx.array,
    k: int,
    batch_x: Optional[mx.array] = None,
    batch_y: Optional[mx.array] = None,
    cosine: bool = False,
    num_graphs: Optional[int] = None,
) -> mx.array:
    """Finds for each point of ``y`` the ``k`` nearest points of ``x``.

    Only points of the same graph are matched when batch vectors are given.
    Graphs with fewer than ``k`` points of ``x`` give fewer neighbors.

    Args:
        x: Points to search in, of shape ``[N, num_features]``.
        y: Query points, of shape ``[M, num_features]``.
        k: The number of neighbors.
        batch_x: Graph of each point of ``x``, of shape ``[N]``. Default:
            ``None`` (a single graph).
        batch_y: Graph of each point of ``y``, of shape ``[M]``. Default:
            ``None`` (a single graph).
        cosine: If ``True``, uses the cosine distance instead of the euclidean
            distance. Default: ``False``
        num_graphs: Number of graphs. Default: ``None`` (inferred from the
            batch vectors).

    Returns:
        An array of shape ``[2, num_pairs]`` whose first row holds the index of
        each query point of ``y`` and the second row the index of its neighbor
        in ``x``. Pairs are sorted by query, then by increasing distance.

    Example:

    .. code-block:: python

        import mlx.core as mx
        from mlx_graphs.utils import knn

        x = mx.array([[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]])
        y = mx.array([[-1.0, 0.0], [1.0, 0.0]])
        assign_index = knn(x, y, 2)
        # [[0, 0, 1, 1], [0, 1, 2, 3]] (up to the order of equal distances)
    """
    rows = list(_neighbor_blocks(x, y, batch_x, batch_y, num_graphs, k, False, cosine))
    return _sorted_pairs(rows, by_distance=True)

from mlx_graphs.nn import knn · mlx_graphs/utils/spatial.py