[英]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
格式矩陣,創建一個新的復合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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.