.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_clustering/plot_24_coclustering.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_06_clustering_plot_24_coclustering.py: Co-clustering vertices and hyperedges ===================================== Spectral co-clustering (Dhillon 2001) clusters the rows and columns of the incidence matrix at the same time. On the synthetic news data it groups themes with the articles that use them; ordering the matrix by the co-clusters exposes the narratives as dense blocks. .. GENERATED FROM PYTHON SOURCE LINES 12-27 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd import hatvis as hv from hatvis.plotting import plot_incidence T = hv.datasets.make_news_transactions(60, random_state=0) # [snippet:cocluster] rl, cl = hv.spectral_coclustering(T, n_clusters=6, random_state=0) rows = list(rl.sort_values(kind="stable").index) cols = list(cl.sort_values(kind="stable").index) # [/snippet:cocluster] print(pd.crosstab(rl, T.node_data["group"])) .. rst-class:: sphx-glr-script-out .. code-block:: none group climate energy finance impacts policy society vertex bicluster 0 4 0 0 0 0 0 1 0 0 0 0 4 0 2 0 0 4 4 0 0 3 0 0 0 0 0 4 4 0 4 0 0 0 0 .. GENERATED FROM PYTHON SOURCE LINES 28-35 .. code-block:: Python fig, ax = plt.subplots(figsize=(12, 4.5)) plot_incidence(T, order=(rows, cols), ax=ax, labels=False, title="incidence matrix ordered by co-clusters") ax.set_yticks(range(len(rows)), rows, fontsize=6) b = np.cumsum(rl.value_counts().sort_index().to_numpy())[:-1] for y in b: ax.axhline(y - 0.5, color=hv.plotting.PALETTE[1], lw=0.8) plt.tight_layout() .. image-sg:: /auto_examples/06_clustering/images/sphx_glr_plot_24_coclustering_001.png :alt: incidence matrix ordered by co-clusters :srcset: /auto_examples/06_clustering/images/sphx_glr_plot_24_coclustering_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.459 seconds) .. _sphx_glr_download_auto_examples_06_clustering_plot_24_coclustering.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/hatvis/main?urlpath=lab/tree/notebooks/auto_examples/06_clustering/plot_24_coclustering.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_24_coclustering.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_24_coclustering.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_24_coclustering.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_