如何从稀疏稀疏矩阵矩阵中获得块? [英] How to get the blocks back from a scipy sparse block matrix?

查看:186
本文介绍了如何从稀疏稀疏矩阵矩阵中获得块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过一些矢量化计算后,我得到了一个稀疏的块矩阵,所有结果都堆积在相同大小的块中.

After some vectorized calculations, I get a sparse block matrix with all my results stacked in blocks of same size.

>>> A = [[1, 1],
...      [1, 1]]
>>> B = [[2, 2],
...      [2, 2]]
>>> C = [[3, 3],
...      [3, 3]]
>>> results = scipy.sparse.block_diag(A, B, C)
>>> print(results.toarray())
[[1 1 0 0 0 0]
 [1 1 0 0 0 0]
 [0 0 2 2 0 0]
 [0 0 2 2 0 0]
 [0 0 0 0 3 3]
 [0 0 0 0 3 3]]

如果需要通过提供形状(2,2),如何有效地取回这些数组A,B,C?

How can I get back these arrays A,B,C in an efficient way, if necessery by providing their shape (2,2)?

推荐答案

In [177]: >>> A = [[1, 1],
     ...: ...      [1, 1]]
     ...: >>> B = [[2, 2],
     ...: ...      [2, 2]]
     ...: >>> C = [[3, 3],
     ...: ...      [3, 3]]
     ...: >>> results = sparse.block_diag([A, B, C])
     ...:      
In [178]: results
Out[178]: 
<6x6 sparse matrix of type '<class 'numpy.int64'>'
    with 12 stored elements in COOrdinate format>

block_diag不保留输入.而是创建coo格式矩阵,表示整个矩阵,而不是片段.

block_diag does not preserve the inputs; rather it creates coo format matrix, representing the whole matrix, not the pieces.

In [194]: results.data
Out[194]: array([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3], dtype=int64)
In [195]: results.row
Out[195]: array([0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5], dtype=int32)
In [196]: results.col
Out[196]: array([0, 1, 0, 1, 2, 3, 2, 3, 4, 5, 4, 5], dtype=int32)


In [179]: results.A
Out[179]: 
array([[1, 1, 0, 0, 0, 0],
       [1, 1, 0, 0, 0, 0],
       [0, 0, 2, 2, 0, 0],
       [0, 0, 2, 2, 0, 0],
       [0, 0, 0, 0, 3, 3],
       [0, 0, 0, 0, 3, 3]], dtype=int64)

block_diag将数组传递到sparse.bmat.依次从每个矩阵中创建一个coo矩阵,然后将coo属性合并为3个数组,这些数组是全局稀疏矩阵的输入.

block_diag pass the arrays to sparse.bmat. That in turn makes a coo matrix from each, and then merges the coo attributes into 3 arrays, which are inputs to the global sparse matrix.

还有另一种稀疏格式bsr可以保留块(直到转换为csr进行计算),但是我不得不做实验才能看到这种情况.

There is another sparse format bsr that may preserve the blocks (until conversion to csr for calculation), but I'll have to experiment to see that's the case.

让我们根据results coo来创建bsr:

In [186]: bresults = sparse.bsr_matrix(results)
In [187]: bresults
Out[187]: 
<6x6 sparse matrix of type '<class 'numpy.int64'>'
    with 12 stored elements (blocksize = 2x2) in Block Sparse Row format>
In [188]: bresults.blocksize
Out[188]: (2, 2)
In [189]: bresults.data
Out[189]: 
array([[[1, 1],
        [1, 1]],

       [[2, 2],
        [2, 2]],

       [[3, 3],
        [3, 3]]], dtype=int64)

因此,它可以推断出是否有块,如您所愿.

So it deduces that there are blocks, just as you desired.

In [191]: bresults.indices
Out[191]: array([0, 1, 2], dtype=int32)
In [192]: bresults.indptr
Out[192]: array([0, 1, 2, 3], dtype=int32)

所以它就像csr一样的存储,但data是以块的形式排列的.

So it's a csr like storage, but with the data arranged in blocks.

可能可以在没有block_diag中介的情况下从您的A,B,C构建它,但是我不得不更多地研究文档.

It may be possible to construct this from your A,B,C without the block_diag intermediary, but I'd have to look at the docs more.

这篇关于如何从稀疏稀疏矩阵矩阵中获得块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆