scipy.io.
mmwrite#
- scipy.io.mmwrite(target, a, comment=None, field=None, precision=None, symmetry='AUTO')[source]#
將稀疏或密集陣列 a 寫入 Matrix Market 檔案類型的 target。
- 參數:
- targetstr 或檔案類型
Matrix Market 檔案名稱 (副檔名 .mtx) 或開啟的檔案類型物件。
- a類陣列
稀疏或密集的 2 維陣列。
- commentstr,選用
要附加到 Matrix Market 檔案的註解。
- fieldNone 或 str,選用
可以是 ‘real’、‘complex’、‘pattern’ 或 ‘integer’。
- precisionNone 或 int,選用
用於顯示實數或複數值的位數。
- symmetryNone 或 str,選用
可以是 ‘AUTO’、‘general’、‘symmetric’、‘skew-symmetric’ 或 ‘hermitian’。如果 symmetry 為 None,則 ‘a’ 的對稱類型由其值決定。如果 symmetry 為 ‘AUTO’,則 ‘a’ 的對稱類型將被決定或設定為 ‘general’,由 mmwrite 自行決定。
- 回傳值:
- None
註解
版本 1.12.0 中變更:C++ 實作。
範例
>>> from io import BytesIO >>> import numpy as np >>> from scipy.sparse import coo_array >>> from scipy.io import mmwrite
將一個小的 NumPy 陣列寫入 matrix market 檔案。該檔案將以
'array'
格式寫入。>>> a = np.array([[1.0, 0, 0, 0], [0, 2.5, 0, 6.25]]) >>> target = BytesIO() >>> mmwrite(target, a) >>> print(target.getvalue().decode('latin1')) %%MatrixMarket matrix array real general % 2 4 1 0 0 2.5 0 0 0 6.25
在輸出檔案中新增註解,並將精度設定為 3。
>>> target = BytesIO() >>> mmwrite(target, a, comment='\n Some test data.\n', precision=3) >>> print(target.getvalue().decode('latin1')) %%MatrixMarket matrix array real general % % Some test data. % 2 4 1.00e+00 0.00e+00 0.00e+00 2.50e+00 0.00e+00 0.00e+00 0.00e+00 6.25e+00
在呼叫
mmwrite
之前,先轉換為稀疏矩陣。這將導致輸出格式為'coordinate'
而不是'array'
。>>> target = BytesIO() >>> mmwrite(target, coo_array(a), precision=3) >>> print(target.getvalue().decode('latin1')) %%MatrixMarket matrix coordinate real general % 2 4 3 1 1 1.00e+00 2 2 2.50e+00 2 4 6.25e+00
將複數 Hermitian 陣列寫入 matrix market 檔案。請注意,實際上只有六個值被寫入檔案;其他值由對稱性暗示。
>>> z = np.array([[3, 1+2j, 4-3j], [1-2j, 1, -5j], [4+3j, 5j, 2.5]]) >>> z array([[ 3. +0.j, 1. +2.j, 4. -3.j], [ 1. -2.j, 1. +0.j, -0. -5.j], [ 4. +3.j, 0. +5.j, 2.5+0.j]])
>>> target = BytesIO() >>> mmwrite(target, z, precision=2) >>> print(target.getvalue().decode('latin1')) %%MatrixMarket matrix array complex hermitian % 3 3 3.0e+00 0.0e+00 1.0e+00 -2.0e+00 4.0e+00 3.0e+00 1.0e+00 0.0e+00 0.0e+00 5.0e+00 2.5e+00 0.0e+00
此方法是多執行緒的。預設執行緒數等於系統中的 CPU 數量。使用 threadpoolctl 覆寫
>>> import threadpoolctl >>> >>> target = BytesIO() >>> with threadpoolctl.threadpool_limits(limits=2): ... mmwrite(target, a)