[SciPy-User] Reading / writing sparse matrices

Matthew Brett matthew.brett@gmail....
Thu Nov 11 19:27:37 CST 2010


On Fri, Jun 18, 2010 at 6:31 PM, Lutz Maibaum <lutz.maibaum@gmail.com> wrote:
> 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
> dtype('int32')
>>>> 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?

I had a quick look at the code, and then at the Matrix Market format,
and it looks to me:


as if Matrix Market only allows integer, real or complex - hence the
(somewhat unhelpful) error.



More information about the SciPy-User mailing list