{ } Raw JSON

bundles / scipy 1.18.0 / scipy / io / _fast_matrix_market / mmread

function

scipy.io._fast_matrix_market:mmread

source: /scipy/io/_fast_matrix_market/__init__.py:301

Signature

def mmread(source, *, spmatrix = <object object at 0x0000>)

Summary

Reads the contents of a Matrix Market file-like 'source' into a matrix.

Parameters

source : str or file-like

Matrix Market filename (extensions .mtx, .mtz.gz) or open file-like object.

spmatrix : bool, optional (default: True)

If True, return sparse matrix. Otherwise return sparse array.

Returns

a : ndarray or coo_array

Dense or sparse array depending on the matrix format in the Matrix Market file.

Notes

Examples

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)`` returns the data as sparse array in COO format.
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.]])
This method is threaded. The default number of threads is equal to the number of CPUs in the system. Use `threadpoolctl <https://github.com/joblib/threadpoolctl>`_ to override:
import threadpoolctl
with threadpoolctl.threadpool_limits(limits=2):
    m = mmread(StringIO(text), spmatrix=False)
⚠
Traceback (most recent call last):
  File "<doctest scipy.io._fast_matrix_market:mmread[0]>", line 1, in <module>
    with threadpoolctl.threadpool_limits(limits=2):
         ^^^^^^^^^^^^^
NameError: name 'threadpoolctl' is not defined

Aliases

  • scipy.io.mmread

Referenced by