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)
Errors
Common errors & fixes
AttributeError: module 'node2vec' has no attribute 'Node2Vec'
Importing the module instead of the class, or using outdated import path.
fixUse '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.
fixEnsure 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.
fixVerify 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.
fixUpgrade: 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