nairouz mrabah
nairouz mrabah

Reputation: 1217

Saving numpy arrays as a dictionary

I'm saving 2 Numpy arrays as a dictionary.
When I load the data from the binary file, I get another ndarray. Can I use the loaded Numpy array as a dictionary?

Here is my code and the output of my script:

import numpy as np

x = np.arange(10)
y = np.array([100, 101, 102, 103, 104, 105, 106, 107])
z = {'X': x, 'Y': y}
np.save('./data.npy', z)
z1 = np.load('./data.npy')
print(type(z1))
print(z1)
print(z1['X']) #this line will generate an error

Output: {'X': array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]), 'Y': array([100, 101, 102, 103, 104, 105, 106, 107])}

Upvotes: 0

Views: 1797

Answers (3)

jpp
jpp

Reputation: 164693

An alternative and underused method of storing numpy arrays is HDF5. The benefits are:

  • Transportability, i.e. not Python-specific like pickle
  • Ability to access data out-of-memory & chunking options for optimisation
  • Compression options to optimize read or write performance

Here's a demo:

import h5py, numpy as np

x = np.arange(10)
y = np.array([100, 101, 102, 103, 104, 105, 106, 107])
z = {'X': x, 'Y': y}

with h5py.File('file.h5', 'w', libver='latest') as f:  # use 'latest' for performance
    for k, v in z.items():
        f.create_dataset('dict/'+str(k), data=v)

with h5py.File('file.h5', 'r', libver='latest') as f:
    x_read = f['dict']['X'][:]  # [:] syntax extracts numpy array into memory
    y_read = f['dict']['Y'][:]

print(x_read)

array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

Upvotes: 1

Davide Fiocco
Davide Fiocco

Reputation: 5914

Another option to access data from z1 should be:

z1.flatten()[0]['X']

Upvotes: 1

jpp
jpp

Reputation: 164693

Yes, you can access the underlying dictionary in a 0-dimensional array. Try z1[()].

Here's a demo:

np.save('./data.npy', z)
d = np.load('./data.npy')[()]

print(type(d))
<class 'dict'>

print(d['X'])
[0 1 2 3 4 5 6 7 8 9]

Upvotes: 5

Related Questions