Python empty csr_matrix throws ValueError: cannot infer dimensions from zero sized index arrays
<p>Here's the python SSCCE: </p>

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

<p>I'm running it with python2.7 I get an error:</p>

<pre><code>raise ValueError('cannot infer dimensions from zero sized index arrays')
ValueError: cannot infer dimensions from zero sized index arrays
</code></pre>

<p>It works correctly when I feed them values like this:</p>

<pre><code>csr = scipy.sparse.csr_matrix(([10,20,30], ([0,0,0],[0,1,2])))
</code></pre>

<p>or like this:</p>

<pre><code>csr = scipy.sparse.csr_matrix(([10,20], ([0,0],[0,1])))
csr = scipy.sparse.csr_matrix(([10], ([0],[0])))
</code></pre>

<p>I read the documentation at:
<a href="http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html" rel="nofollow">http://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html</a> and </p>

<p><a href="http://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information" rel="nofollow">http://docs.scipy.org/doc/scipy/reference/sparse.html#usage-information</a> </p>

<p>but that doesn't seem to explain why I can't make a csr matrix with zero items in it.</p>

<p>What's going on with this error?  I guess scipy.sparse.csr.csr_matrix types must have at least one value in them on instantiation?  That seems like a silly restriction. </p>

<p>You can make an empty sparse matrix - from an empty dense one or by giving the shape parameter:</p>

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

<p>You can even create a large empty one with <code>shape</code>:</p>

<pre><code>In [480]: sparse.csr_matrix(([],([],[])),shape=(1000,1000))
Out[480]: 
&lt;1000x1000 sparse matrix of type '&lt;class 'numpy.float64'&gt;'
    with 0 stored elements in Compressed Sparse Row format&gt;
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)
</code></pre>

<p>But given the inefficiency of inserting values or concatenating a new matrix, I don't see why you'd want to make such a creature.</p>

<p>The relevant code from <code>sparse.coo_matrix</code> when the first argument is a tuple:</p>

<pre><code>           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)
</code></pre>

python scipy sparse-matrix