.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_patterns/plot_25_itemset_hypergraph.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_07_patterns_plot_25_itemset_hypergraph.py: Frequent itemsets as a hypergraph ================================= Frequent theme combinations of the news articles form a weighted hypergraph: every frequent itemset is a hyperedge, its support the weight. Eclat and Apriori give identical results; closed and maximal itemsets compress the pattern set, and the inclusion lattice (Hasse diagram) of the closed itemsets shows how the combinations nest. .. GENERATED FROM PYTHON SOURCE LINES 13-29 .. code-block:: Python import matplotlib.pyplot as plt import hatvis as hv from hatvis.plotting import draw_hypergraph, plot_lattice T = hv.datasets.make_news_transactions(150, random_state=0) # [snippet:mine] fi = hv.frequent_itemsets(T, min_support=0.05) same = fi.equals(hv.frequent_itemsets(T, min_support=0.05, algorithm="apriori")) closed = hv.closed_itemsets(fi) maximal = hv.maximal_itemsets(fi) IH = hv.itemset_hypergraph(closed[closed["support"] >= 0.08], min_size=2) # [/snippet:mine] print(f"{len(fi)} frequent, {len(closed)} closed, {len(maximal)} maximal itemsets; eclat == apriori: {same}") print(maximal.head(6).to_string()) .. rst-class:: sphx-glr-script-out .. code-block:: none 88 frequent, 88 closed, 8 maximal itemsets; eclat == apriori: True itemset size count support 0 (TEMPERATURE, CLIMATE, EMISSIONS, IPCC) 4 43.0 0.095556 1 (DEVELOPING, AID, FINANCE, LOSS_DAMAGE) 4 37.0 0.082222 2 (FLOOD, WILDFIRE, DROUGHT, HEATWAVE) 4 37.0 0.082222 3 (SUMMIT, NEGOTIATION, AGREEMENT, PLEDGE) 4 36.0 0.080000 4 (COAL, ENERGY, OIL_GAS, RENEWABLES) 4 33.0 0.073333 5 (YOUTH, MIGRATION, PROTEST) 3 25.0 0.055556 .. GENERATED FROM PYTHON SOURCE LINES 30-35 .. code-block:: Python fig, ax = plt.subplots(figsize=(9, 7)) draw_hypergraph(IH.remove_isolated(), layout="force", style="smooth", width_by="weight", ax=ax, legend=False, pad=0.02, random_state=0, title="closed itemsets with support of at least 8%") plt.tight_layout() .. image-sg:: /auto_examples/07_patterns/images/sphx_glr_plot_25_itemset_hypergraph_001.png :alt: closed itemsets with support of at least 8% :srcset: /auto_examples/07_patterns/images/sphx_glr_plot_25_itemset_hypergraph_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 36-42 .. code-block:: Python climate = {"CLIMATE", "EMISSIONS", "IPCC", "TEMPERATURE"} sub = fi[fi["itemset"].map(lambda x: x <= climate)] G = hv.patterns.inclusion_lattice(sub["itemset"], weights=sub["support"]) plot_lattice(G, title="inclusion lattice of the frequent itemsets of the climate narrative") plt.gcf().set_size_inches(10, 4.8) plt.tight_layout() .. image-sg:: /auto_examples/07_patterns/images/sphx_glr_plot_25_itemset_hypergraph_002.png :alt: inclusion lattice of the frequent itemsets of the climate narrative :srcset: /auto_examples/07_patterns/images/sphx_glr_plot_25_itemset_hypergraph_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.443 seconds) .. _sphx_glr_download_auto_examples_07_patterns_plot_25_itemset_hypergraph.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/07_patterns/plot_25_itemset_hypergraph.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_25_itemset_hypergraph.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_25_itemset_hypergraph.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_25_itemset_hypergraph.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_