Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I combine multiple sparse and dense matrices together

I've been working with some text data and I've got few sparse matrices and dense (numpy arrays). I just want to know how to combine them correctly.

These are the types and shape of the arrays:

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

I just want to combine all of them together as one dense matrix. I tried some vstack and hstack but couldn't figure it out. Any help is much appreciated.

Output required: (109248, 3501)
like image 619
user_6396 Avatar asked Feb 22 '19 08:02

user_6396


1 Answers

sparse.hstack can join sparse and dense arrays. It first converts everything to coo format matrices, creates a new composite data, row and col arrays, and returns a coo matrix (optionally converting it to another specified format):

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>
like image 151
hpaulj Avatar answered Oct 02 '22 14:10

hpaulj