Python 使用 numpy.load 从文件加载压缩数据 (.npz)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18231135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:15:46  来源:igfitidea点击:

Load compressed data (.npz) from file using numpy.load

pythonarraysnumpyfile-io

提问by atomh33ls

I have an array:

我有一个数组:

>>> data = np.ones((1,3,128))

I save it to file using savez_compressed:

我使用savez_compressed以下命令将其保存到文件中:

>>> with open('afile','w') as f:
        np.savez_compressed(f,data=data)

When I try to load it I don't seem to be able to access the data:

当我尝试加载它时,我似乎无法访问数据:

>>> with open('afile','r') as f:
        b=np.load(f)
>>> b.files
['data']
>>> b['data']

Traceback (most recent call last):
  File "<pyshell#196>", line 1, in <module>
    b['data']
  File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 238, in __getitem__
    bytes = self.zip.read(key)
  File "C:\Python27\lib\zipfile.py", line 828, in read
    return self.open(name, "r", pwd).read()
  File "C:\Python27\lib\zipfile.py", line 853, in open
    zef_file.seek(zinfo.header_offset, 0)
ValueError: I/O operation on closed file

Am I doing something obviously wrong?

我做错了什么吗?

EDIT

编辑

Following @Saullo Castro's answer I tried this:

按照@Saullo Castro 的回答,我尝试了这个:

>>> np.savez_compressed('afile.npz',data=data)
>>> b=np.load('afile.npz')
>>> b.files
['data']
>>> b['data']

and got the following error:

并得到以下错误:

Traceback (most recent call last):
  File "<pyshell#253>", line 1, in <module>
    b['data']
  File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 241, in __getitem__
    return format.read_array(value)
  File "C:\Python27\lib\site-packages\numpy\lib\format.py", line 440, in read_array
    shape, fortran_order, dtype = read_array_header_1_0(fp)
  File "C:\Python27\lib\site-packages\numpy\lib\format.py", line 336, in read_array_header_1_0
    d = safe_eval(header)
  File "C:\Python27\lib\site-packages\numpy\lib\utils.py", line 1156, in safe_eval
    ast = compiler.parse(source, mode="eval")
  File "C:\Python27\lib\compiler\transformer.py", line 53, in parse
    return Transformer().parseexpr(buf)
  File "C:\Python27\lib\compiler\transformer.py", line 132, in parseexpr
    return self.transform(parser.expr(text))
  File "C:\Python27\lib\compiler\transformer.py", line 124, in transform
    return self.compile_node(tree)
  File "C:\Python27\lib\compiler\transformer.py", line 159, in compile_node
    return self.eval_input(node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 194, in eval_input
    return Expression(self.com_node(nodelist[0]))
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 578, in testlist
    return self.com_binary(Tuple, nodelist)
  File "C:\Python27\lib\compiler\transformer.py", line 1082, in com_binary
    return self.lookup_node(n)(n[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 596, in test
    then = self.com_node(nodelist[0])
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 610, in or_test
    return self.com_binary(Or, nodelist)
  File "C:\Python27\lib\compiler\transformer.py", line 1082, in com_binary
    return self.lookup_node(n)(n[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 615, in and_test
    return self.com_binary(And, nodelist)
  File "C:\Python27\lib\compiler\transformer.py", line 1082, in com_binary
    return self.lookup_node(n)(n[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 619, in not_test
    result = self.com_node(nodelist[-1])
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 626, in comparison
    node = self.com_node(nodelist[0])
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 659, in expr
    return self.com_binary(Bitor, nodelist)
  File "C:\Python27\lib\compiler\transformer.py", line 1082, in com_binary
    return self.lookup_node(n)(n[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 663, in xor_expr
    return self.com_binary(Bitxor, nodelist)
  File "C:\Python27\lib\compiler\transformer.py", line 1082, in com_binary
    return self.lookup_node(n)(n[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 667, in and_expr
    return self.com_binary(Bitand, nodelist)
  File "C:\Python27\lib\compiler\transformer.py", line 1082, in com_binary
    return self.lookup_node(n)(n[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 671, in shift_expr
    node = self.com_node(nodelist[0])
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 683, in arith_expr
    node = self.com_node(nodelist[0])
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 695, in term
    node = self.com_node(nodelist[0])
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 715, in factor
    node = self.lookup_node(nodelist[-1])(nodelist[-1][1:])
  File "C:\Python27\lib\compiler\transformer.py", line 727, in power
    node = self.com_node(nodelist[0])
  File "C:\Python27\lib\compiler\transformer.py", line 805, in com_node
    return self._dispatch[node[0]](node[1:])
  File "C:\Python27\lib\compiler\transformer.py", line 739, in atom
    return self._atom_dispatch[nodelist[0][0]](nodelist)
  File "C:\Python27\lib\compiler\transformer.py", line 754, in atom_lbrace
    return self.com_dictorsetmaker(nodelist[1])
  File "C:\Python27\lib\compiler\transformer.py", line 1214, in com_dictorsetmaker
    assert nodelist[0] == symbol.dictorsetmaker
AssertionError

EDIT 2

编辑 2

The above error was in IDLE. It worked using Ipython.

上面的错误是在空闲状态。它使用 Ipython 工作。

采纳答案by Saullo G. P. Castro

When using numpy.loadyou can pass the file name, and if the extension is .npz, it will first descompress:

使用时numpy.load可以通过文件名,如果扩展是.npz,它会先descompress:

np.savez_compressed('filename.npz', array1=array1, array2=array2)
b = np.load('filename.npz')

and do b['array1']and so forth to retrieve the data from each array...

并执行b['array1']等等以从每个数组中检索数据...

回答by Paul Evans

Try opening the file as binary:

尝试以二进制形式打开文件:

with open('afile','rb') as f:

回答by NineSPRing

I do have the same problem(AssertionError) when using numpy 1.7.1/1.8.0 with python 2.7.6 both on MAC OS and Windows. But the problem was automatically fixed after I switch to linux with python 2.7.5. Then I reinstall python 2.7.5 on MACOS and Windows and all the problem was gone. Basically the problem is with python instead of numpy as the compiler is sending alert.So mightly the version matters.

在 MAC OS 和 Windows 上使用 numpy 1.7.1/1.8.0 和 python 2.7.6 时,我确实遇到了同样的问题(AssertionError)。但是在我用python 2.7.5切换到linux后问题自动修复。然后我在 MACOS 和 Windows 上重新安装了 python 2.7.5,所有问题都消失了。基本上问题在于python而不是numpy,因为编译器正在发送警报。所以版本可能很重要。

But though npy is the serializable type of numpy, I don't think the file is small enough even with savez_compressed for large matrix.

但是,尽管 npy 是 numpy 的可序列化类型,但我认为即使将 savez_compressed 用于大矩阵,文件也不够小。

Hopefully your problem is the same with mine

希望你的问题和我的一样

回答by mrk

You can also use the fattribute, which leaves you with a np.ndarray:

您还可以使用该f属性,它会给您留下一个 np.ndarray

images = np.load('images.npz')
images = images.f.arr_0

The name/key of the array inside the .npz-file (e.g. arr_0) can be found through

arr_0可以通过以下方式找到.npz 文件(例如)中数组的名称/键

images.keys()

Note: The fattribute is not documented in the docstring of load. When load reads an npzfile, it returns an instance of the class NpzFile. This class is available as numpy.lib.npyio.NpzFile. The docstring of the NpzFileclass describes the fattribute. (As of this writing, the source code of the class can be found here.

注意:该f属性未记录在 load 的文档字符串中。当 load 读取一个npz文件时,它返回一个class NpzFile. 此类可作为numpy.lib.npyio.NpzFile. NpzFile类的文档字符串描述了f属性。(在撰写本文时,可以在此处找到该类的源代码。