Matlab 稀疏函数 v.s。 Python Scipy.sparse 图书馆
Matlab Sparse function v.s. Python Scipy.sparse library
我正在将 Matlab 代码翻译成 Python 环境,现在在 Matlab 中使用稀疏函数进行处理。我知道我们有一个名为 scipy.sparse
的库,其中之一是 csc_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
.
但是,当我检查从 scipy.sparse
库计算的那些时,它们与来自 Matlab 的不匹配。
我有大小为 1693872 的大数据,称为 Ig
(1693872,)、Jg
(1693872,) 和 K_dummy
(1693872,),其中 K_dummy(Ig(i),Jg(i)) = K_dummy(i)
。
我已经用 Matlab 检查了所有变量,Ig
、Jg
、K_dummy
并完美匹配。你们有什么想法我必须考虑其他方面吗?
下面是我在python和Matlab中的示例代码,分别作为参考:
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);
其中K_dummy
是(18, 18, 5228)数组,Ig
是(324, 5228)数组,Jg
是(324, 5228)数组,noDofs
是一个 int 变量,如 42442.
八度:
>> 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
在 numpy
中与 scipy.io
和 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>}
并查看稀疏矩阵:
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
并重新创建矩阵
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
使用较新的 numpy/scipy
我不得不将索引转换为整数(他们对浮点数作为索引更加挑剔)。但扁平化似乎效果很好。
你在所有这些变量中都有一个额外的维度,我懒得重新创建。正确展平可能会导致您的问题。
我正在将 Matlab 代码翻译成 Python 环境,现在在 Matlab 中使用稀疏函数进行处理。我知道我们有一个名为 scipy.sparse
的库,其中之一是 csc_matrix((data, (row_ind, col_ind)), [shape=(M, N)])
.
但是,当我检查从 scipy.sparse
库计算的那些时,它们与来自 Matlab 的不匹配。
我有大小为 1693872 的大数据,称为 Ig
(1693872,)、Jg
(1693872,) 和 K_dummy
(1693872,),其中 K_dummy(Ig(i),Jg(i)) = K_dummy(i)
。
我已经用 Matlab 检查了所有变量,Ig
、Jg
、K_dummy
并完美匹配。你们有什么想法我必须考虑其他方面吗?
下面是我在python和Matlab中的示例代码,分别作为参考:
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);
其中K_dummy
是(18, 18, 5228)数组,Ig
是(324, 5228)数组,Jg
是(324, 5228)数组,noDofs
是一个 int 变量,如 42442.
八度:
>> 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
在 numpy
中与 scipy.io
和 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>}
并查看稀疏矩阵:
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
并重新创建矩阵
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
使用较新的 numpy/scipy
我不得不将索引转换为整数(他们对浮点数作为索引更加挑剔)。但扁平化似乎效果很好。
你在所有这些变量中都有一个额外的维度,我懒得重新创建。正确展平可能会导致您的问题。