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

我一直在处理一些文本数据,但很少有稀疏矩阵和密集(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)


潇湘沐
浏览 206回答 1
1回答

摇曳的蔷薇

sparse.hstack可以连接稀疏和密集数组。它首先转换一切coo格式矩阵,创建一个新的复合data,row和col阵列,并返回一个coo矩阵(它任选地转换成另一种指定的格式):In [379]: M=sparse.random(10,10,.2,'csr')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [380]: M&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Out[380]:&nbsp;<10x10 sparse matrix of type '<class 'numpy.float64'>'&nbsp; &nbsp; with 20 stored elements in Compressed Sparse Row format>In [381]: A=np.ones((10,2),float)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;In [382]: sparse.hstack([M,A])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;Out[382]:&nbsp;<10x12 sparse matrix of type '<class 'numpy.float64'>'&nbsp; &nbsp; with 40 stored elements in COOrdinate format>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python