Python npy文件如何访问变量

2024-09-30 23:29:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用下面的tensorlayer命令创建了一个npy数据集。在

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

我可以使用

^{pr2}$

我想这应该类似于字典(print(data)作品)。因此,我努力了

actions = data['actions'] 

但这给了我以下错误

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']

Tags: tonameactionsonlydatasaveanyfiles