use*_*396 6 python numpy scipy sparse-matrix python-3.x
我一直在处理一些文本数据,并且有一些稀疏矩阵和密集矩阵(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
Run Code Online (Sandbox Code Playgroud)
我只想将它们全部组合在一起作为一个密集矩阵。我尝试了一些 vstack 和 hstack 但无法弄清楚。任何帮助深表感谢。
Output required: (109248, 3501)
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2257 次 |
最近记录: |