API reference¶
node2vec
¶
node2vec(
src: Ids,
dst: Ids,
weight: Weights | None = None,
*,
directed: bool = False,
p: float = 1.0,
q: float = 1.0,
dim: int = 128,
walks_per_node: int = 10,
walk_length: int = 80,
window: int = 10,
epochs: int = 5,
neg: int = 5,
lr: float = 0.025,
sample: float = 0.001,
normalise: bool = True,
seed: int = 42,
n_threads: int | None = None,
verbose: bool = False,
) -> Embedding
Embed the nodes of a homogeneous graph with node2vec.
Second-order biased random walks feed a skip-gram model with negative sampling, trained with lock-free (Hogwild!) SGD.
Memory is dominated by the transition table, keyed on (previous,
current) node pairs, so it grows with the sum of squared degrees. Hub-heavy
graphs get expensive fast; metapath2vec walks are first-order and do not
have this problem.
With more than one thread the threads race on the shared weights, so the
vectors differ between runs at the same seed. Pass n_threads=1 for
bit-identical output. Walk generation is reproducible at any thread count.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Ids
|
Source node id per edge. Integers or strings. |
required |
dst
|
Ids
|
Target node id per edge, same length as |
required |
weight
|
Weights | None
|
Positive weight per edge. None weights every edge 1.0. |
None
|
directed
|
bool
|
Keep edges one-way. Undirected graphs get both directions. |
False
|
p
|
float
|
Return parameter. Low values keep the walk local. |
1.0
|
q
|
float
|
In-out parameter. Low values push the walk outwards (DFS-like), high values keep it near the start (BFS-like). |
1.0
|
dim
|
int
|
Embedding dimension. |
128
|
walks_per_node
|
int
|
Walks started from every node. |
10
|
walk_length
|
int
|
Nodes per walk. |
80
|
window
|
int
|
Maximum skip-gram context window. |
10
|
epochs
|
int
|
Passes over the walks. |
5
|
neg
|
int
|
Negative samples per positive pair. |
5
|
lr
|
float
|
Initial learning rate, decayed linearly towards zero. |
0.025
|
sample
|
float
|
Subsampling threshold for frequent nodes. 0 disables it. |
0.001
|
normalise
|
bool
|
L2-normalise every output vector. |
True
|
seed
|
int
|
Random seed. |
42
|
n_threads
|
int | None
|
Threads for this call. None uses every core. |
None
|
verbose
|
bool
|
Print stage timings and one line per epoch. In a terminal the walks and training also get live progress bars. |
False
|
Returns:
| Type | Description |
|---|---|
Embedding
|
The embedding. Every node that appears in |
Embedding
|
rows are sorted by node id. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On mismatched edge arrays, a non-positive or non-finite weight, or an out-of-range parameter. |
metapath2vec
¶
metapath2vec(
src: Ids,
dst: Ids,
node_ids: Ids,
node_types: Ids,
metapath: str,
weight: Weights | None = None,
*,
plus: bool = False,
directed: bool = False,
dim: int = 128,
walks_per_node: int = 10,
walk_length: int = 80,
window: int = 10,
epochs: int = 5,
neg: int = 5,
lr: float = 0.025,
sample: float = 0.001,
normalise: bool = True,
seed: int = 42,
n_threads: int | None = None,
verbose: bool = False,
) -> Embedding
Embed the nodes of a heterogeneous graph with metapath2vec.
Walks follow a cyclic schema over node types, e.g. "gene-pathway-gene",
and start only on nodes of the schema's first type. The walk is
first-order, so memory stays linear in the edge count.
Same reproducibility caveat as node2vec: pass n_threads=1 for
bit-identical vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
src
|
Ids
|
Source node id per edge. Must appear in |
required |
dst
|
Ids
|
Target node id per edge, same length as |
required |
node_ids
|
Ids
|
Every node id, unique. Integers or strings. |
required |
node_types
|
Ids
|
Type name per entry of |
required |
metapath
|
str
|
Hyphen-separated type names that close on the starting type,
e.g. |
required |
weight
|
Weights | None
|
Positive weight per edge. None makes the graph unweighted. |
None
|
plus
|
bool
|
Draw negatives from the context node's own type, i.e. metapath2vec++. |
False
|
directed
|
bool
|
Keep edges one-way. Undirected graphs get both directions. |
False
|
dim
|
int
|
Embedding dimension. |
128
|
walks_per_node
|
int
|
Walks started from every node of the starting type. |
10
|
walk_length
|
int
|
Nodes per walk, rounded up to close the schema. |
80
|
window
|
int
|
Maximum skip-gram context window. |
10
|
epochs
|
int
|
Passes over the walks. |
5
|
neg
|
int
|
Negative samples per positive pair. |
5
|
lr
|
float
|
Initial learning rate, decayed linearly towards zero. |
0.025
|
sample
|
float
|
Subsampling threshold for frequent nodes. 0 disables it. |
0.001
|
normalise
|
bool
|
L2-normalise every output vector. |
True
|
seed
|
int
|
Random seed. |
42
|
n_threads
|
int | None
|
Threads for this call. None uses every core. |
None
|
verbose
|
bool
|
Print stage timings and one line per epoch. In a terminal the walks and training also get live progress bars. |
False
|
Returns:
| Type | Description |
|---|---|
Embedding
|
The embedding with |
Embedding
|
type, not in |
Embedding
|
no walk reaches keep their random initialisation. |
Raises:
| Type | Description |
|---|---|
ValueError
|
On mismatched or duplicate ids, an edge naming an unknown node, or an out-of-range parameter. |
Node2VecError
|
If the core rejects the metapath or no node carries its starting type. Subclasses ValueError. |
Embedding
dataclass
¶
Node vectors and the ids that name their rows.
Attributes:
| Name | Type | Description |
|---|---|---|
vectors |
ndarray
|
|
ids |
ndarray
|
Node id per row, in the dtype the ids came in as. |
types |
ndarray | None
|
Node type per row. metapath2vec only. |
walk_stats |
dict[str, int | float] | None
|
What walk generation produced: start nodes, walks attempted, truncated and dropped, mean realised length and the walk length after rounding up to the schema. metapath2vec only. A high truncated or dropped count means the schema rarely fits the graph and the vectors are not worth much. |
Node2VecError
¶
Bases: ValueError