.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/graph_learning.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_examples_graph_learning.py: Graph learning. ============== Use Kalofolias2016 algorithm to learn the graph from a signal. The algorithm uses the pairwise distances between each node signal. In this case, we compute the euclidean distance between the node's coordenates but the euclidean distance between each node signal would work in the same style. .. GENERATED FROM PYTHON SOURCE LINES 9-10 .. code-block:: Python import matplotlib.pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 11-16 .. code-block:: Python import numpy as np from scipy import spatial from pygsp2 import graph_learning, graphs .. GENERATED FROM PYTHON SOURCE LINES 17-23 .. code-block:: Python G = graphs.ring.Ring(10) # We will learn the graph from the coords # but signals from the graph also work the same signal = G.coords .. GENERATED FROM PYTHON SOURCE LINES 24-53 .. code-block:: Python kdt = spatial.KDTree(signal) D, NN = kdt.query(signal[:, None], k=len(signal)) # Allocate distance array Z = np.zeros((G.N, G.N)) for i, n in enumerate(NN): Z[i, n] = D[i] # Learn graph A = 0.8 B = 0.1 W = graph_learning.graph_log_degree(Z, A, B) W[W < 1e-5] = 0 plt.figure(figsize=(10, 4)) plt.subplot(121) plt.imshow(G.W.toarray(), vmin=0, vmax=1) plt.colorbar() plt.title('Original Graph') plt.subplot(122) plt.imshow(W, vmin=0, vmax=1) plt.colorbar() plt.title('Learned Graph') plt.show() .. image-sg:: /examples/images/sphx_glr_graph_learning_001.png :alt: Original Graph, Learned Graph :srcset: /examples/images/sphx_glr_graph_learning_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Found solution after 178 iterations .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.405 seconds) **Estimated memory usage:** 178 MB .. _sphx_glr_download_examples_graph_learning.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: graph_learning.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: graph_learning.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: graph_learning.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_