Skip to content
sanghack81Public

About

A few graph kernels implemented in Python

Resources

Stars

4 stars

Watchers

2 watching

Forks

Latest commit

 

History

16 Commits

Folders and files

Repository files navigation

CI

pyGK

pyGK is a Python implementation of graph kernels based on MATLAB code by Nino Shervashidze.

Implemented kernels include:

  • shortest-path kernels for unlabeled and discretely labeled graphs;
  • random-walk kernels for unlabeled and discretely labeled graphs;
  • the Weisfeiler-Lehman kernel for discretely labeled graphs;
  • the labeled 3-graphlet kernel;
  • unlabeled 3-, 4-, and 5-graphlet kernels.

Installation

pyGK requires Python 3.10 or later.

python -m pip install .

For an editable development install with the test and build tools:

python -m pip install -e ".[test]"

Example

import networkx as nx

from pygk.unlabeled import shortest_path_kernel
from pygk.utils import KGraph

graphs = [
    KGraph(nx.path_graph(5)),
    KGraph(nx.cycle_graph(5)),
]
kernel, features = shortest_path_kernel(graphs)
print(kernel)

kernel is a 2 × 2 Gram matrix here, and features is the shortest-path feature matrix with one column per graph. Other kernel functions are in pygk.unlabeled, pygk.labeled, and pygk.graphlets.

For labeled kernels, set a label attribute on each NetworkX node before constructing KGraph:

graph = nx.path_graph(5)
nx.set_node_attributes(graph, "carbon", "label")
labeled_graph = KGraph(graph)

Development

python -m pytest -q
python -m ruff check .
python -m build

The numerical regression tests compare results with the original MATLAB implementation using the datasets under data/.

License

Apache License 2.0. See NOTICE.txt.

About

A few graph kernels implemented in Python

Resources

Stars

4 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages