.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/02_nbvfs/plot_02_confidence_mds.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_02_nbvfs_plot_02_confidence_mds.py: NBVFS-CM: confidence-based MDS map =================================== The weighted network only links a sequence with its direct extensions. The CM map gives every pair of sequences a similarity: adjacent sequences have the confidence of their extension, and sequences further apart in the same tree the largest product of confidences along a path (a max-product transitive closure :math:`\mathbf{A}^*`). Metric multidimensional scaling of :math:`\mathbf{D}=\mathbf{1}-\mathbf{A}^*` places similar sequences close together; node size is the support, color the starting event, edges join a sequence to its parent. .. GENERATED FROM PYTHON SOURCE LINES 14-23 .. code-block:: Python import matplotlib.pyplot as plt import spmviz as sv from spmviz.networks import confidence_matrix, transitive_confidence db = sv.datasets.load_clickstream() ps = sv.mine(db, 0.01, algorithm="cmspam", max_gap=1, max_length=10) .. GENERATED FROM PYTHON SOURCE LINES 24-26 The enriched confidence matrix ------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 26-29 .. code-block:: Python A = confidence_matrix(ps) # a_ij = c(phi_j) if phi_i is the parent of phi_j S = transitive_confidence(A) # max-product closure of max(A, A^T) print("non-zero similarities:", (A > 0).sum(), "->", (S > 0).sum() - len(ps)) .. rst-class:: sphx-glr-script-out .. code-block:: none non-zero similarities: 227 -> 7372 .. GENERATED FROM PYTHON SOURCE LINES 30-32 The map, highlighting three starting events ------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python cm = sv.NBVFS("cm").fit(ps) print("normalized stress:", round(cm.stress_, 3)) cm.plot(start_items=[2, 3, 4], labels=16) .. image-sg:: /auto_examples/02_nbvfs/images/sphx_glr_plot_02_confidence_mds_001.png :alt: NBVFS-CM confidence-based MDS :srcset: /auto_examples/02_nbvfs/images/sphx_glr_plot_02_confidence_mds_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none normalized stress: 0.406 .. GENERATED FROM PYTHON SOURCE LINES 36-40 Parameter study: projection method ---------------------------------- Classical scaling is deterministic and fast; SMACOF minimizes the stress (default); a neighbourhood method keeps local groups but distorts distances. .. GENERATED FROM PYTHON SOURCE LINES 40-45 .. code-block:: Python fig, axes = plt.subplots(1, 2, figsize=(12, 5)) for ax, method in zip(axes, ("classical", "smacof")): m = sv.NBVFS("cm", mds=method).fit(ps) m.plot(ax=ax, legend=False, labels=8, title=f"{method}: stress {m.stress_:.2f}") plt.show() .. image-sg:: /auto_examples/02_nbvfs/images/sphx_glr_plot_02_confidence_mds_002.png :alt: classical: stress 0.84, smacof: stress 0.41 :srcset: /auto_examples/02_nbvfs/images/sphx_glr_plot_02_confidence_mds_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 2.356 seconds) .. _sphx_glr_download_auto_examples_02_nbvfs_plot_02_confidence_mds.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/spmviz/main?urlpath=lab/tree/notebooks/auto_examples/02_nbvfs/plot_02_confidence_mds.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_02_confidence_mds.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_02_confidence_mds.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_02_confidence_mds.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_