.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/erdos_renyi.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. 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_erdos_renyi.py: =========================================================================== Graph classification on a randomly generated dataset of Erdos-Renyi graphs. =========================================================================== Script makes use of :class:`grakel.Graph` and :class:`grakel.ShortestPath` .. GENERATED FROM PYTHON SOURCE LINES 8-56 .. code-block:: default from __future__ import print_function print(__doc__) import numpy as np from random import random from sklearn.model_selection import train_test_split from sklearn.svm import SVC from sklearn.metrics import accuracy_score from grakel import Graph from grakel.kernels import ShortestPath # Generates 3 sets of Erdos-Renyi graphs. Each edge is included in the graph with probability p # independent from every other edge. The probability p is set equal to 0.25, 0.5 and 0.75 for # the graphs of the 1st, 2nd and 3rd set, respectivery Gs = list() y = list() probs = [0.25, 0.5, 0.75] for i in range(len(probs)): for j in range(5, 35): edges = list() for n1 in range(j): for n2 in range(n1+1, j): if random() <= probs[i]: edges.append((n1, n2)) edges.append((n2, n1)) Gs.append(Graph(edges)) y.append(i) # Splits the dataset into a training and a test set G_train, G_test, y_train, y_test = train_test_split(Gs, y, test_size=0.1, random_state=42) # Uses the shortest path kernel to generate the kernel matrices gk = ShortestPath(normalize=True, with_labels=False) 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_erdos_renyi.py: .. only :: html .. container:: sphx-glr-footer :class: sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: erdos_renyi.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: erdos_renyi.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_