.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/2000_cluster/ssc_omp.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 .. rst-class:: sphx-glr-example-title .. _sphx_glr_gallery_2000_cluster_ssc_omp.py: .. _gallery:cluster:ssc:omp: Sparse Subspace Clustering - OMP ============================================= This example demonstrates the sparse subspace clustering algorithm via orthogonal matching pursuit. .. GENERATED FROM PYTHON SOURCE LINES 11-12 Configure JAX to work with 64-bit floating point precision. .. GENERATED FROM PYTHON SOURCE LINES 12-15 .. code-block:: default from jax.config import config config.update("jax_enable_x64", True) .. GENERATED FROM PYTHON SOURCE LINES 16-17 Let's import necessary libraries .. GENERATED FROM PYTHON SOURCE LINES 17-34 .. code-block:: default from jax import random import jax.numpy as jnp import cr.nimble as cnb import cr.sparse.data as crdata import cr.nimble as cnb import cr.nimble.subspaces # clustering related import cr.sparse.cluster.spectral as spectral import cr.sparse.cluster.ssc as ssc # Plotting import matplotlib.pyplot as plt # evaluation import sklearn.metrics # Some PRNGKeys for later use key = random.PRNGKey(0) keys = random.split(key, 10) .. GENERATED FROM PYTHON SOURCE LINES 35-36 Problem configuration .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. code-block:: default # ambient space dimension N = 40 # Subspace dimension D = 5 # Number of subspaces K = 5 # Number of points per subspace S = 50 .. GENERATED FROM PYTHON SOURCE LINES 47-49 Test data preparation ---------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-52 Construct orthonormal bases for K subspaces .. GENERATED FROM PYTHON SOURCE LINES 52-54 .. code-block:: default bases = crdata.random_subspaces_jit(keys[0], N, D, K) .. GENERATED FROM PYTHON SOURCE LINES 55-56 Measure angles between subspaces in degrees .. GENERATED FROM PYTHON SOURCE LINES 56-58 .. code-block:: default angles = cnb.subspaces.smallest_principal_angles_deg(bases) .. GENERATED FROM PYTHON SOURCE LINES 59-60 Print the minimum angle between any pair of subspaces .. GENERATED FROM PYTHON SOURCE LINES 60-62 .. code-block:: default print(cnb.off_diagonal_min(angles)) .. rst-class:: sphx-glr-script-out .. code-block:: none 47.44974475121892 .. GENERATED FROM PYTHON SOURCE LINES 63-64 Generate uniformly distributed points on each subspace .. GENERATED FROM PYTHON SOURCE LINES 64-66 .. code-block:: default X = crdata.uniform_points_on_subspaces(keys[1], bases, S) .. GENERATED FROM PYTHON SOURCE LINES 67-69 Assign true labels to each point to corresponding subspace index .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: default true_labels = jnp.repeat(jnp.arange(K), S) print(true_labels) .. rst-class:: sphx-glr-script-out .. code-block:: none [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4] .. GENERATED FROM PYTHON SOURCE LINES 73-74 Total number of data points .. GENERATED FROM PYTHON SOURCE LINES 74-77 .. code-block:: default total = len(true_labels) print(total) .. rst-class:: sphx-glr-script-out .. code-block:: none 250 .. GENERATED FROM PYTHON SOURCE LINES 78-80 Sparse Subspace Clustering Algorithm ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 82-84 Build representation of each point in terms of other points by using Orthogonal Matching Pursuit algorithm .. GENERATED FROM PYTHON SOURCE LINES 84-86 .. code-block:: default Z, I, R = ssc.build_representation_omp_jit(X, D) .. GENERATED FROM PYTHON SOURCE LINES 87-88 Combine values and indices to form full representation .. GENERATED FROM PYTHON SOURCE LINES 88-90 .. code-block:: default Z_full = ssc.sparse_to_full_rep(Z, I) .. GENERATED FROM PYTHON SOURCE LINES 91-92 Build the affinity matrix .. GENERATED FROM PYTHON SOURCE LINES 92-95 .. code-block:: default affinity = abs(Z_full) + abs(Z_full).T plt.imshow(affinity, cmap='gray') .. image-sg:: /gallery/2000_cluster/images/sphx_glr_ssc_omp_001.png :alt: ssc omp :srcset: /gallery/2000_cluster/images/sphx_glr_ssc_omp_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 96-97 Perform the spectral clustering on the affinity matrix .. GENERATED FROM PYTHON SOURCE LINES 97-99 .. code-block:: default res = spectral.unnormalized_k_jit(keys[2], affinity, K) .. GENERATED FROM PYTHON SOURCE LINES 100-101 Predicted cluster labels .. GENERATED FROM PYTHON SOURCE LINES 101-104 .. code-block:: default pred_labels = res.assignment print(pred_labels) .. rst-class:: sphx-glr-script-out .. code-block:: none [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] .. GENERATED FROM PYTHON SOURCE LINES 105-106 Evaluate the clustering performance .. GENERATED FROM PYTHON SOURCE LINES 106-110 .. code-block:: default print(sklearn.metrics.rand_score(true_labels, pred_labels)) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. GENERATED FROM PYTHON SOURCE LINES 111-113 SSC-OMP with shuffled data ------------------------------------------ .. GENERATED FROM PYTHON SOURCE LINES 115-116 Choose a random permutation .. GENERATED FROM PYTHON SOURCE LINES 116-118 .. code-block:: default perm = random.permutation(keys[3], total) .. GENERATED FROM PYTHON SOURCE LINES 119-120 Randomly permute the data points .. GENERATED FROM PYTHON SOURCE LINES 120-125 .. code-block:: default X = X[:, perm] # Permute the true labels accordingly true_labels = true_labels[perm] print(true_labels) .. rst-class:: sphx-glr-script-out .. code-block:: none [0 3 2 2 1 4 0 0 3 3 0 3 4 2 4 3 3 0 2 1 3 4 1 3 0 2 4 1 0 4 2 2 3 0 2 4 3 3 0 0 1 0 4 3 1 1 4 4 1 1 2 4 1 0 3 4 1 4 0 1 1 2 0 1 3 0 3 4 0 0 4 1 1 1 3 2 4 0 2 3 3 1 2 3 2 1 1 4 3 4 2 0 0 4 4 1 4 0 2 2 0 4 4 2 0 1 1 1 4 1 2 2 4 2 0 0 4 2 1 0 2 3 3 3 1 3 4 3 0 4 3 4 2 1 3 3 4 3 4 3 1 4 4 2 0 0 1 1 1 1 1 3 3 4 2 1 2 1 4 4 3 2 3 0 0 1 4 1 0 4 3 1 1 2 3 1 2 0 0 2 4 2 2 2 2 0 3 3 1 0 3 2 0 4 0 0 4 4 0 1 0 1 1 4 3 3 1 3 2 2 3 0 4 2 4 3 2 3 0 3 2 3 3 0 4 2 0 2 2 4 0 2 1 2 1 0 3 0 3 2 1 0 2 1 4 4 2 0 2 4] .. GENERATED FROM PYTHON SOURCE LINES 126-128 Build representation of each point in terms of other points by using Orthogonal Matching Pursuit algorithm .. GENERATED FROM PYTHON SOURCE LINES 128-130 .. code-block:: default Z, I, R = ssc.build_representation_omp_jit(X, D) .. GENERATED FROM PYTHON SOURCE LINES 131-132 Combine values and indices to form full representation .. GENERATED FROM PYTHON SOURCE LINES 132-134 .. code-block:: default Z_full = ssc.sparse_to_full_rep(Z, I) .. GENERATED FROM PYTHON SOURCE LINES 135-136 Build the affinity matrix .. GENERATED FROM PYTHON SOURCE LINES 136-139 .. code-block:: default affinity = abs(Z_full) + abs(Z_full).T plt.imshow(affinity, cmap='gray') .. image-sg:: /gallery/2000_cluster/images/sphx_glr_ssc_omp_002.png :alt: ssc omp :srcset: /gallery/2000_cluster/images/sphx_glr_ssc_omp_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 140-141 Perform the spectral clustering on the affinity matrix .. GENERATED FROM PYTHON SOURCE LINES 141-143 .. code-block:: default res = spectral.unnormalized_k_jit(keys[4], affinity, K) .. GENERATED FROM PYTHON SOURCE LINES 144-145 Predicted cluster labels .. GENERATED FROM PYTHON SOURCE LINES 145-148 .. code-block:: default pred_labels = res.assignment print(pred_labels) .. rst-class:: sphx-glr-script-out .. code-block:: none [3 0 1 1 2 4 3 3 0 0 3 0 4 1 4 0 0 3 1 2 0 4 2 0 3 1 4 2 3 4 1 1 0 3 1 4 0 0 3 3 2 3 4 0 2 2 4 4 2 2 1 4 2 3 0 4 2 4 3 2 2 1 3 2 0 3 0 4 3 3 4 2 2 2 0 1 4 3 1 0 0 2 1 0 1 2 2 4 0 4 1 3 3 4 4 2 4 3 1 1 3 4 4 1 3 2 2 2 4 2 1 1 4 1 3 3 4 1 2 3 1 0 0 0 2 0 4 0 3 4 0 4 1 2 0 0 4 0 4 0 2 4 4 1 3 3 2 2 2 2 2 0 0 4 1 2 1 2 4 4 0 1 0 3 3 2 4 2 3 4 0 2 2 1 0 2 1 3 3 1 4 1 1 1 1 3 0 0 2 3 0 1 3 4 3 3 4 4 3 2 3 2 2 4 0 0 2 0 1 1 0 3 4 1 4 0 1 0 3 0 1 0 0 3 4 1 3 1 1 4 3 1 2 1 2 3 0 3 0 1 2 3 1 2 4 4 1 3 1 4] .. GENERATED FROM PYTHON SOURCE LINES 149-150 Evaluate the clustering performance .. GENERATED FROM PYTHON SOURCE LINES 150-152 .. code-block:: default print(sklearn.metrics.rand_score(true_labels, pred_labels)) .. rst-class:: sphx-glr-script-out .. code-block:: none 1.0 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 5.554 seconds) .. _sphx_glr_download_gallery_2000_cluster_ssc_omp.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: ssc_omp.py ` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: ssc_omp.ipynb ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_