knn_graph
¶
knn_graph(
x: array,
k: int,
batch_indices: Optional[array] = None,
loop: bool = False,
flow: Literal["source_to_target", "target_to_source"] = "source_to_target",
cosine: bool = False,
num_graphs: Optional[int] = None,
) -> array
Computes the graph connecting each point to its k nearest points.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
array
|
Point coordinates (or features), of shape |
required |
k
|
int
|
The number of neighbors of each point. |
required |
batch_indices
|
Optional[array]
|
Graph of each point, of shape |
None
|
loop
|
bool
|
If |
False
|
flow
|
Literal['source_to_target', 'target_to_source']
|
Direction of the edges, for use with message passing. With
|
'source_to_target'
|
cosine
|
bool
|
If |
False
|
num_graphs
|
Optional[int]
|
Number of graphs. Default: |
None
|
Returns:
| Type | Description |
|---|---|
array
|
The |
array
|
by center point, closest neighbors first. |
Example:
import mlx.core as mx
from mlx_graphs.utils import knn_graph
x = mx.array([[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]])
batch_indices = mx.array([0, 0, 0, 0])
edge_index = knn_graph(x, k=2, batch_indices=batch_indices)
>>> edge_index.shape
(2, 8)
Source code in mlx_graphs/utils/spatial.py
from mlx_graphs.nn import knn_graph · mlx_graphs/utils/spatial.py