Show Sidebar Hide Sidebar

# Spectral Biclustering algorithm in Scikit-learn

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.

#### 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_checkerboard 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_checkerboard
from sklearn.datasets import samples_generator as sg
from sklearn.cluster.bicluster import SpectralBiclustering
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

n_clusters = (4, 3)
data, rows, columns = make_checkerboard(
shape=(300, 300), n_clusters=n_clusters, noise=10,
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 = 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_)]

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

checkerboard_structure = go.Heatmap(z=np.outer(np.sort(model.row_labels_) + 1,
np.sort(model.column_labels_) + 1),
colorscale=
matplotlib_to_plotly(plt.cm.Blues,len(data)),
showscale=False)

consensus score: 1.0

In [4]:
fig = tools.make_subplots(rows=2, cols=2,
subplot_titles=('Original dataset','Shuffled dataset',
'After biclustering: rearranged to show biclusters',
'Checkerboard structure of rearranged data'))
fig.append_trace(original_dataset, 1, 1)
fig.append_trace(shuffled_dataset, 1, 2)
fig.append_trace(after_biclustering, 2, 1)
fig.append_trace(checkerboard_structure, 2, 2)

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 ]  [ (2,2) x4,y4 ]


Out[4]:

Author:

    Kemal Eren <kemal@kemaleren.com>



     BSD 3 clause