scipy.io.mmread#

scipy.io.mmread(source)[source]#

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

Parameters:
sourcestr or file-like

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

Returns:
andarray or coo_matrix

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

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 matrix in COO format.

>>> 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.]])