A demo of the Spectral Co-Clustering algorithm

This example demonstrates how to generate a dataset and bicluster it using the Spectral Co-Clustering algorithm.

The dataset is generated using the make_biclusters function, which creates a matrix of small values and implants bicluster with large values. The rows and columns are then shuffled and passed to the Spectral Co-Clustering algorithm. Rearranging the shuffled matrix to make biclusters contiguous shows how accurately the algorithm found the biclusters.

  • ../../_images/sphx_glr_plot_spectral_coclustering_001.png
  • ../../_images/sphx_glr_plot_spectral_coclustering_002.png
  • ../../_images/sphx_glr_plot_spectral_coclustering_003.png

Out:

1
consensus score: 1.000
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
print(__doc__)
 
# 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_biclusters
from sklearn.datasets import samples_generator as sg
from sklearn.cluster.bicluster import SpectralCoclustering
from sklearn.metrics import consensus_score
 
data, rows, columns = make_biclusters(
    shape=(300, 300), n_clusters=5, noise=5,
    shuffle=False, random_state=0)
 
plt.matshow(data, cmap=plt.cm.Blues)
plt.title("Original dataset")
 
data, row_idx, col_idx = sg._shuffle(data, random_state=0)
plt.matshow(data, cmap=plt.cm.Blues)
plt.title("Shuffled dataset")
 
model = SpectralCoclustering(n_clusters=5, random_state=0)
model.fit(data)
score = consensus_score(model.biclusters_,
                        (rows[:, row_idx], columns[:, col_idx]))
 
print("consensus score: {:.3f}".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.show()

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

Download Python source code: plot_spectral_coclustering.py
Download IPython notebook: plot_spectral_coclustering.ipynb
doc_scikit_learn
2025-01-10 15:47:30
Comments
Leave a Comment

Please login to continue.