Skip to content

SpectralClustering() ValueError: expected 1-d or 2-d array or matrix, got array(None, dtype=object) #21147

@nkornelakis

Description

@nkornelakis

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.

similarity_matrix.csv

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions