Skip to main content

graph2emb

graph2emb is a lightweight Python package for learning graph embeddings. It combines Node2Vec-style random-walk node embeddings, edge embeddings, and Graph2Vec-style whole-graph embeddings in one project.

The implementation is inspired by:

Features

  • Node2Vec: random-walk based node embeddings.
  • Edge embeddings: Hadamard, average, weighted L1, and weighted L2 edge representations.
  • Graph2Vec: Weisfeiler-Lehman hashing + Doc2Vec-based graph embeddings.
  • Gensim-like APIs: small in-repo Word2Vec, Doc2Vec, and KeyedVectors implementations.
  • uv-friendly workflow for development, testing, and running examples.

Installation

From the project root:

uv add graph2emb

Or install the package in editable mode with pip:

pip install graph2emb

Quick start

Node embeddings with Node2Vec

import networkx as nx
from graph2emb import Node2Vec

# Build a small graph.
graph = nx.fast_gnp_random_graph(n=100, p=0.3, seed=42)

# Precompute transition probabilities and generate random walks.
node2vec = Node2Vec(
    graph,
    dimensions=64,
    walk_length=30,
    num_walks=20,
    workers=1,
    seed=42,
    quiet=True,
)

# Train embeddings from the walks.
model = node2vec.fit(window=10, min_count=1, epochs=5)

# Node ids are stored as strings.
print(model.wv.most_similar("2", topn=5))

Edge embeddings

from graph2emb.edges import HadamardEmbedder, AverageEmbedder

# Reuse a trained Node2Vec model.
hadamard = HadamardEmbedder(model.wv, quiet=True)
average = AverageEmbedder(model.wv, quiet=True)

print(hadamard[("1", "2")])
print(average[("1", "2")])

Graph embeddings with Graph2Vec

import networkx as nx
from graph2emb import Graph2Vec

graphs = [
    nx.fast_gnp_random_graph(n=12, p=0.3, seed=1),
    nx.fast_gnp_random_graph(n=14, p=0.2, seed=2),
]

graph2vec = Graph2Vec(
    dimensions=32,
    wl_iterations=2,
    workers=1,
    min_count=1,
    epochs=3,
    seed=42,
)
graph2vec.fit(graphs)

embeddings = graph2vec.get_embedding()
print(embeddings.shape)  # (2, 32)

Running the sample scripts

The sample/ directory contains small runnable examples with intentionally small parameters.

# Node2Vec from an edge-list file
uv run python sample/node2vec_from_edgelist.py

# Node2Vec + edge embeddings
uv run python sample/node2vec_edge_embeddings.py

# Graph2Vec
uv run python sample/graph2vec_basic.py

Outputs are written to sample/out/.

Notes

  • Node labels are converted to strings in generated walks and learned KeyedVectors.
  • Weighted Node2Vec graphs use the weight edge attribute by default. Override this with weight_key="...".
  • Edge weights, p, and q must be finite positive numbers.
  • For MultiGraph inputs, parallel edge weights are validated and summed.

Development

# Run tests
uv run pytest

# Run tests in parallel
uv run pytest -n auto

# Run coverage
uv run pytest --cov=graph2emb --cov-report=term-missing

# Build package artifacts
uv build

License

MIT. See LICENSE.

Metadata

Release files for graph2emb 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for graph2emb 1.0.0
File Size Uploaded Provenance Actions
graph2emb-1.0.0.tar.gz 29.9 kB Without provenance Details for graph2emb-1.0.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for graph2emb 1.0.0
File Interpreter ABI Platform Actions
graph2emb-1.0.0-py3-none-any.whl Python 3 none any Details for graph2emb-1.0.0-py3-none-any.whl

Total release size: 54.0 kB

Release files / graph2emb-1.0.0.tar.gz

Download URL graph2emb-1.0.0.tar.gz
Size 29.9 kB
Tags Source
SHA-256 checksum
How to use checksums
f3b12c3091d914e70f42c7f50bc0b643fa5d13f469bd3f21bdaf9b6d11afad9f
BLAKE2b-256 checksum
How to use checksums
74373571bcd2421e00d800ffe32208b867da5ffb11166d87a7c9fc0a42b075dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / graph2emb-1.0.0-py3-none-any.whl

Download URL graph2emb-1.0.0-py3-none-any.whl
Size 24.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
247118c9622016fe5fb7e72d4608ac4d63f9a62e322d2fc9006ffd335fe919c3
BLAKE2b-256 checksum
How to use checksums
8708a82182abda2eb99ae19bc68b2a6eb752d46ef810fee34a209afca93d79da
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for graph2emb 1.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page