.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/06_clustering/plot_21_planted_communities.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_21_planted_communities.py: Community detection on a planted-partition hypergraph ===================================================== A hypergraph stochastic block model with four communities is clustered by the Louvain-type maximization of the strict hypergraph modularity, by Louvain on the clique expansion, by spectral clustering with the hypergraph Laplacian and by label propagation. The adjusted Rand index (ARI) against the planted partition is reported as the share of intra-community hyperedges :math:`p_\mathrm{in}` decreases. .. GENERATED FROM PYTHON SOURCE LINES 14-37 .. code-block:: Python import matplotlib.pyplot as plt import pandas as pd from sklearn.metrics import adjusted_rand_score import hatvis as hv rows = [] for p_in in (0.9, 0.75, 0.6, 0.45, 0.3): H, truth = hv.models.planted_partition(120, 4, 300, sizes=(2, 3, 4, 5), p_in=p_in, random_state=0) # [snippet:methods] methods = { "hypergraph Louvain": hv.hypergraph_louvain(H, random_state=0), "clique Louvain": hv.louvain(H, random_state=0), "spectral (Zhou)": hv.spectral_clustering(H, 4, random_state=0), "label propagation": hv.label_propagation(H, random_state=0), } # [/snippet:methods] for name, lab in methods.items(): rows.append({"p_in": p_in, "method": name, "ARI": adjusted_rand_score(truth, lab), "Q": hv.modularity(H, lab), "communities": lab.nunique()}) df = pd.DataFrame(rows) print(df.pivot(index="p_in", columns="method", values="ARI").round(3)) .. rst-class:: sphx-glr-script-out .. code-block:: none method clique Louvain hypergraph Louvain label propagation spectral (Zhou) p_in 0.30 0.206 0.144 0.000 0.190 0.45 0.872 0.956 0.380 0.869 0.60 1.000 1.000 0.548 0.978 0.75 1.000 1.000 0.978 1.000 0.90 1.000 1.000 1.000 1.000 .. GENERATED FROM PYTHON SOURCE LINES 38-46 .. code-block:: Python fig, ax = plt.subplots(figsize=(7, 4)) for k, (name, sub) in enumerate(df.groupby("method", sort=False)): ax.plot(sub["p_in"], sub["ARI"], "o-", label=name, color=hv.plotting.PALETTE[k]) ax.set_xlabel(r"share of intra-community hyperedges $p_\mathrm{in}$") ax.set_ylabel("adjusted Rand index") ax.invert_xaxis() ax.legend(frameon=False) plt.tight_layout() .. image-sg:: /auto_examples/06_clustering/images/sphx_glr_plot_21_planted_communities_001.png :alt: plot 21 planted communities :srcset: /auto_examples/06_clustering/images/sphx_glr_plot_21_planted_communities_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.201 seconds) .. _sphx_glr_download_auto_examples_06_clustering_plot_21_planted_communities.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_21_planted_communities.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_21_planted_communities.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_21_planted_communities.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_21_planted_communities.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_