如何将多个稀疏矩阵和密集矩阵组合在一起

how can I combine multiple sparse and dense matrices together

我一直在处理一些文本数据,但我得到的稀疏矩阵和密集矩阵(numpy 数组)很少。我只想知道如何正确组合它们。

这些是数组的类型和形状:

list1 
<109248x9 sparse matrix of type '<class 'numpy.int64'>'
    with 152643 stored elements in Compressed Sparse Row format>

list2
<109248x3141 sparse matrix of type '<class 'numpy.int64'>'
    with 350145 stored elements in Compressed Sparse Row format>

list3.shape   ,  type(list3)
(109248, 300) ,  numpy.ndarray

list4.shape   ,  type
(109248, 51)  ,  numpy.ndarray

我只想将它们全部组合成一个密集矩阵。我尝试了一些 vstack 和 hstack 但无法弄清楚。非常感谢任何帮助。

Output required: (109248, 3501)

sparse.hstack 可以连接稀疏数组和密集数组。它首先将所有内容转换为 coo 格式矩阵,创建一个新的复合 datarowcol 数组,以及 returns 一个 coo 矩阵(可选择将其转换为另一种指定格式):

In [379]: M=sparse.random(10,10,.2,'csr')                                       
In [380]: M                                                                     
Out[380]: 
<10x10 sparse matrix of type '<class 'numpy.float64'>'
    with 20 stored elements in Compressed Sparse Row format>
In [381]: A=np.ones((10,2),float)                                               
In [382]: sparse.hstack([M,A])                                                  
Out[382]: 
<10x12 sparse matrix of type '<class 'numpy.float64'>'
    with 40 stored elements in COOrdinate format>