[SciPy-User] Reading / writing sparse matrices

Lutz Maibaum lutz.maibaum@gmail....
Fri Jun 18 20:31:59 CDT 2010

How can I write a sparse matrix with elements of type uint64 to a file, and recover it while preserving the data type? For example:

>>> import numpy as np
>>> import scipy.sparse
>>> a=scipy.sparse.lil_matrix((5,5), dtype=np.uint64)
>>> a[0,0]=9876543210

Now I save this matrix to a file:

>>> import scipy.io
>>> scipy.io.mmwrite("test.mtx", a, field='integer')

If I do not specify the field argument of mmwrite, I get a "unexpected dtype of kind u" exception. The generated file test.mtx looks as expected. But when I try to read this matrix, it is converted to int32:

>>> b=scipy.io.mmread("test.mtx")
>>> b.dtype
>>> b.data
array([-2147483648], dtype=int32)

As far as I can tell, it is not possible to specify a dtype when calling mmread. Is there a better way to go about this?

Any help is much appreciated.


More information about the SciPy-User mailing list