lovespeed
lovespeed

Reputation: 5055

Loading arrays from numpy npz files in python

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?

Upvotes: 6

Views: 41799

Answers (3)

senior_mle
senior_mle

Reputation: 849

# 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 !!

Upvotes: 1

Warren Weckesser
Warren Weckesser

Reputation: 114976

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.)

Upvotes: 7

Padraic Cunningham
Padraic Cunningham

Reputation: 180540

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)

Upvotes: 8

Related Questions