为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

潇湘沐 2021-11-16 14:47:03
我一直在处理一些文本数据,但很少有稀疏矩阵和密集(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.ndarraylist4.shape   ,  type(109248, 51)  ,  numpy.ndarray我只想将所有这些组合在一起作为一个密集矩阵。我尝试了一些 vstack 和 hstack,但无法弄清楚。任何帮助深表感谢。Output required: (109248, 3501)
查看完整描述

1 回答

?
摇曳的蔷薇

TA贡献1793条经验 获得超6个赞

sparse.hstack可以连接稀疏和密集数组。它首先转换一切coo格式矩阵,创建一个新的复合data,row和col阵列,并返回一个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>


查看完整回答
反对 回复 2021-11-16
  • 1 回答
  • 0 关注
  • 205 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信