-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspectral-biclustering.py
More file actions
26 lines (21 loc) 路 960 Bytes
/
Copy pathspectral-biclustering.py
File metadata and controls
26 lines (21 loc) 路 960 Bytes
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
import numpy as np
def spectral_biclustering(X, n_clusters=(2, 2)):
from scipy.linalg import svd
X = np.array(X, dtype=float)
X_centered = X - X.mean(axis=0)
U, S, Vt = svd(X_centered, full_matrices=False)
n_row_clusters, n_col_clusters = n_clusters
row_features = U[:, :n_row_clusters]
col_features = Vt.T[:, :n_col_clusters]
def kmeans(U, k, max_iters=100):
centroids = U[np.random.choice(len(U), k, replace=False)]
for _ in range(max_iters):
labels = np.argmin(np.linalg.norm(U[:, None] - centroids, axis=2), axis=1)
new_centroids = np.array([U[labels == i].mean(axis=0) for i in range(k)])
if np.allclose(centroids, new_centroids, atol=1e-6):
break
centroids = new_centroids
return labels
row_labels = kmeans(row_features, n_row_clusters)
col_labels = kmeans(col_features, n_col_clusters)
return row_labels, col_labels