我想使用相应的 scipy 之一构造一个分块矩阵格式。最终,矩阵必须转换为CSC。
我基本上以(密集)numpy 数组(具有 ndim == 2)的形式获取 block ,或者偶尔以稀疏身份的形式获取 block 。对于行的每个子集(从上到下),我从左到右添加相应的 block 。目前我正在创建矩阵,然后根据索引切片分配 block 。
我的问题(有关性能)如下:
- 建议使用切片还是应该使用
scipy.sparse.bmat
? - 如果我确实使用切片,我应该使用哪种矩阵类型来插入 block (我分配
M[a:b,:]
和M[:,a 形式的切片:b]
)?
最佳答案
我不知道 scipy 方法的效率如何,但使用 coo 格式手动构建 block 矩阵相对简单。所需要做的就是收集 block 的 row
、col
和 data
属性,将 block 偏移量添加到坐标(即 >row
和 col
),然后连接:
import numpy as np
from scipy import sparse
from collections import namedtuple
from operator import attrgetter
submat = namedtuple('submat', 'row_offset col_offset block')
def join_blocks(blocks):
roff, coff, mat = zip(*blocks)
row, col, data = zip(*map(attrgetter('row', 'col', 'data'), mat))
row = [o + r for o, r in zip(roff, row)]
col = [o + c for o, c in zip(coff, col)]
row, col, data = map(np.concatenate, (row, col, data))
return sparse.coo_matrix((data, (row, col))).tocsr()
example = [*map(submat, range(0, 10, 2), range(8, -2, -2), map(sparse.coo_matrix, np.multiply.outer([6, 2, 1, 3, 4], [[1, 0], [-1, 1]])))]
print('Example:')
for sm in example:
print(sm)
print('\nCombined')
print(join_blocks(example).A)
打印:
Example:
submat(row_offset=0, col_offset=8, block=<2x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in COOrdinate format>)
submat(row_offset=2, col_offset=6, block=<2x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in COOrdinate format>)
submat(row_offset=4, col_offset=4, block=<2x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in COOrdinate format>)
submat(row_offset=6, col_offset=2, block=<2x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in COOrdinate format>)
submat(row_offset=8, col_offset=0, block=<2x2 sparse matrix of type '<class 'numpy.int64'>'
with 3 stored elements in COOrdinate format>)
Combined
[[ 0 0 0 0 0 0 0 0 6 0]
[ 0 0 0 0 0 0 0 0 -6 6]
[ 0 0 0 0 0 0 2 0 0 0]
[ 0 0 0 0 0 0 -2 2 0 0]
[ 0 0 0 0 1 0 0 0 0 0]
[ 0 0 0 0 -1 1 0 0 0 0]
[ 0 0 3 0 0 0 0 0 0 0]
[ 0 0 -3 3 0 0 0 0 0 0]
[ 4 0 0 0 0 0 0 0 0 0]
[-4 4 0 0 0 0 0 0 0 0]]
关于python - 哪种稀疏矩阵格式更适合构建分块矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55044093/