.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/bicluster/plot_spectral_biclustering.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        Click :ref:`here <sphx_glr_download_auto_examples_bicluster_plot_spectral_biclustering.py>`
        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_bicluster_plot_spectral_biclustering.py:


=============================================
A demo of the Spectral Biclustering algorithm
=============================================

This example demonstrates how to generate a checkerboard dataset and
bicluster it using the Spectral Biclustering algorithm.

The data is generated with the ``make_checkerboard`` function, then
shuffled and passed to the Spectral Biclustering algorithm. The rows
and columns of the shuffled matrix are rearranged to show the
biclusters found by the algorithm.

The outer product of the row and column label vectors shows a
representation of the checkerboard structure.

.. GENERATED FROM PYTHON SOURCE LINES 18-66



.. rst-class:: sphx-glr-horizontal


    *

      .. image-sg:: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_001.png
         :alt: Original dataset
         :srcset: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_001.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_002.png
         :alt: Shuffled dataset
         :srcset: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_002.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_003.png
         :alt: After biclustering; rearranged to show biclusters
         :srcset: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_003.png
         :class: sphx-glr-multi-img

    *

      .. image-sg:: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_004.png
         :alt: Checkerboard structure of rearranged data
         :srcset: /auto_examples/bicluster/images/sphx_glr_plot_spectral_biclustering_004.png
         :class: sphx-glr-multi-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    consensus score: 1.0






|

.. code-block:: default


    # Author: Kemal Eren <kemal@kemaleren.com>
    # License: BSD 3 clause

    import numpy as np
    from matplotlib import pyplot as plt

    from sklearn.datasets import make_checkerboard
    from sklearn.cluster import SpectralBiclustering
    from sklearn.metrics import consensus_score


    n_clusters = (4, 3)
    data, rows, columns = make_checkerboard(
        shape=(300, 300), n_clusters=n_clusters, noise=10, shuffle=False, random_state=0
    )

    plt.matshow(data, cmap=plt.cm.Blues)
    plt.title("Original dataset")

    # shuffle clusters
    rng = np.random.RandomState(0)
    row_idx = rng.permutation(data.shape[0])
    col_idx = rng.permutation(data.shape[1])
    data = data[row_idx][:, col_idx]

    plt.matshow(data, cmap=plt.cm.Blues)
    plt.title("Shuffled dataset")

    model = SpectralBiclustering(n_clusters=n_clusters, method="log", random_state=0)
    model.fit(data)
    score = consensus_score(model.biclusters_, (rows[:, row_idx], columns[:, col_idx]))

    print("consensus score: {:.1f}".format(score))

    fit_data = data[np.argsort(model.row_labels_)]
    fit_data = fit_data[:, np.argsort(model.column_labels_)]

    plt.matshow(fit_data, cmap=plt.cm.Blues)
    plt.title("After biclustering; rearranged to show biclusters")

    plt.matshow(
        np.outer(np.sort(model.row_labels_) + 1, np.sort(model.column_labels_) + 1),
        cmap=plt.cm.Blues,
    )
    plt.title("Checkerboard structure of rearranged data")

    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.574 seconds)


.. _sphx_glr_download_auto_examples_bicluster_plot_spectral_biclustering.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example


  .. container:: binder-badge

    .. image:: images/binder_badge_logo.svg
      :target: https://mybinder.org/v2/gh/scikit-learn/scikit-learn/1.0.X?urlpath=lab/tree/notebooks/auto_examples/bicluster/plot_spectral_biclustering.ipynb
      :alt: Launch binder
      :width: 150 px


  .. container:: sphx-glr-download sphx-glr-download-python

     :download:`Download Python source code: plot_spectral_biclustering.py <plot_spectral_biclustering.py>`



  .. container:: sphx-glr-download sphx-glr-download-jupyter

     :download:`Download Jupyter notebook: plot_spectral_biclustering.ipynb <plot_spectral_biclustering.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_