Python 可以创建一个NumPy中没有行或列的虚拟稀疏矩阵吗?

Python 可以创建一个NumPy中没有行或列的虚拟稀疏矩阵吗?,python,numpy,matrix,scipy,sparse-matrix,Python,Numpy,Matrix,Scipy,Sparse Matrix,我使用的是稀疏矩阵数组,有时一些矩阵没有行或列(即,shape=(0,0))。我希望我的各种其他函数忽略这些矩阵,或者执行一些其他智能默认操作 似乎我不能使用任何稀疏矩阵格式,因为它们抱怨“无效形状”。由于某种原因,matrix([])和matrix([[])具有shape=(1,0),因此这些都不是很好(实际上,这是一个bug吗?它没有什么意义…) 我知道我可以使用None,[],数组([])或类似的东西。然而,标准的矩阵操作函数将失败时,交给这些,这意味着我将需要分散 if block is

我使用的是稀疏矩阵数组,有时一些矩阵没有行或列(即,
shape=(0,0)
)。我希望我的各种其他函数忽略这些矩阵,或者执行一些其他智能默认操作

似乎我不能使用任何稀疏矩阵格式,因为它们抱怨“无效形状”。由于某种原因,
matrix([])
matrix([[])
具有
shape=(1,0)
,因此这些都不是很好(实际上,这是一个bug吗?它没有什么意义…)

我知道我可以使用
None
[]
数组([])
或类似的东西。然而,标准的矩阵操作函数将失败时,交给这些,这意味着我将需要分散

if block is not None:
或类似的代码

是否有一个矩阵类我错过了,这将允许我优雅地处理这些案件


对于任何好奇的人来说:这来自于按变量类型将有限元分解成块。其中一些变量可能被固定(固定为常数值),因此没有导数。因此,雅可比矩阵中没有条目。

是。你能行

from scipy import sparse
result = sparse.eye(0)
# result.shape = (0, 0)
矩阵([]]->shape=(1,0)非常合理;你给它一行,零列;numpy怎么能猜出你真正想要的是零行呢


所有数组创建函数,如np.zeros((0,0))都可以满足您的需要。

创建0x0稀疏矩阵的方法很多。比如说,

In [16]: from scipy.sparse import csr_matrix, coo_matrix, dok_matrix, eye

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

In [18]: coo_matrix(([],[[],[]]), shape=(0,0))
Out[18]: 
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in COOrdinate format>

In [19]: dok_matrix((0,0))
Out[19]: 
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in Dictionary Of Keys format>

In [20]: eye(0)
Out[20]: 
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements (1 diagonals) in DIAgonal format>

In [21]: csr_matrix(np.zeros((0, 0)))
Out[21]: 
<0x0 sparse matrix of type '<type 'numpy.float64'>'
with 0 stored elements in Compressed Sparse Row format>
[16]中的
:从scipy.sparse导入csr\u矩阵、coo\u矩阵、dok\u矩阵、eye
在[17]中:csr_矩阵(([],[[],[]),形状=(0,0))
出[17]:
在[18]中:coo_矩阵(([],[[],[]),形状=(0,0))
出[18]:
[19]中的dok_矩阵((0,0))
出[19]:
In[20]:眼睛(0)
出[20]:
[21]中:csr_矩阵(np.zeros((0,0)))
出[21]:
你说你在切碎一个雅可比矩阵。如果你索引 一个具有零长度切片的稀疏矩阵 0x0结果:

In [22]: a = np.random.randint(0, 2, size=(10,10))

In [23]: m = csr_matrix(a)

In [24]: m[3:3, 3:3]
Out[24]: 
<0x0 sparse matrix of type '<type 'numpy.int64'>'
with 0 stored elements in Compressed Sparse Row format>
[22]中的
a=np.random.randint(0,2,size=(10,10))
在[23]中:m=csr_矩阵(a)
In[24]:m[3:3,3:3]
出[24]:
(我使用的是scipy 0.13.2。)


某些稀疏操作可能无法正确处理0x0矩阵,这可能是一个错误。如果您有一些示例,可以将它们添加到您的问题中吗?

您可以尝试下面的代码,这很简单

将numpy作为np导入
np.zeros((0,0))

也许我的numpy版本太旧了,因为1)
sparse.eye
为我至少取两个参数,2)执行
sparse.eye(0,0)
为我提供
ValueError:invalid shape
。我的
np.version.version=1.7.1
。我有相同的numpy版本和scipy
0.13.0
。您可以尝试
x=np.zero((0,0));sparse.csr_matrix(x)
或升级到最新版本,如果这是使用
np.zero((0,0))
内部
sparse.csr_matrix()
给出相同的
无效形状
错误。哦,这句话是指
矩阵([])
具有
形状(1,0)
(我添加了关于
矩阵([[[])的部分)
稍后)。啊哈,np.零((0,0))起作用。我想在我的scipy版本中,只有数组(而不是矩阵或稀疏矩阵格式)可以有形状(0,0)。事实上,谢谢!我使用的是
m=sp.matrix(sp.zeros((0,0));m、 nnz=0
作为临时黑客,直到我开始更新Linux发行版或安装更新的scipy。我肯定需要更新scipy!第一组示例都给出了
ValueError:invalid shape
。第二个示例为我提供了
indexer-ror:index-out-bounds:0