numpy.ones(n,n)的sklearn.utils.sparsetools.connected_components失败

时间:2015-09-29 08:59:27

标签: python python-3.x scipy scikit-learn

我尝试了以下代码片段,遗憾的是它失败了,因为connected_components的pydoc未记录的内容的等级不是1。

import numpy
from scipy.sparse import csc_matrix
from sklearn.utils.sparsetools import connected_components

connected_components(numpy.ones(shape=(3,3), dtype=numpy.bool))

这会导致此错误:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "_traversal.pyx", line 75, in sklearn.utils.mst._traversal.connected_components (sklearn/utils/sparsetools/_traversal.c:1495)
  File "/usr/local/lib/python3.4/dist-packages/sklearn/utils/sparsetools/_graph_validation.py", line 50, in validate_graph
    nan_null=nan_null)
  File "_graph_tools.pyx", line 166, in sklearn.utils._graph_tools.csgraph_from_dense (sklearn/utils/sparsetools/_graph_tools.c:2601)
  File "_graph_tools.pyx", line 70, in sklearn.utils._graph_tools.csgraph_from_masked (sklearn/utils/sparsetools/_graph_tools.c:1816)
  File "/usr/local/lib/python3.4/dist-packages/scipy/sparse/compressed.py", line 88, in __init__
    self.check_format(full_check=False)
  File "/usr/local/lib/python3.4/dist-packages/scipy/sparse/compressed.py", line 156, in check_format
    raise ValueError('data, indices, and indptr should be 1-D')
ValueError: data, indices, and indptr should be 1-D

另一方面,这有效:

connected_components(csc_matrix(numpy.ones(shape=(3,3), dtype=numpy.bool)))
#output: (1, array([0, 0, 0], dtype=int32))

同样,这也有效:

connected_components(numpy.zeros(shape=(3,3), dtype=numpy.bool))
#output: (1, array([0, 1, 2], dtype=int32))

connected_components状态的pydoc:

graph : array_like or sparse
  The N x N matrix representation of a directed or undirected graph.
  If dense, then non-edges are indicated by zeros or infinities.

这表明numpy.matrix(我认为是array_like或密集)是一个有效的输入。这个错误消息是什么意思?数据,索引和indptr没有排名1是什么意思?我假设密集矩阵被转换为稀疏矩阵,以允许更快或更容易的边缘遍历,因此使用数据,索引,indptr。然而,我无法推断这3个一维结构的等级的含义应该是什么(除了0或1)。

这个错误是什么意思?这是一个错误,还是这个PEBKAC?

干杯!

0 个答案:

没有答案