{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# A demo of the Spectral Co-Clustering algorithm\n\nThis example demonstrates how to generate a dataset and bicluster it\nusing the Spectral Co-Clustering algorithm.\n\nThe dataset is generated using the ``make_biclusters`` function, which\ncreates a matrix of small values and implants bicluster with large\nvalues. The rows and columns are then shuffled and passed to the\nSpectral Co-Clustering algorithm. Rearranging the shuffled matrix to\nmake biclusters contiguous shows how accurately the algorithm found\nthe biclusters.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause\n\nimport numpy as np\nfrom matplotlib import pyplot as plt\n\nfrom sklearn.cluster import SpectralCoclustering\nfrom sklearn.datasets import make_biclusters\nfrom sklearn.metrics import consensus_score\n\ndata, rows, columns = make_biclusters(\n    shape=(300, 300), n_clusters=5, noise=5, shuffle=False, random_state=0\n)\n\nplt.matshow(data, cmap=plt.cm.Blues)\nplt.title(\"Original dataset\")\n\n# shuffle clusters\nrng = np.random.RandomState(0)\nrow_idx = rng.permutation(data.shape[0])\ncol_idx = rng.permutation(data.shape[1])\ndata = data[row_idx][:, col_idx]\n\nplt.matshow(data, cmap=plt.cm.Blues)\nplt.title(\"Shuffled dataset\")\n\nmodel = SpectralCoclustering(n_clusters=5, random_state=0)\nmodel.fit(data)\nscore = consensus_score(model.biclusters_, (rows[:, row_idx], columns[:, col_idx]))\n\nprint(\"consensus score: {:.3f}\".format(score))\n\nfit_data = data[np.argsort(model.row_labels_)]\nfit_data = fit_data[:, np.argsort(model.column_labels_)]\n\nplt.matshow(fit_data, cmap=plt.cm.Blues)\nplt.title(\"After biclustering; rearranged to show biclusters\")\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.11.14"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}