.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/01_graphs/plot_02_geodesic_distances.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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_01_graphs_plot_02_geodesic_distances.py: Geodesic distances on a neighborhood graph ========================================== On a curved manifold the Euclidean distance cuts through empty space. The length of the shortest path in a neighborhood graph, with edges weighted by their Euclidean length, follows the data instead: it approximates the *geodesic* distance along the manifold. Too few neighbors split the graph, too many create short-circuit edges between the windings of the swiss roll. .. GENERATED FROM PYTHON SOURCE LINES 11-58 .. image-sg:: /auto_examples/01_graphs/images/sphx_glr_plot_02_geodesic_distances_001.png :alt: Euclidean distance from the marked sample, geodesic distance (8-NN graph) from the marked sample, choice of k :srcset: /auto_examples/01_graphs/images/sphx_glr_plot_02_geodesic_distances_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none k= 2: 70 component(s), correlation 0.894 k= 3: 4 component(s), correlation 0.981 k= 4: 1 component(s), correlation 0.987 k= 6: 1 component(s), correlation 0.988 k= 8: 1 component(s), correlation 0.987 k=12: 1 component(s), correlation 0.986 k=16: 1 component(s), correlation 0.885 k=24: 1 component(s), correlation 0.720 k=32: 1 component(s), correlation 0.632 | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from scipy.sparse.csgraph import connected_components from graphclust import graphs from graphclust.datasets import make_swiss_roll X, t = make_swiss_roll(1000, random_state=0) # [snippet:geodesic] A = graphs.knn_graph(X, k=8) D_geo = graphs.geodesic_distances(A, points=X, connect=True) D_eu = np.linalg.norm(X[:, None] - X[None], axis=-1) # [/snippet:geodesic] src = int(np.argmin(t)) fig = plt.figure(figsize=(12, 4)) for i, (D, name) in enumerate([(D_eu, "Euclidean distance"), (D_geo, "geodesic distance (8-NN graph)")]): ax = fig.add_subplot(1, 3, i + 1, projection="3d") ax.scatter(*X.T, c=D[src], cmap="viridis", s=6) ax.scatter(*X[src], c="#D55E00", s=60, marker="*") ax.set_title(f"{name} from the marked sample", fontsize=10) ax.view_init(8, -70) ax.set_xticks([]) ax.set_yticks([]) ax.set_zticks([]) ax = fig.add_subplot(1, 3, 3) ks = [2, 3, 4, 6, 8, 12, 16, 24, 32] comps, corr = [], [] for k in ks: Ak = graphs.knn_graph(X, k) comps.append(connected_components(Ak)[0]) Dk = graphs.geodesic_distances(Ak, points=X, connect=True) corr.append(np.corrcoef(Dk[src], t)[0, 1]) ax.plot(ks, corr, "o-", color="#0072B2", label="corr(geodesic, position on curve)") ax2 = ax.twinx() ax2.plot(ks, comps, "s--", color="#D55E00", label="graph components") ax.set_xscale("log") ax.set_xlabel("k") ax.set_ylabel("correlation") ax2.set_ylabel("components") ax.set_title("choice of k", fontsize=10) fig.legend(loc="lower right", fontsize=8, frameon=False) plt.tight_layout() for k, c, r in zip(ks, comps, corr): print(f"k={k:2d}: {c} component(s), correlation {r:.3f}") .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.372 seconds) .. _sphx_glr_download_auto_examples_01_graphs_plot_02_geodesic_distances.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/ComplexSystemsIntelligence/graphclust/main?urlpath=lab/tree/notebooks/auto_examples/01_graphs/plot_02_geodesic_distances.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_geodesic_distances.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_geodesic_distances.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_geodesic_distances.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_