简体   繁体   中英

Matlab Sparse function v.s. Python Scipy.sparse library

I am translating Matlab code into Python environment, and now handling with sparse function in Matlab. I know that we have the library called scipy.sparse and the one of them such that csc_matrix((data, (row_ind, col_ind)), [shape=(M, N)]) .

However, when I check those computed from scipy.sparse library, they don't match the one from Matlab. I have big data sized of 1693872 called Ig (1693872,), Jg (1693872,), and K_dummy (1693872,), where K_dummy(Ig(i),Jg(i)) = K_dummy(i) .

I have already check all the variables, Ig , Jg , K_dummy with Matlab and matches perfectly. Do you guys have any idea that I have to consider other aspects?

Here is my sample code in python and Matlab, respectively as references:

K = csc_matrix((K_dummy.flatten('F'),(Ig.flatten('F')-1,Jg.flatten('F')-1)),shape=(noDofs,noDofs))

K = sparse(Ig(:),Jg(:),K_dummy_python(:),noDofs,noDofs);

where K_dummy is (18, 18, 5228) array, Ig is (324, 5228) array, Jg is (324, 5228) array, and noDofs is an int variable as 42442.

In Octave:

>> data=[1,2;3,4];
>> I=[1,2,3,4];
>> J=[2,3,4,2];
>> M = sparse(I(:),J(:),data(:))
M =

Compressed Column Sparse (rows = 4, cols = 4, nnz = 4 [25%])

  (1, 2) ->  1
  (4, 2) ->  4
  (2, 3) ->  3
  (3, 4) ->  2

>> full(M)
ans =

   0   1   0   0
   0   0   3   0
   0   0   0   2
   0   4   0   0

>> save -7 sparse1.mat data I J M

In numpy with scipy.io and scipy.sparse :

In [57]: d = loadmat('sparse1.mat')                                                              
In [58]: d                                                                                       
Out[58]: 
{'__header__': b'MATLAB 5.0 MAT-file, written by Octave 4.2.2, 2020-01-26 19:36:04 UTC',
 '__version__': '1.0',
 '__globals__': [],
 'data': array([[1., 2.],
        [3., 4.]]),
 'I': array([[1., 2., 3., 4.]]),
 'J': array([[2., 3., 4., 2.]]),
 'M': <4x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Column format>}

And looking at the sparse matrix:

In [59]: d['M'].A                                                                                
Out[59]: 
array([[0., 1., 0., 0.],
       [0., 0., 3., 0.],
       [0., 0., 0., 2.],
       [0., 4., 0., 0.]])
In [60]: print(d['M'])                                                                           
  (0, 1)    1.0
  (3, 1)    4.0
  (1, 2)    3.0
  (2, 3)    2.0

And recreating the matrix

In [61]: M1 = sparse.csc_matrix((d['data'].flatten('F'), 
                (d['I'].astype(int).flatten('F')-1, 
                 d['J'].astype(int).flatten('F')-1)))                                                        
In [62]: M1                                                                                      
Out[62]: 
<4x4 sparse matrix of type '<class 'numpy.float64'>'
    with 4 stored elements in Compressed Sparse Column format>
In [63]: M1.A                                                                                    
Out[63]: 
array([[0., 1., 0., 0.],
       [0., 0., 3., 0.],
       [0., 0., 0., 2.],
       [0., 4., 0., 0.]])
In [64]: print(M1)                                                                               
  (0, 1)    1.0
  (3, 1)    4.0
  (1, 2)    3.0
  (2, 3)    2.0

With a newer numpy/scipy I had to convert the indices to integer (they've gotten pickier about floats as indices). But the flattening seems to work fine.

You have an added dimension in all those variables, which I was too lazy to recreate. Flattening that correctly might be causing your problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM