6

I usually save data in npz files in python. How to write a function which loads the npz file and automatically creates arrays which are present in the .npz file. For example, say there are three arrays A, B, and C in a file named some_data.npz.

What I want the function to do is load the npz file as

data1 = np.load('some_data.npz')

and then automatically create three arrays named data1A, data1B and data1C which stores the arrays A, B, and C from the original .npz file. How to do this?

3
  • Doesn't the numpy.load function already give you a dictionary of arrays? If you just want to bind each individual array to some fancy names, just assign them. Possibly, write a class and make the three names class attributes. Commented Sep 20, 2015 at 18:49
  • @CongMa exactly. The second example shows how this is accomplished. Commented Sep 20, 2015 at 18:52
  • You can read this as well stackoverflow.com/a/71183327/16733101 Commented Feb 19, 2022 at 8:34

3 Answers 3

8

If you want to create names store the arrays in a dict:

a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
a3 = np.array([7,8,9])

np.savez("test", A=a1,B=a2,C=a3)
a = np.load("test.npz")
d = dict(zip(("data1A","data1B","data1C"), (a[k] for k in a)))
print(d)
{'data1A': array([4, 5, 6]), 'data1C': array([7, 8, 9]), 'data1B': array([1, 2, 3])}

If you want to create the keys without passing the names explicitly:

a1 = np.array([1, 2, 3])
a2 = np.array([4, 5, 6])
a3 = np.array([7, 8, 9])

np.savez("test", A=a1,B=a2,C=a3)
a = np.load("test.npz")

d = dict(zip(("data1{}".format(k) for k in a), (a[k] for k in a)))
print(d)
Sign up to request clarification or add additional context in comments.

3 Comments

Is this not essentially the same as the object returned by np.load? Do correct me if I'm wrong.
@areuexperienced, load does not let you pick the name so if you want to store all the arrays with certain names then this is how to do it
Acknowledged. It's just that one would (or should) usually specify names at savetime, hence making it redundant. Your comment still stands though.
7

You can almost do that already, via the f attribute of the object returned by numpy.load. For example, in the following, foo.npz contains three arrays, A, B and C:

In [1367]: foo = np.load('foo.npz')

In [1368]: foo.keys()
Out[1368]: ['A', 'C', 'B']

In [1369]: foo.f.A
Out[1369]: array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.])

In [1370]: foo.f.B
Out[1370]: 
array([[ 0,  1],
       [-1,  0]])

In [1371]: foo.f.C
Out[1371]: array([ 3.14159265,  2.71828183,  0.57721566])

Note: The f attribute is not documented in the docstring of load. When load reads an npz file, it returns an instance of the class NpzFile. This class is available as numpy.lib.npyio.NpzFile. The docstring of the NpzFile class describes the f attribute. (As of this writing, the source code of the class can be found here: https://github.com/numpy/numpy/blob/master/numpy/lib/npyio.py#L95.)

Comments

1
# Assuming that you saved the original data with labels 'A', 'B', and 'C'
import numpy as np

a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
a3 = np.array([7,8,9])
# Save the arrays:
np.savez_compressed('some_data.npz', A=a1,B=a2,C=a3)

# Now Load Using,
data1 = np.load('some_data.npz', 'r')
data1A = data1['A']
data1B = data1['B']
data1C = data1['C']

Hope this helps !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.