scipy.io.
mmread#
- scipy.io.mmread(source, *, spmatrix=True)[source]#
读取 Matrix Market 文件(或类似文件)'source' 的内容到矩阵中。
- 参数:
- sourcestr 或 类文件对象
Matrix Market 文件名(扩展名 .mtx, .mtz.gz)或已打开的类文件对象。
- spmatrix布尔值, 可选 (默认: True)
如果为
True
,则返回稀疏coo_matrix
。否则返回coo_array
。
- 返回:
- andarray 或 coo_array
密集或稀疏数组,取决于 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), spmatrix=False) >>> m <COOrdinate sparse array 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), spmatrix=False)