scipy.io.
mmread#
- scipy.io.mmread(source, *, spmatrix=True)[原始碼]#
將 Matrix Market 檔案類型的 'source' 內容讀取到矩陣中。
- 參數:
- sourcestr 或檔案類型
Matrix Market 檔名 (副檔名為 .mtx, .mtz.gz) 或開啟的檔案類型物件。
- spmatrixbool,選用性 (預設值: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)