Skip to content

nearest

nearest(
    x: array,
    y: array,
    batch_x: Optional[array] = None,
    batch_y: Optional[array] = None,
) -> array

Assigns each point of x to its nearest point of y, in the same graph.

Parameters:

Name Type Description Default
x array

Points to assign, of shape [N, num_features].

required
y array

Cluster centers, of shape [M, num_features].

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

Returns:

Type Description
array

The index in y of the nearest point of each point of x, of

array

shape [N] (the smallest index on ties).

Example:

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

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]])
>>> nearest(x, y)
array([0, 0, 1, 1], dtype=int32)
Source code in mlx_graphs/utils/clustering.py
def nearest(
    x: mx.array,
    y: mx.array,
    batch_x: Optional[mx.array] = None,
    batch_y: Optional[mx.array] = None,
) -> mx.array:
    r"""Assigns each point of ``x`` to its nearest point of ``y``, in the same
    graph.

    Args:
        x: Points to assign, of shape ``[N, num_features]``.
        y: Cluster centers, of shape ``[M, num_features]``.
        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).

    Returns:
        The index in ``y`` of the nearest point of each point of ``x``, of
        shape ``[N]`` (the smallest index on ties).

    Example:

    .. code-block:: python

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

        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]])
        >>> nearest(x, y)
        array([0, 0, 1, 1], dtype=int32)
    """
    x = x.reshape(-1, 1) if x.ndim == 1 else x
    out = np.full(x.shape[0], -1, dtype=np.int64)
    best = np.full(x.shape[0], np.inf)
    for query, neighbor, dist in _neighbor_blocks(
        y, x, batch_y, batch_x, None, None, False
    ):
        # all the candidates of each query, in increasing order of distance
        # within the block: keep the smallest index among the closest ones
        order = np.lexsort((neighbor, dist, query))
        query, neighbor, dist = query[order], neighbor[order], dist[order]
        first = np.ones(query.size, dtype=bool)
        first[1:] = query[1:] != query[:-1]
        query, neighbor, dist = query[first], neighbor[first], dist[first]
        better = dist < best[query]
        out[query[better]] = neighbor[better]
        best[query[better]] = dist[better]
    if np.any(out < 0):
        raise ValueError(
            "Some points of `x` have no point of `y` in the same graph to be "
            "assigned to"
        )
    return mx.array(out.astype(np.int32))

from mlx_graphs.utils import nearest · mlx_graphs/utils/clustering.py