scipy.io.

mmread#

scipy.io.mmread(source)[源代码][源代码]#

将类似于 ‘source’ 的 Matrix Market 文件内容读入矩阵。

参数:
源代码str 或 file-like

Matrix Market 文件名(扩展名 .mtx, .mtz.gz)或类文件对象。

返回:
andarray 或 coo_matrix

根据Matrix Market文件中的矩阵格式,可以是密集矩阵或稀疏矩阵。

注释

在 1.12.0 版本发生变更: C++ 实现。

示例

>>> from io import StringIO
>>> from scipy.io import mmread
>>> text = '''%%MatrixMarket matrix coordinate real general
...  5 5 7
...  2 3 1.0
...  3 4 2.0
...  3 5 3.0
...  4 1 4.0
...  4 2 5.0
...  4 3 6.0
...  4 4 7.0
... '''

mmread(source) 以 COO 格式返回数据作为稀疏矩阵。

>>> m = mmread(StringIO(text))
>>> m
<COOrdinate sparse matrix of dtype 'float64'
    with 7 stored elements and shape (5, 5)>
>>> m.toarray()
array([[0., 0., 0., 0., 0.],
       [0., 0., 1., 0., 0.],
       [0., 0., 0., 2., 3.],
       [4., 5., 6., 7., 0.],
       [0., 0., 0., 0., 0.]])

此方法是多线程的。默认的线程数等于系统中的CPU数量。使用 threadpoolctl 来覆盖:

>>> import threadpoolctl
>>>
>>> with threadpoolctl.threadpool_limits(limits=2):
...     m = mmread(StringIO(text))