File tree Expand file tree Collapse file tree 1 file changed +17
-0
lines changed
Expand file tree Collapse file tree 1 file changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -223,6 +223,22 @@ def embed_MDS(
223223 else :
224224 X_dist = squareform (pdist (X , distance_metric ))
225225
226+ # Check for degenerate distance matrix before calling classic MDS
227+ # This happens with extreme hyperparameters (e.g., KNN close to dataset size)
228+ # causing complete diffusion homogeneity
229+ if X_dist .std () < 1e-10 or len (np .unique (X_dist )) <= 1 :
230+ import warnings
231+ warnings .warn (
232+ f"Degenerate distance matrix detected (std={ X_dist .std ():.2e} , "
233+ f"unique_values={ len (np .unique (X_dist ))} ). "
234+ "This typically occurs when hyperparameters cause complete diffusion homogeneity "
235+ "(e.g., KNN close to dataset size). "
236+ "Returning zero embedding." ,
237+ RuntimeWarning
238+ )
239+ # Return all zeros to indicate complete collapse
240+ return np .zeros ((X_dist .shape [0 ], ndim ))
241+
226242 # initialize all by CMDS
227243 Y_classic = classic (X_dist , n_components = ndim , random_state = seed )
228244 if how == "classic" :
@@ -244,6 +260,7 @@ def embed_MDS(
244260 )
245261 else :
246262 raise RuntimeError
263+
247264 if how == "metric" :
248265 # re-orient to classic
249266 _ , Y , _ = scipy .spatial .procrustes (Y_classic , Y )
You can’t perform that action at this time.
0 commit comments