Repository navigation
Open
Labels
Description
Describe the bug
_laplacian_dense from scipy\sparse\csgraph_laplacian.py breaks, crushing SpectralClustering
Steps/Code to Reproduce
import numpy as np
from sklearn.cluster import SpectralClustering
x = np.random.randn(100, 10)
x = x @ x.T
spectral = SpectralClustering(n_clusters=4, affinity='precomputed',
random_state=0).fit(x)
result = list(spectral.labels_)
print(result)Expected Results
No error
Actual Results
$ C:/Users/Name/AppData/Local/Programs/Python/Python37/python.exe c:/Users/Name/Downloads/tmp1.py
C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse\csgraph\_laplacian.py:118: RuntimeWarning: invalid value encountered in sqrt
w = np.where(isolated_node_mask, 1, np.sqrt(w))
Traceback (most recent call last):
File "c:/Users/Name/Downloads/tmp1.py", line 6, in <module>
random_state=0).fit(x)
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\cluster\_spectral.py", line 590, in fit
verbose=self.verbose,
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\cluster\_spectral.py", line 308, in spectral_clustering
drop_first=False,
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\manifold\_spectral_embedding.py", line 300, in spectral_embedding
laplacian, k=n_components, sigma=1.0, which="LM", tol=eigen_tol, v0=v0
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 1645, in eigsh
hermitian=True, tol=tol)
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 1069, in get_OPinv_matvec
return LuInv(A).matvec
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\sparse\linalg\eigen\arpack\arpack.py", line 936, in __init__
self.M_lu = lu_factor(M)
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\linalg\decomp_lu.py", line
70, in lu_factor
a1 = asarray_chkfinite(a)
File "C:\Users\Name\AppData\Local\Programs\Python\Python37\lib\site-packages\numpy\lib\function_base.py", line 489, in asarray_chkfinite
"array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNsVersions
System:
python: 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
executable: C:\Users\Name\AppData\Local\Programs\Python\Python37\python.exe
machine: Windows-10-10.0.19041-SP0
Python dependencies:
pip: 21.2.4
setuptools: 58.1.0
sklearn: 1.0
numpy: 1.21.2
scipy: 1.7.1
Cython: 0.29.24
pandas: 1.3.3
matplotlib: 3.4.3
joblib: 1.0.1
threadpoolctl: 2.2.0
Activity
ogrisel commentedon Oct 12, 2021
Any suggestion for a possible fix?
lobpcg commentedon Oct 12, 2021
I think the issue is that many of the weights are negative in this example, leading to negative row sums, so the attempt fails to take the Square root for the construction of the normalized Laplacian. So this is not a genuine bug.
Possible fixes are:
Produce a meaningful error about the weights being negative not allowed.
Produce a warning and take absolute value of the row sum. That would still fail if the sum is 0.
Produce a warning and take the row sum of the absolute values.
ogrisel commentedon Oct 12, 2021
Yes indeed. I think we should just raise a
ValueErrorwith an informative error message if there are negative values in a precomputed affinity matrix.ogrisel commentedon Oct 12, 2021
Actually I am not 100% sure. Isn't it supposed to work on affinity matrices computed based on the cosine similarity for instance? I would need to re-read the code.
lobpcg commentedon Oct 12, 2021
There is nothing mathematically wrong with negative weights, one just cannot use then the standard definition of the normalized Laplacian; see e.g. https://epubs.siam.org/doi/pdf/10.1137/1.9781611975215.2
[-]SpectralClustering breaks on small-size low-rank data[/-][+]SpectralClustering breaks normalized Laplacian when the row sums are negative[/+]