0

I have a npz file that looks like this.

data = np.load('Data_5_iteration.npz') # Load data from file
keys = list(data.keys()) # all keys in the dictionary
print(keys)
(base) hell@hell:~$ ['nodes', 'temperature', 'max_iter', 'best_error_vec', 'best_u_pred', 'final_error_vec', 'final_u_pred', 'best_iter', 'PDE_loss_array', 'BC_loss_array', 'total_loss_array']

I want to store all these numpy arrays in different arrays with the same name as in the list without writing them line by line.

For example, I don't want to write:

nodes = data[keys[0]]
temperature = data[keys[1]]
max_iter = data[keys[2]]
best_error_vec = data[keys[3]]
best_u_pred = data[keys[4]]
final_error_vec = data[keys[5]]
final_u_pred = data[keys[6]]
best_iter = data[keys[7]]
PDE_loss_array = data[keys[8]]
BC_loss_array = data[keys[9]]
total_loss_array = data[keys[10]]

Can I do this with some automated way?

1 Answer 1

1

Using a sample npz I can get a list or dict:

In [42]: with np.load('data.npz') as f:
    ...:     alist = [(key,f[key]) for key in f]
    ...: 
In [43]: alist
Out[43]: [('fone', array(['t1', 't2', 't3'], dtype='<U2')), ('nval', array([1, 2, 3]))]
In [44]: with np.load('data.npz') as f:
    ...:     adict = {key:f[key] for key in f}
    ...: 
In [45]: adict
Out[45]: {'fone': array(['t1', 't2', 't3'], dtype='<U2'), 'nval': array([1, 2, 3])}

The list could be unpacked with:

In [46]: fone, nval = alist
In [47]: fone
Out[47]: ('fone', array(['t1', 't2', 't3'], dtype='<U2'))

We can also use the dict approach to set variables in the global or local namespace, but that's not encouraged in Python.

Sign up to request clarification or add additional context in comments.

4 Comments

I am getting an error here; fone, nval = alist ValueError: too many values to unpack (expected 2)
You need one variable name per key. There are just 2 in my example.
so what is the point? My question was something else. How to use the dictionary keys as the numpy array name? So, if I have 100 numpy array in npz file, I don't need to write all of them.
Do you really want 100 variables in your script? Even you automate tbe load, you stil have to write them to use any of them

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.