train_test_split_edges
¶
Functions:
| Name | Description |
|---|---|
train_test_split_edges |
Splits the edges of an undirected graph into positive and negative |
train_test_split_edges
¶
train_test_split_edges(
data: GraphData, val_ratio: float = 0.05, test_ratio: float = 0.1
) -> GraphData
Splits the edges of an undirected graph into positive and negative training, validation and test edges.
Only the edges (i, j) with i < j are considered (one per
undirected edge). floor(val_ratio * num) of them become validation
edges, floor(test_ratio * num) test edges and the others training
edges, which are returned in both directions. As many negative edges
(pairs i < j that are not edges) as positive ones are sampled for the
validation and test sets.
The returned graph has the attributes train_pos_edge_index,
val_pos_edge_index, test_pos_edge_index, val_neg_edge_index,
test_neg_edge_index and train_neg_adj_mask (a boolean
[num_nodes, num_nodes] upper-triangular mask of the pairs that can
still be used as training negatives), plus *_pos_edge_features if the
graph has edge_features. Its edge_index and edge_features are
set to None. The input graph is not modified.
Randomness comes from mx.random, so results are reproducible with
mx.random.seed.
Note
Prefer RandomLinkSplit, which this
function predates (it is deprecated in PyG).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
GraphData
|
The graph to split. |
required |
val_ratio
|
float
|
Ratio of validation edges. Default: |
0.05
|
test_ratio
|
float
|
Ratio of test edges. Default: |
0.1
|
Returns:
| Type | Description |
|---|---|
GraphData
|
A copy of |
Example:
graph = GraphData(
edge_index=mx.array([[0, 1, 1, 2, 2, 3], [1, 0, 2, 1, 3, 2]]),
num_nodes=4,
)
graph = train_test_split_edges(graph, val_ratio=0.34, test_ratio=0.34)
graph.val_pos_edge_index
# e.g. mx.array([[1], [2]])
Source code in mlx_graphs/utils/train_test_split_edges.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
from mlx_graphs.utils import train_test_split_edges · mlx_graphs/utils/train_test_split_edges.py