将 scipy.sparse 矩阵转换为等效的 MATLAB 稀疏矩阵
Converting a scipy.sparse matrix into an equivalent MATLAB sparse matrix
我有一个 scipy.sparse.lil_matrix
,我想使用 MATLAB Engine API for Python 将其输入到 MATLAB 方法(不是我编写的)中。到目前为止我看到的帖子要么是关于如何将 MATLAB 稀疏矩阵转换为 python 等效矩阵,要么是它们需要修改我宁愿绕过的 matlab 代码。
我相信 MATLAB 在内部使用类似 csc
的格式。但是构造是(至少当我几年前使用它时)使用 coo
样式输入 - 数据、行、列。
我建议在 MATLAB 中制作一个稀疏矩阵,并将其保存(在 HDF5 之前的模式中)到 .mat。然后用 scipy.io.loadmat
加载它。然后在将 scipy.sparse
矩阵写回 .mat
.
时使用该结果作为指导
scipy.sparse
有一个save
函数,但是它使用np.savez
来写入各自的属性数组。如果您有可以处理 .npy
文件的 MATLAB 代码,您可能可以加载这样的保存(再次使用 coo
格式)。
===
一个测试。
创建并保存稀疏矩阵:
In [263]: from scipy import io, sparse
In [264]: M = sparse.random(10,10,.2,'coo')
In [265]: io.savemat('sparse.mat', {'M':M})
Python 侧的测试负载:
In [268]: io.loadmat('sparse.mat')
Out[268]:
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Wed Jul 3 11:41:23 2019',
'__version__': '1.0',
'__globals__': [],
'M': <10x10 sparse matrix of type '<class 'numpy.float64'>'
with 20 stored elements in Compressed Sparse Column format>}
所以savemat在保存前将coo
格式转换为csc
。
在 Octave 会话中:
>> load sparse.mat
>> M
M =
Compressed Column Sparse (rows = 10, cols = 10, nnz = 20 [20%])
(4, 1) -> 0.41855
(6, 1) -> 0.33456
(7, 1) -> 0.47791
(4, 3) -> 0.27464
(2, 4) -> 0.96700
(3, 4) -> 0.60283
(10, 4) -> 0.41400
(1, 5) -> 0.57004
(2, 5) -> 0.44211
(1, 6) -> 0.63884
(3, 7) -> 0.012127
(8, 7) -> 0.77328
(8, 8) -> 0.25287
(10, 8) -> 0.46280
(1, 9) -> 0.0022617
(6, 9) -> 0.70874
(1, 10) -> 0.79101
(3, 10) -> 0.81999
(6, 10) -> 0.12515
(9, 10) -> 0.60660
所以看起来 savemat/loadmat
代码以 MATLAB 兼容的方式处理稀疏矩阵。
我有一个 scipy.sparse.lil_matrix
,我想使用 MATLAB Engine API for Python 将其输入到 MATLAB 方法(不是我编写的)中。到目前为止我看到的帖子要么是关于如何将 MATLAB 稀疏矩阵转换为 python 等效矩阵,要么是它们需要修改我宁愿绕过的 matlab 代码。
我相信 MATLAB 在内部使用类似 csc
的格式。但是构造是(至少当我几年前使用它时)使用 coo
样式输入 - 数据、行、列。
我建议在 MATLAB 中制作一个稀疏矩阵,并将其保存(在 HDF5 之前的模式中)到 .mat。然后用 scipy.io.loadmat
加载它。然后在将 scipy.sparse
矩阵写回 .mat
.
scipy.sparse
有一个save
函数,但是它使用np.savez
来写入各自的属性数组。如果您有可以处理 .npy
文件的 MATLAB 代码,您可能可以加载这样的保存(再次使用 coo
格式)。
===
一个测试。
创建并保存稀疏矩阵:
In [263]: from scipy import io, sparse
In [264]: M = sparse.random(10,10,.2,'coo')
In [265]: io.savemat('sparse.mat', {'M':M})
Python 侧的测试负载:
In [268]: io.loadmat('sparse.mat')
Out[268]:
{'__header__': b'MATLAB 5.0 MAT-file Platform: posix, Created on: Wed Jul 3 11:41:23 2019',
'__version__': '1.0',
'__globals__': [],
'M': <10x10 sparse matrix of type '<class 'numpy.float64'>'
with 20 stored elements in Compressed Sparse Column format>}
所以savemat在保存前将coo
格式转换为csc
。
在 Octave 会话中:
>> load sparse.mat
>> M
M =
Compressed Column Sparse (rows = 10, cols = 10, nnz = 20 [20%])
(4, 1) -> 0.41855
(6, 1) -> 0.33456
(7, 1) -> 0.47791
(4, 3) -> 0.27464
(2, 4) -> 0.96700
(3, 4) -> 0.60283
(10, 4) -> 0.41400
(1, 5) -> 0.57004
(2, 5) -> 0.44211
(1, 6) -> 0.63884
(3, 7) -> 0.012127
(8, 7) -> 0.77328
(8, 8) -> 0.25287
(10, 8) -> 0.46280
(1, 9) -> 0.0022617
(6, 9) -> 0.70874
(1, 10) -> 0.79101
(3, 10) -> 0.81999
(6, 10) -> 0.12515
(9, 10) -> 0.60660
所以看起来 savemat/loadmat
代码以 MATLAB 兼容的方式处理稀疏矩阵。