Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save a dictionary of arrays to file in Numpy

Tags:

python

numpy

I have a dict of arrays

{1:array([...]), 2:array([...]), 3:array([...])}

I'd like to save it to a file and load it back later.

I found a numpy had a list of Input and output methods, but seems they only deal with arrays.

Thanks.

like image 440
Harrison Avatar asked Mar 02 '16 01:03

Harrison


Video Answer


2 Answers

The following script will save an dict of numpy arrays to disk and then load it back in to memory.

import numpy as np

arr1 = np.arange(0, 10, 1)
arr2 = np.arange(10, 20, 1)
arr3 = np.arange(20, 30, 1)

dct = {'1': arr1, '2': arr2, '3':arr3}

outfile = 'dict_of_arrays.npz'
np.savez(outfile, **dct)

npzfile = np.load(outfile)
print('npzfile.files: {}'.format(npzfile.files))
print('npzfile["1"]: {}'.format(npzfile["1"]))

Running this script shows the following:

npzfile.files: ['1', '3', '2']
npzfile["1"]: [0 1 2 3 4 5 6 7 8 9]

Note that your dict keys must be strings. Perhaps that was the problem?

I am running numpy 1.10.4

like image 166
Eric Dill Avatar answered Sep 17 '22 23:09

Eric Dill


Actually you can use built-in pickle library to serialize and deserialize your objects without using numpy.

Here is a mock code

import pickle

data1 = {'a': [1, 2.0, 3, 4 + 6j],
         'b': ('string', u'Unicode string'),
         'c': None}

print data1, type(data1)
with open('data.pkl', 'wb') as output:
    # Pickle dictionary using protocol 0.
    pickle.dump(data1, output)

# load data from pkl file
with open("data.pkl", "rb") as fp:
    loaded_data1 = pickle.load(fp)

print loaded_data1, type(loaded_data1)

Results

Before: {'a': [1, 2.0, 3, (4+6j)], 'c': None, 'b': ('string', u'Unicode string')} <type 'dict'>
After: {'a': [1, 2.0, 3, (4+6j)], 'c': None, 'b': ('string', u'Unicode string')} <type 'dict'>

Hope it helps.

like image 23
eric Avatar answered Sep 21 '22 23:09

eric