Registry / ai-ml / node2vec

node2vec

JSON →
library0.5.0pypypi✓ verified 83d ago

Python implementation of the node2vec algorithm for learning continuous feature representations for nodes in networks. Version 0.5.0 supports Python 3.8-3.11, with sparse matrix input and optional GPU acceleration via CuPy.

pip install node2vec
INSTALL
IMPORT
SIG · NODE2VEC
N
node2vec
ai-mlpythonv0.5.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Node2Vec
from node2vec import Node2Vec
from node2vec.node2vec import Node2Vec
Internal module path changed in 0.4.0; top-level import is recommended
Node2Vec
from node2vec import Node2Vec
import node2vec; model = node2vec.Node2Vec(...)
Import the class directly to avoid attribute errors

Basic usage: create a graph, generate walks, and train embeddings.

import networkx as nx from node2vec import Node2Vec # Create a graph g = nx.karate_club_graph() # Precompute probabilities and generate walks node2vec = Node2Vec(g, dimensions=64, walk_length=30, num_walks=200, workers=4) # Train Word2Vec model model = node2vec.fit(window=10, min_count=1, batch_words=4) # Get embedding for node 0 embedding = model.wv['0'] print(embedding.shape)
Debug
Known issues
breakingIn v0.5.0, the 'target' parameter for biased walks is removed. Use 'p' and 'q' parameters to control return and in-out parameters.
fix
Replace 'target' with 'p' and 'q' in Node2Vec constructor.
affects: >=0.5.0
gotchaNode identifiers must be strings. Passing non-string node IDs (e.g., integers) will cause a cryptic error in Gensim's Word2Vec.
fix
Convert node IDs to strings: nx.relabel_nodes(g, {n: str(n) for n in g.nodes()})
affects: all
gotchaGraph must be connected. Disconnected nodes will not appear in walks and embeddings will be missing.
fix
Ensure the graph is a single connected component or handle missing nodes after training.
affects: all
deprecatedThe 'weight_key' parameter for edge weights is deprecated and will be removed in a future version. Use 'edge_weight_attr' instead.
fix
Replace weight_key='weight' with edge_weight_attr='weight' in Node2Vec constructor.
affects: >=0.5.0
Errors
Common errors & fixes
AttributeError: module 'node2vec' has no attribute 'Node2Vec'
Importing the module instead of the class, or using outdated import path.
fix
Use 'from node2vec import Node2Vec'.
ValueError: Node '0' not in graph
Trying to get embedding for a node that doesn't exist in the graph, or node IDs were converted to strings but lookup uses original type.
fix
Ensure all node lookups use the same type (string) as the graph nodes: model.wv[str(0)].
KeyError: "Key '0' not in vocabulary"
The node did not appear in any generated walks, likely because the graph is disconnected or node degree is zero.
fix
Verify graph connectivity and ensure all nodes have at least one walk.
ImportError: cannot import name 'Node2Vec' from 'node2vec'
Installed version is older than 0.4.0, where the class was not exposed at top level.
fix
Upgrade: pip install --upgrade node2vec, then use from node2vec import Node2Vec.
Upgrade
Version history
0.5.0latest on PyPI · released Aug 2, 2024
Audit
Dependencies
numpyrequiredNumerical arrays
networkxrequiredGraph representation
gensimrequiredWord2Vec implementation used for embedding
cupy-cuda11xoptionalGPU acceleration (optional)
tqdmrequiredProgress bars
scipyrequiredSparse matrix support
Agent activity
13 hits · last 30 days
node
12
Resources
node2vec — pip install node2vec · libregistry