ysig / GraKeL

A scikit-learn compatible library for graph kernels
https://ysig.github.io/GraKeL/
Other
588 stars 96 forks source link

How to use adjacency matrix to construct graph in the weisfeiler_lehman_subtree example #50

Closed craiggeng closed 3 years ago

craiggeng commented 3 years ago

Code: gk = WeisfeilerLehman(n_iter=10, base_graph_kernel=VertexHistogram, normalize=True) K_train = gk.fit_transform(G_train) For example,G_train is 149 adjacency matrixs of 90*90 fit_transform call the parse_input(), and then call the Graph() Error: line 45, in K_train = gk.fit_transform(G_train) File "D:\Anaconda\lib\site-packages\grakel\kernels\weisfeiler_lehman.py", line 295, in fit_transform km, self.X = self.parse_input(X) File "D:\Anaconda\lib\site-packages\grakel\kernels\weisfeiler_lehman.py", line 176, in parse_input 'a graph like object and node labels ' + TypeError: each element of X must be either a graph object or a list with at least a graph like object and node labels dict

giannisnik commented 3 years ago

Hi @CraigGeng

The Weisfeiler-Lehman subtree kernel assumes that the nodes of the input graphs are annotated with discrete labels. If your graphs do not contain node labels, you can annotate all nodes with a single common label. See, for instance the following example where I have annotated all nodes with the label a:

import numpy as np
from grakel import Graph
from grakel.kernels import WeisfeilerLehman, VertexHistogram

adj_1 = np.array([[0,1,0,1], [1,0,1,0], [0,1,0,1], [1,0,1,0]])
node_labels_1 = {0: 'a', 1: 'a', 2: 'a', 3: 'a'}
G_1 = Graph(adj_1, node_labels=node_labels_1)

adj_2 = np.array([[0,1,0], [1,0,1], [0,1,0]])
node_labels_2 = {0: 'a', 1: 'a', 2: 'a'}
G_2 = Graph(adj_2, node_labels=node_labels_2)

G_train = [G_1, G_2]

gk = WeisfeilerLehman(n_iter=3, base_graph_kernel=VertexHistogram, normalize=True)
K_train = gk.fit_transform(G_train)