KNNGraph
¶
KNNGraph(
k: int = 6,
loop: bool = False,
force_undirected: bool = False,
flow: Literal["source_to_target", "target_to_source"] = "source_to_target",
cosine: bool = False,
)
Bases: BaseTransform
Creates a k-NN graph from the node positions pos of a graph.
The previous edges of the graph are replaced, and its edge_features and
edge_weights are dropped. For a GraphDataBatch, only the nodes of
the same graph are connected.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k
|
int
|
The number of neighbors of each node. Default: |
6
|
loop
|
bool
|
If |
False
|
force_undirected
|
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
|
Example:
import mlx.core as mx
from mlx_graphs.data import GraphData
from mlx_graphs.transforms import KNNGraph
pos = mx.random.normal((10, 3))
graph = GraphData(edge_index=mx.zeros((2, 0), dtype=mx.int32), pos=pos)
graph = KNNGraph(k=3)(graph)
>>> graph.edge_index.shape
(2, 30)
Methods:
| Name | Description |
|---|---|
process |
Replaces the edges of |
Source code in mlx_graphs/transforms/spatial.py
process
¶
Replaces the edges of data by its k-NN graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GraphData
|
The graph to transform. It must have a |
required |
Returns:
| Type | Description |
|---|---|
GraphData
|
The graph with the k-NN edges. |
Source code in mlx_graphs/transforms/spatial.py
from mlx_graphs.transforms import KNNGraph · mlx_graphs/transforms/spatial.py