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?

U3.1415926
  • 812
  • 12
  • 30
lovespeed
  • 4,835
  • 15
  • 41
  • 54
  • 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. – Cong Ma Sep 20 '15 at 18:49
  • @CongMa [exactly](http://docs.scipy.org/doc/numpy/reference/generated/numpy.load.html). The second example shows how this is accomplished. – MattDMo Sep 20 '15 at 18:52
  • You can read this as well https://stackoverflow.com/a/71183327/16733101 – Hamzah Feb 19 '22 at 08:34

3 Answers3

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)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
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.)

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
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 !!

Exa
  • 4,020
  • 7
  • 43
  • 60
senior_mle
  • 809
  • 1
  • 10
  • 20