.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/10_dynamics_learning/plot_37_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 or to run this example in your browser via Binder. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_10_dynamics_learning_plot_37_learning.py: Semi-supervised learning and hyperedge prediction ================================================= With labels for only a few vertices, hypergraph label spreading (Zhou et al. 2006), the same with class mass normalization, and a simplified hypergraph neural network (HGNN propagation followed by logistic regression) classify the rest. The accuracy is shown against the fraction of labeled vertices. Hiding hyperedges and scoring them against random vertex sets measures how predictable the higher-order structure is. .. GENERATED FROM PYTHON SOURCE LINES 14-44 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import pandas as pd import hatvis as hv H, truth = hv.models.planted_partition(150, 3, 400, sizes=(2, 3, 4), p_in=0.75, random_state=0) rng = np.random.default_rng(0) rows = [] for frac in (0.02, 0.05, 0.1, 0.2, 0.4): for _rep in range(5): lab = truth.astype(float).where(rng.random(H.n_nodes) < frac) if lab.nunique() < 3: continue # [snippet:ssl] pred_ls = hv.learning.label_spreading(H, lab, alpha=0.9).idxmax(axis=1) pred_cm = hv.learning.label_spreading(H, lab, alpha=0.9, class_mass_normalization=True).idxmax(axis=1) pred_hg = hv.learning.hgnn_classifier(H, np.eye(H.n_nodes), lab, n_layers=2).astype(float) # [/snippet:ssl] rows.append({"labeled": frac, "label spreading": (pred_ls == truth).mean(), "label spreading, class mass norm.": (pred_cm == truth).mean(), "simplified HGNN": (pred_hg == truth).mean()}) acc = pd.DataFrame(rows).groupby("labeled").mean() print(acc.round(3)) # [snippet:prediction] pred = pd.DataFrame({m: hv.learning.hyperedge_prediction(H, method=m, random_state=0) for m in ("mean_jaccard", "min_cooccurrence", "adamic_adar")}).T # [/snippet:prediction] print(pred.round(3)) .. rst-class:: sphx-glr-script-out .. code-block:: none label spreading label spreading, class mass norm. simplified HGNN labeled 0.02 0.778 0.929 0.669 0.05 0.836 0.927 0.851 0.10 0.713 0.965 0.911 0.20 0.912 0.971 0.976 0.40 0.975 0.989 0.991 AUC AP positives negatives mean_jaccard 0.593 0.612 80.0 80.0 min_cooccurrence 0.525 0.521 80.0 80.0 adamic_adar 0.655 0.663 80.0 80.0 .. GENERATED FROM PYTHON SOURCE LINES 45-52 .. code-block:: Python fig, ax = plt.subplots(figsize=(6, 3.8)) acc.plot(ax=ax, marker="o", color=hv.plotting.PALETTE[:3]) ax.set_xscale("log") ax.set_xlabel("fraction of labeled vertices") ax.set_ylabel("accuracy") ax.legend(frameon=False) plt.tight_layout() .. image-sg:: /auto_examples/10_dynamics_learning/images/sphx_glr_plot_37_learning_001.png :alt: plot 37 learning :srcset: /auto_examples/10_dynamics_learning/images/sphx_glr_plot_37_learning_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.086 seconds) .. _sphx_glr_download_auto_examples_10_dynamics_learning_plot_37_learning.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/10_dynamics_learning/plot_37_learning.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_37_learning.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_37_learning.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_37_learning.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_