Show Sidebar Hide Sidebar

# Spectral Co-Clustering Algorithm in Scikit-learn

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.

#### New to Plotly?¶

You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version¶

In [1]:
import sklearn
sklearn.__version__

Out[1]:
'0.18'

### Imports¶

This tutorial imports make_biclusters and consensus_score

In [2]:
print(__doc__)

import plotly.plotly as py
from plotly import tools
import plotly.graph_objs as go

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

Automatically created module for IPython interactive environment


### Calculations and Plots¶

In [3]:
def matplotlib_to_plotly(cmap, pl_entries):
h = 1.0/(pl_entries-1)
pl_colorscale = []

for k in range(pl_entries):
C = map(np.uint8, np.array(cmap(k*h)[:3])*255)
pl_colorscale.append([k*h, 'rgb'+str((C[0], C[1], C[2]))])

return pl_colorscale

data, rows, columns = make_biclusters(
shape=(300, 300), n_clusters=5, noise=5,
shuffle=False, random_state=0)

original_dataset = go.Heatmap(z=data, colorscale=
matplotlib_to_plotly(plt.cm.Blues,len(data)),
showscale=False)

data, row_idx, col_idx = sg._shuffle(data, random_state=0)

shuffled_dataset = go.Heatmap(z=data, colorscale=
matplotlib_to_plotly(plt.cm.Blues,len(data)),
showscale=False)

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_)]

after_biclustering = go.Heatmap(z=fit_data  , colorscale=
matplotlib_to_plotly(plt.cm.Blues,len(fit_data)),
showscale=False)

consensus score: 1.000

In [4]:
fig = tools.make_subplots(rows=2, cols=2, specs=[[{}, {}], [{'colspan': 2}, None]],
subplot_titles=('Original dataset','Shuffled dataset',
'After biclustering: rearranged to show biclusters'))
fig.append_trace(original_dataset, 1, 1)
fig.append_trace(shuffled_dataset, 1, 2)
fig.append_trace(after_biclustering, 2, 1)

fig['layout'].update(height=900)
py.iplot(fig)

This is the format of your plot grid:
[ (1,1) x1,y1 ]  [ (1,2) x2,y2 ]
[ (2,1) x3,y3           -      ]


Out[4]:

Author:

        Kemal Eren <kemal@kemaleren.com>



        BSD 3 clause