The scipy.sparse module includes the functions hstack and vstack.
For example:
In [44]: import scipy.sparse as sp
In [45]: c1 = sp.csr_matrix([[0,0,1,0],
...: [2,0,0,0],
...: [0,0,0,0]])
In [46]: c2 = sp.csr_matrix([[0,3,4,0],
...: [0,0,0,5],
...: [6,7,0,8]])
In [47]: h = sp.hstack((c1, c2), format='csr')
In [48]: h
Out[48]:
<3x8 sparse matrix of type '<type 'numpy.int64'>'
with 8 stored elements in Compressed Sparse Row format>
In [49]: h.A
Out[49]:
array([[0, 0, 1, 0, 0, 3, 4, 0],
[2, 0, 0, 0, 0, 0, 0, 5],
[0, 0, 0, 0, 6, 7, 0, 8]])
In [50]: v = sp.vstack((c1, c2), format='csr')
In [51]: v
Out[51]:
<6x4 sparse matrix of type '<type 'numpy.int64'>'
with 8 stored elements in Compressed Sparse Row format>
In [52]: v.A
Out[52]:
array([[0, 0, 1, 0],
[2, 0, 0, 0],
[0, 0, 0, 0],
[0, 3, 4, 0],
[0, 0, 0, 5],
[6, 7, 0, 8]])
本文介绍了Scipy.sparse模块中的hstack和vstack函数,并通过示例展示了如何使用这两个函数来处理稀疏矩阵。具体包括创建稀疏矩阵、水平堆叠(hstack)和垂直堆叠(vstack)的操作。
1441

被折叠的 条评论
为什么被折叠?



