當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy io.mmread用法及代碼示例

本文簡要介紹 python 語言中 scipy.io.mmread 的用法。

用法:

scipy.io.mmread(source)#

將矩陣市場 file-like ‘source’ 的內容讀取到矩陣中。

參數

source str 或 file-like

Matrix Market 文件名(擴展名 .mtx、.mtz.gz)或打開 file-like 對象。

返回

a ndarray 或coo_matrix

密集或稀疏矩陣取決於矩陣市場文件中的矩陣格式。

注意

例子

>>> 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
<5x5 sparse matrix of type '<class 'numpy.float64'>'
with 7 stored elements in COOrdinate format>
>>> m.A
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))

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.io.mmread。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。