.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_clustering/plot_05_trajectories.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_clustering_plot_05_trajectories.py: Objects that move together: trajectory clustering ================================================= When many objects (units, sensors, countries, customers) are observed in the same phase space over time, two questions arise: which objects move *along similar paths*, and which ones stay *neighbours* over time. The neighbourhood-preservation similarity -- the share of time steps at which two objects are among each other's nearest neighbours -- answers the second, and DBSCAN on it yields groups of co-moving objects. .. GENERATED FROM PYTHON SOURCE LINES 12-28 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from mvtsa import clustering as cl rng = np.random.default_rng(0) T, groups = 60, [(0, 12), (12, 24), (24, 36)] P = np.zeros((36, T, 2)) for g, (a, b) in enumerate(groups): centre = np.cumsum(rng.normal(0, 0.3, (T, 2)), axis=0) + 4 * np.array( [np.cos(2 * g), np.sin(2 * g)] ) P[a:b] = centre + 0.4 * rng.normal(size=(b - a, T, 2)) truth = np.repeat([0, 1, 2], 12) .. GENERATED FROM PYTHON SOURCE LINES 29-31 Neighbourhood preservation -------------------------- .. GENERATED FROM PYTHON SOURCE LINES 31-48 .. code-block:: Python tc = cl.TrajectoryClustering(method="neighborhood", k=6, window=20).fit(P) print("labels:", tc.labels_) print( "labels per 20-step window:", [lab.tolist()[:6] for lab in tc.labels_window_], ) fig, ax = plt.subplots(figsize=(5, 4)) for i in range(36): ax.plot( P[i, :, 0], P[i, :, 1], color=f"C{max(tc.labels_[i], 0)}", lw=0.6, alpha=0.7, ) ax.set_title("trajectories coloured by cluster") plt.show() .. image-sg:: /auto_examples/07_clustering/images/sphx_glr_plot_05_trajectories_001.png :alt: trajectories coloured by cluster :srcset: /auto_examples/07_clustering/images/sphx_glr_plot_05_trajectories_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none labels: [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2] labels per 20-step window: [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0]] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.066 seconds) .. _sphx_glr_download_auto_examples_07_clustering_plot_05_trajectories.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/mvtsa/main?urlpath=lab/tree/notebooks/auto_examples/07_clustering/plot_05_trajectories.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_05_trajectories.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_05_trajectories.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_05_trajectories.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_