Skip to content

Commit d9790c1

Browse files
authored
fix: gracefully handle issues with Spectral Clustering (#196)
1 parent 1659435 commit d9790c1

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

mostlyai/qa/_distances.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,15 +205,17 @@ def correlation_graph(X):
205205
corr_matrix (ndarray): Absolute correlation matrix
206206
"""
207207
n = X.shape[1]
208-
corr_matrix = np.abs(np.corrcoef(X, rowvar=False))
208+
# add tiny noise to avoid zero variance
209+
X_noisy = X + 1e-8 * np.random.randn(*X.shape)
210+
corr_matrix = np.abs(np.corrcoef(X_noisy, rowvar=False))
209211
G = nx.Graph()
210212
for i in range(n):
211213
for j in range(i + 1, n):
212214
G.add_edge(i, j, weight=corr_matrix[i, j])
213215
return G, corr_matrix
214216

215217
# Step 1: Create correlation graph and matrix
216-
G, corr_matrix = correlation_graph(X)
218+
G, _ = correlation_graph(X)
217219

218220
# Step 2: Convert graph to adjacency matrix (symmetric)
219221
adj_matrix = np.zeros((X.shape[1], X.shape[1]))
@@ -228,7 +230,11 @@ def correlation_graph(X):
228230
assign_labels="kmeans", # clustering on the embedding
229231
random_state=42,
230232
)
231-
labels = sc.fit_predict(adj_matrix)
233+
try:
234+
labels = sc.fit_predict(adj_matrix)
235+
except Exception as e:
236+
_LOG.warning(f"Spectral clustering failed: {e}")
237+
return []
232238

233239
# Step 4: Group column indices by their cluster labels
234240
groups = [np.where(labels == i)[0].tolist() for i in range(k)]

0 commit comments

Comments
 (0)