简体   繁体   中英

Python npy file how to access variables

I have created a npy dataset by using the following tensorlayer command.

tl.files.save_any_to_npy(
save_dict={
    'images': aggregated_images, 
    'actions': aggregated_actions,
    'rewards': aggregated_rewards}, 
    name='./data/episode0.npy')

I am able to load the file (rewards/actions are arrays of scalars; images is an array of matrices) by using

import numpy as np
data = np.load('./data/episode0.npy')

I thought this would be similar to a dictionary ( print(data) works). Hence, I tried

actions = data['actions'] 

but this gives me the following error

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
>>> actions = data['rewards']
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

How can I resolve this error? I think I could use three variables to have a workaround, but I would rather like to only keep track of one file with all the.

Solution (credit goes to Goyo):

 import tensorlayer as tl data = tl.files.load_npy_to_any(path='./data', name='episode0.npy') actions = data['actions'] 

Try this:

data = np.load('./data/episode0.npy').item()
data["actions"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM