add_random_edge
¶
add_random_edge(
edge_index: array,
p: float = 0.5,
force_undirected: bool = False,
num_nodes: Optional[Union[int, tuple[int, int]]] = None,
training: bool = True,
) -> tuple[array, array]
Randomly adds round(p * num_edges) new edges to edge_index. The
new edges are sampled with negative_sampling():
they are all different, are not self-loops and are not already in the
graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edge_index
|
array
|
a [2, num_edges] array representing the source and target nodes of each edge |
required |
p
|
float
|
Ratio of edges to add, relative to the number of edges.
Default: |
0.5
|
force_undirected
|
bool
|
If |
False
|
num_nodes
|
Optional[Union[int, tuple[int, int]]]
|
Number of nodes in the graph, or a tuple |
None
|
training
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
array
|
A tuple |
array
|
followed by the added edges, and the added edges alone. |
Example:
edge_index = mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]])
edge_index, added_edges = add_random_edge(edge_index, p=0.5)
# edge_index: e.g. mx.array([[0, 1, 1, 2, 2, 3, 2, 1, 3],
# [1, 0, 2, 1, 3, 2, 0, 3, 0]])
# added_edges: e.g. mx.array([[2, 1, 3], [0, 3, 0]])
Source code in mlx_graphs/utils/augmentation.py
from mlx_graphs.utils import add_random_edge · mlx_graphs/utils/augmentation.py