如何以非延迟方式加载npz文件? [英] How to load a npz file in a non-lazy way?

查看:123
本文介绍了如何以非延迟方式加载npz文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NumPy的 load() 函数npz文件时,href ="https://stackoverflow.com/a/34119852/395857">返回惰性文件加载器,而不是实际数据.如何加载npz文件,以便将数据加载到内存中?

NumPy's load() function returns a lazy file loader, not the actual data, when loading a npz file. How to load a npz file so that the data get loaded in memory?

推荐答案

如果要强制读取和解压缩数组的内容,只需将其内容分配给变量,例如:

If you want to force the contents of the arrays to be read and decompressed, just assign their contents to variables, e.g.:

data = np.load('/path/to/data.npz', 'r')
a = data['a']
b = data['b']
# etc

如果您希望保持与惰性加载器完全相同的语法,则可以简单地将所有数组加载到字典中,例如:

If you wanted to keep the exact same syntax as with the lazy loader, you could simply load all of the arrays into a dict, e.g.:

data_dict = dict(data)

所以现在您可以使用

data_dict['a']

以在脚本的后续部分中引用a.不过,就我个人而言,我不会遵守该命令,因为它保留了对所有数组的引用这一事实将防止稍后在脚本中对任何未使用的单个数组进行垃圾回收.

to refer to a in later parts of your script. Personally I wouldn't keep the dict around, though, since the fact that it holds references to all of the arrays would prevent any individual unused ones from being garbage collected later on in your script.

这篇关于如何以非延迟方式加载npz文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆