.. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_weisfeiler_lehman_subtree.py: ========================================================================= Graph classification on MUTAG using the Weisfeiler-Lehman subtree kernel. ========================================================================= Script makes use of :class:`grakel.WeisfeilerLehman`, :class:`grakel.VertexHistogram` .. code-block:: default from __future__ import print_function print(__doc__) import numpy as np 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 WeisfeilerLehman, VertexHistogram # 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 Weisfeiler-Lehman subtree kernel to generate the kernel matrices gk = WeisfeilerLehman(n_iter=4, base_kernel=VertexHistogram, normalize=True) K_train = gk.fit_transform(G_train) K_test = gk.transform(G_test) # Uses the SVM classifier to perform classification clf = SVC(kernel="precomputed") clf.fit(K_train, y_train) y_pred = clf.predict(K_test) # Computes and prints the classification accuracy acc = accuracy_score(y_test, y_pred) print("Accuracy:", str(round(acc*100, 2)) + "%") .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.000 seconds) .. _sphx_glr_download_auto_examples_weisfeiler_lehman_subtree.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download :download:`Download Python source code: weisfeiler_lehman_subtree.py ` .. container:: sphx-glr-download :download:`Download Jupyter notebook: weisfeiler_lehman_subtree.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_