Repository navigation
Closed
Description
Describe the bug
I am trying to apply spectral clustering to the given similarity matrix (see attached csv file) for 15 clusters. Instead of the result I am getting this error. It works fine for 1-14 clusters and for 16 clusters.
Steps/Code to Reproduce
import csv
from sklearn.cluster import SpectralClustering
with open('similarity_matrix.csv', newline='') as csvfile:
x = list(csv.reader(csvfile))
spectral = SpectralClustering(n_clusters=15, affinity='precomputed', random_state=0).fit(x)
result = list(spectral.labels_)
print(result)Expected Results
The labels of the data objects
Actual Results
Traceback (most recent call last):
File "C:\Users\Nikos\PycharmProjects\community_detection_python\src\test.py", line 8, in <module>
spectral = SpectralClustering(n_clusters=15, affinity='precomputed', random_state=0).fit(x)
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\cluster\_spectral.py", line 535, in fit
self.labels_ = spectral_clustering(self.affinity_matrix_,
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
return f(*args, **kwargs)
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\cluster\_spectral.py", line 271, in spectral_clustering
maps = spectral_embedding(affinity, n_components=n_components,
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\utils\validation.py", line 63, in inner_f
return f(*args, **kwargs)
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\sklearn\manifold\_spectral_embedding.py", line 348, in spectral_embedding
_, diffusion_map = lobpcg(laplacian, X, tol=1e-15,
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\sparse\linalg\eigen\lobpcg\lobpcg.py", line 493, in lobpcg
activeBlockVectorAR = A(activeBlockVectorR)
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\sparse\linalg\interface.py", line 390, in __call__
return self*x
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\sparse\linalg\interface.py", line 393, in __mul__
return self.dot(x)
File "C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\scipy\sparse\linalg\interface.py", line 422, in dot
raise ValueError('expected 1-d or 2-d array or matrix, got %r'
ValueError: expected 1-d or 2-d array or matrix, got array(None, dtype=object)Versions
C:\Users\Nikos\AppData\Local\Programs\Python\Python39\lib\site-packages\setuptools\distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
warnings.warn(
System:
python: 3.9.2 (tags/v3.9.2:1a79785, Feb 19 2021, 13:44:55) [MSC v.1928 64 bit (AMD64)]
executable: C:\Users\Nikos\AppData\Local\Programs\Python\Python39\python.exe
machine: Windows-10-10.0.19041-SP0
Python dependencies:
pip: 21.2.4
setuptools: 49.2.1
sklearn: 0.24.1
numpy: 1.20.2
scipy: 1.6.2
Cython: None
pandas: 1.2.3
matplotlib: 3.4.0
joblib: 1.0.1
threadpoolctl: 2.1.0
Built with OpenMP: True
Metadata
Metadata
Assignees
Labels
No labels
Activity
lobpcg commentedon Sep 26, 2021
lobpcg(laplacian, X, tol=1e-15, is a too small tolerance for lobpcg that may make it break, resulting in this error. Try to see if a larger tolerance can be passed to lobpcg.
nkornelakis commentedon Sep 26, 2021
Hello @lobpcg and thank you very much for your quick response.
I guess you mean arpack which is the default eigen_solver and eigen_tol parameter which is set by default to 0.0.
I tried passing a small value to this parameter as you suggested and it worked!
Also there were no significant changes in the results for the rest number of clusters!
Thank you very much again!!!
lobpcg commentedon Sep 26, 2021
Your error comes from lobpcg, not arpack. I do not remember what the default solver is, maybe amg, which is also lobpcg. This may still be a bug, if the default tol is 1e-15 as in your printout. I suggest that you reopen it to give someone an opportunity to look at it. And provide a code to reproduce it.
adrinjalali commentedon Sep 26, 2021
I agree this needs to be investigated. It's also worth checking with the latest release.
nkornelakis commentedon Sep 26, 2021
Yes you are right.
Sorry for closing it
lobpcg commentedon Sep 26, 2021
I confirm that it is a bug in https://github.com/scikit-learn/scikit-learn/blob/main/sklearn/manifold/_spectral_embedding.py l. 369
_, diffusion_map = lobpcg(
laplacian, X, tol=1e-15, largest=False, maxiter=2000
)