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 |
required |
y
|
array
|
Query points, of shape |
required |
k
|
int
|
The number of neighbors. |
required |
batch_x
|
Optional[array]
|
Graph of each point of |
None
|
batch_y
|
Optional[array]
|
Graph of each point of |
None
|
cosine
|
bool
|
If |
False
|
num_graphs
|
Optional[int]
|
Number of graphs. Default: |
None
|
Returns:
| Type | Description |
|---|---|
array
|
An array of shape |
array
|
each query point of |
array
|
in |
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
from mlx_graphs.utils import knn · mlx_graphs/utils/spatial.py