【问题标题】:Python empty csr_matrix throws ValueError: cannot infer dimensions from zero sized index arraysPython 空 csr_matrix 抛出 ValueError:无法从零大小的索引数组推断维度
【发布时间】:2016-09-20 15:06:45
【问题描述】:

这里是python SSCCE:

import scipy.sparse
data = []
row = []
col = []
csr = scipy.sparse.csr_matrix((data, (row, col)))  #error happens here
print(type(csr))
print(csr)

我用python2.7运行它我得到一个错误:

raise ValueError('cannot infer dimensions from zero sized index arrays')
ValueError: cannot infer dimensions from zero sized index arrays

当我向他们提供这样的值时,它可以正常工作:

csr = scipy.sparse.csr_matrix(([10,20,30], ([0,0,0],[0,1,2])))

或者像这样:

csr = scipy.sparse.csr_matrix(([10,20], ([0,0],[0,1])))
csr = scipy.sparse.csr_matrix(([10], ([0],[0])))

我在以下位置阅读了文档: http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html

http://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information

但这似乎并不能解释为什么我不能制作一个包含零项的 csr 矩阵。

这个错误是怎么回事?我猜 scipy.sparse.csr.csr_matrix 类型在实例化时必须至少有一个值?这似乎是一个愚蠢的限制。

【问题讨论】:

    标签: python scipy sparse-matrix


    【解决方案1】:

    Scipy 稀疏矩阵具有明确的形状,例如(m, n) 其中 m 是行数,n 是列数。例如,当您编写csr_matrix(([1, 2], ([0, 3], [1, 4]))) 时,csr_matrix 从行和列索引的最大值推断形状。但是当你写csr_matrix(([], ([], [])))时,函数无法知道矩阵的形状应该是什么(我猜它不会默认创建形状为(0, 0)的矩阵)。

    一种处理方法是给出明确的shape

    In [241]: csr_matrix(([], ([], [])), shape=(3, 3))
    Out[241]: 
    <3x3 sparse matrix of type '<class 'numpy.float64'>'
        with 0 stored elements in Compressed Sparse Row format>
    

    【讨论】:

    • 真的,你应该给出一个明确的形状,而不是让 SciPy 猜测,即使数组不为空。
    • 您可以像这样创建一个空的 csr_matrix:csr = scipy.sparse.csr_matrix((0, 0))。但是,它不会从空列表中推断出这一点。这就是问题所在,为什么它必须推断出了问题,而不是我们正确地不想要任何元素?答案是因为它就是这样,处理它。
    【解决方案2】:

    您可以创建一个空的稀疏矩阵 - 从一个空的密集矩阵或通过给出形状参数:

    In [477]: from scipy import sparse
    In [478]: sparse.csr_matrix([])
    Out[478]: 
    <1x0 sparse matrix of type '<class 'numpy.float64'>'
        with 0 stored elements in Compressed Sparse Row format>
    In [479]: sparse.csr_matrix(([],([],[])),shape=(0,0))
    Out[479]: 
    <0x0 sparse matrix of type '<class 'numpy.float64'>'
        with 0 stored elements in Compressed Sparse Row format>
    

    你甚至可以用shape创建一个大的空的:

    In [480]: sparse.csr_matrix(([],([],[])),shape=(1000,1000))
    Out[480]: 
    <1000x1000 sparse matrix of type '<class 'numpy.float64'>'
        with 0 stored elements in Compressed Sparse Row format>
    In [481]: M=_
    In [482]: M[34,334]=1000
    /usr/lib/python3/dist-packages/scipy/sparse/compressed.py:730: SparseEfficiencyWarning: Changing the sparsity structure of a csr_matrix is expensive. lil_matrix is more efficient.
      SparseEfficiencyWarning)
    

    但鉴于插入值或连接新矩阵的效率低下,我不明白您为什么要制作这样的生物。

    第一个参数为元组时来自sparse.coo_matrix的相关代码:

               try:
                    obj, (row, col) = arg1
                except (TypeError, ValueError):
                    raise TypeError('invalid input format')
    
                if shape is None:
                    if len(row) == 0 or len(col) == 0:
                        raise ValueError('cannot infer dimensions from zero '
                                         'sized index arrays')
                    M = np.max(row) + 1
                    N = np.max(col) + 1
                    self.shape = (M, N)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      相关资源
      最近更新 更多