.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "examples/distance_matrix.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_distance_matrix.py: Using a distance matrix --------- .. GENERATED FROM PYTHON SOURCE LINES 7-14 Generating data =============== For this example we will generate evenly distributed points on a circle then compute the distance matrix of those points. In practice it makes little sense to go through this song and dance unless your data is given as a distance matrix. .. GENERATED FROM PYTHON SOURCE LINES 14-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from sklearn.metrics import pairwise_distances theta = np.linspace(0, 2 * np.pi, 50) circle = np.c_[np.cos(theta), np.sin(theta)] data = pairwise_distances(circle) plt.matshow(data) plt.show() .. image-sg:: /examples/images/sphx_glr_distance_matrix_001.png :alt: distance matrix :srcset: /examples/images/sphx_glr_distance_matrix_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 28-34 Projecting our data =================== From here we will pretend that we don't know the underlying data which generated the distance matrix. We pick [MDS](https://scikit-learn.org/stable/modules/generated/sklearn.manifold.MDS.html) as our lense function as it plays nicely with distance matrices. .. GENERATED FROM PYTHON SOURCE LINES 34-50 .. code-block:: Python from sklearn.manifold import MDS projection: np.ndarray = MDS( n_components=1, metric="precomputed", n_init=1, # type: ignore init="classical_mds", ).fit_transform(data) plt.scatter(circle[:, 0], circle[:, 1], label="data") plt.scatter(projection, np.zeros_like(projection), label="projection") plt.legend() plt.show() .. image-sg:: /examples/images/sphx_glr_distance_matrix_002.png :alt: distance matrix :srcset: /examples/images/sphx_glr_distance_matrix_002.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 51-58 Defining a clusterer ==================== This is the last place where we need to do something different to facilitate running mapper on a distance matrix. All we have to do is tell the base clusterer that the metric is precomputed. From there the adapter should be clever enough to detect this fact. .. GENERATED FROM PYTHON SOURCE LINES 58-70 .. code-block:: Python from sklearn.cluster import AgglomerativeClustering import zen_mapper as zm sk = AgglomerativeClustering( linkage="single", n_clusters=None, # type: ignore distance_threshold=0.2, metric="precomputed", ) clusterer = zm.sk_learn(sk) .. GENERATED FROM PYTHON SOURCE LINES 71-73 Computing the mapper graph ========================== .. GENERATED FROM PYTHON SOURCE LINES 73-84 .. code-block:: Python cover_scheme = zm.Width_Balanced_Cover(n_elements=3, percent_overlap=0.4) result = zm.mapper( data=data, projection=projection, cover_scheme=cover_scheme, clusterer=clusterer, dim=1, ) .. GENERATED FROM PYTHON SOURCE LINES 85-87 Visualizing the mapper graph ============================ .. GENERATED FROM PYTHON SOURCE LINES 87-91 .. code-block:: Python import networkx as nx graph = zm.to_networkx(result.nerve) nx.draw(graph) .. image-sg:: /examples/images/sphx_glr_distance_matrix_003.png :alt: distance matrix :srcset: /examples/images/sphx_glr_distance_matrix_003.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.152 seconds) .. _sphx_glr_download_examples_distance_matrix.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: distance_matrix.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: distance_matrix.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: distance_matrix.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_