@@ -205,15 +205,17 @@ def correlation_graph(X):
205
205
corr_matrix (ndarray): Absolute correlation matrix
206
206
"""
207
207
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 ))
209
211
G = nx .Graph ()
210
212
for i in range (n ):
211
213
for j in range (i + 1 , n ):
212
214
G .add_edge (i , j , weight = corr_matrix [i , j ])
213
215
return G , corr_matrix
214
216
215
217
# Step 1: Create correlation graph and matrix
216
- G , corr_matrix = correlation_graph (X )
218
+ G , _ = correlation_graph (X )
217
219
218
220
# Step 2: Convert graph to adjacency matrix (symmetric)
219
221
adj_matrix = np .zeros ((X .shape [1 ], X .shape [1 ]))
@@ -228,7 +230,11 @@ def correlation_graph(X):
228
230
assign_labels = "kmeans" , # clustering on the embedding
229
231
random_state = 42 ,
230
232
)
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 []
232
238
233
239
# Step 4: Group column indices by their cluster labels
234
240
groups = [np .where (labels == i )[0 ].tolist () for i in range (k )]
0 commit comments