ysig / GraKeL

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

NotImplementedError: Pairwise operation is not implemented! #34

Closed kgoyal98 closed 4 years ago

kgoyal98 commented 4 years ago

from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import accuracy_score

from grakel.datasets import fetch_dataset from grakel.kernels import ShortestPath

Loads the MUTAG dataset

MUTAG = fetch_dataset("MUTAG", verbose=False) G, y = MUTAG.data, MUTAG.target

Splits the dataset into a training and a test set

G_train, G_test, y_train, y_test = train_test_split(G, y, test_size=0.1, random_state=42)

Uses the shortest path kernel to generate the kernel matrices

gk = ShortestPath(normalize=True) K_train = gk.fit_transform(G_train) gk.pairwise_operation(G_test[0], G_test[1])

Traceback (most recent call last): File "", line 1, in File "/mnt/blossom/more/kgoyal/repos/graphsearch/venv/lib/python3.6/site-packages/grakel/kernels/kernel.py", line 384, in pairwise_operation raise NotImplementedError('Pairwise operation is not implemented!') NotImplementedError: Pairwise operation is not implemented!

giannisnik commented 4 years ago

Dear @kgoyal98 ,

If I understand correctly, what you want to do is to compare two graphs (the 1st and the 2nd) of the test set to each other. You can do this as follows: kernel_val = gk.fit_transform(G_test[:2])[0,1]

If, on the other hand, you just want to generate the kernel matrix for the test samples, you can do the following: K_test = gk.transform(G_test)

kgoyal98 commented 4 years ago

Is there no other way? Actually I wanted to compare one query graph with many other corpus graphs. One way is to add the query graph in list of corpus graphs and compute the kernel matrix of the resultant list. But, this will mean greater computational complexity (by a factor of number of corpus graphs).

giannisnik commented 4 years ago

You can have a list G_query that contains only the query graph, and a list G_corpus that contains the corpus graphs. Then, you can do the following: gk = ShortestPath(normalize=True) gk.fit(G_query) K = gk.transform(G_corpus) Matrix K will be an n x 1 matrix (where n is the number of corpus graphs), and will contain the kernel values between the query graph and all the corpus graphs.