fit_predict 中的 Sklearn 谱聚类错误:k 必须介于 1 和方输入矩阵的阶数之间

Sklearn Spectral Clustering error in fit_predict : k must be between 1 and the order of the square input matrix

我在 Python 中使用 Spectral Clustering Library,相似度矩阵是其主要参数。我的矩阵看起来像:

[[ 1.          0.85018854  0.85091491  0.85717652]
 [ 0.85018854  1.          0.99720197  0.99732831]
 [ 0.85091491  0.99720197  1.          0.9972462 ]
 [ 0.85717652  0.99732831  0.9972462   1.        ]]

我的代码类似于文档示例:

cl = SpectralClustering(n_clusters=4,affinity='precomputed')
y = cl.fit_predict(matrix)

但是出现如下错误:

Traceback (most recent call last):
  File "/home/mahmood/PycharmProjects/sentence2vec/graphClustering.py", line 22, in <module>
    y = cl.fit_predict(matrix)
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/base.py", line 371, in fit_predict
    self.fit(X)
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/cluster/spectral.py", line 454, in fit
    assign_labels=self.assign_labels)
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/cluster/spectral.py", line 258, in spectral_clustering
    eigen_tol=eigen_tol, drop_first=False)
  File "/usr/local/lib/python2.7/dist-packages/scikit_learn-0.17.1-py2.7-linux-x86_64.egg/sklearn/manifold/spectral_embedding_.py", line 254, in spectral_embedding
    tol=eigen_tol)
  File "/usr/lib/python2.7/dist-packages/scipy/sparse/linalg/eigen/arpack/arpack.py", line 1507, in eigsh
    raise ValueError("k must be between 1 and the order of the "
ValueError: k must be between 1 and the order of the square input matrix.

我不知道,我需要知道问题是什么,也许是解决方案。

您有 4 个数据点。

您请求 4 个集群。

你预计会发生什么?

只有平凡的解决方案(每个点都不同),因此谱聚类拒绝 运行。对于明智的解决方案,簇数必须至少为 2,如果你有 n 个点,则簇数最多为 n-1。