python - 哪种稀疏矩阵格式更适合构建分块矩阵

标签 python numpy scipy sparse-matrix

我想使用相应的 scipy 之一构造一个分块矩阵格式。最终,矩阵必须转换为CSC。

我基本上以(密集)numpy 数组(具有 ndim == 2)的形式获取 block ,或者偶尔以稀疏身份的形式获取 block 。对于行的每个子集(从上到下),我从左到右添加相应的 block 。目前我正在创建矩阵,然后根据索引切片分配 block 。

我的问题(有关性能)如下:

  1. 建议使用切片还是应该使用 scipy.sparse.bmat
  2. 如果我确实使用切片,我应该使用哪种矩阵类型来插入 block (我分配 M[a:b,:]M[:,a 形式的切片:b])?

最佳答案

我不知道 scipy 方法的效率如何,但使用 coo 格式手动构建 block 矩阵相对简单。所需要做的就是收集 block 的 rowcoldata 属性,将 block 偏移量添加到坐标(即 >rowcol),然后连接:

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/

相关文章:

python - 如何选择满足 Python 中某些条件的列表的元素对?

python - 使用 numpy 过滤值

python - 沿动态指定的轴切片 numpy 数组

python - 为什么 scipy csr 矩阵的行索引比 numpy 数组慢

python - 提高 kNN 分类器的性能(速度)

python - 在Python列表中查找值的索引(带有近似值)

python - 如何将 Google-Cloud-Vision OCR protobuf 响应保存/加载到磁盘?

matlab - 从 Matlab 文件导入时为 "LapackError: Parameter a has non-native byte order in lapack_lite.dgesdd"

scipy - sklearn DecisionTreeClassifier 更深度更准确?

java - 算法 - 如何有效地删除列表中的重复元素?