[Numpy-discussion] save a matrix

Charles R Harris charlesr.harris at gmail.com
Thu Nov 30 23:19:24 CST 2006


On 11/30/06, Charles R Harris <charlesr.harris at gmail.com> wrote:
>
>
>
> On 11/30/06, Keith Goodman <kwgoodman at gmail.com> wrote:
> >
> > What's a good way to save matrix objects to file for later use? I just
> > need something quick for debugging.
> >
> > I saw two suggestions on this list from Francesc Altet (2006-05-22):
> >
> > 1. Use tofile and fromfile and save the meta data yourself.
> >
> > 2. pytables
> >
> > Any suggestions for #3?
>
> Is this what you want?
>
> In [14]: a
> Out[14]:
> matrix([[2, 3],
>         [4, 5]])
>
> In [15]: b
> Out[15]:
> matrix([[2, 3],
>         [4, 5]])
>
> In [16]: f = open(' dump.pkl','w')
>
> In [17]: pickle.dump(a,f)
>
> In [18]: pickle.dump(b,f)
>
> In [19]: f.close()
>
> In [20]: f = open('dump.pkl','r')
>
> In [21]: x = pickle.load(f)
>
> In [22]: y = pickle.load(f)
>
> In [23]: f.close()
>
> In [24]: x
> Out[24]:
> matrix([[2, 3],
>         [4, 5]])
>
> In [25]: y
> Out[25]:
> matrix([[2, 3],
>         [4, 5]])
>

It is also possible to put the variables of interest in a dictionary, then
pickle the dictionary. That way you can also store the variable names.

In [27]: f = open('dump.pkl','w')

In [28]: pickle.dump( {'a':a,'b':b}, f)

In [29]: f.close()

In [30]: f = open('dump.pkl','r')

In [31]: mystuff = pickle.load(f)

In [32]: f.close()

In [34]: mystuff
Out[34]:
{'a': matrix([[2, 3],
        [4, 5]]), 'b': matrix([[2, 3],
        [4, 5]])}

I think you can actually pickle the whole evironment, but I don't recall
how.

Chuck
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061130/a6f167ee/attachment.html 


More information about the Numpy-discussion mailing list