使用numpy.load从文件加载压缩数据(.npz) [英] Load compressed data (.npz) from file using numpy.load
问题描述
我有一个数组:
>>> data = np.ones((1,3,128))
使用<$ c保存到文件$ c> savez_compressed :
>>>与开放('afile','w')作为f:
np.savez_compressed(f,data = data)
当我尝试加载它时,我似乎无法访问数据:
>>>与开放('afile','r')为f:
b = np.load(f)
>>> b.files
['data']
>>> b ['data']
Traceback(最近一次调用的最后一个):
在< module>中的第1行指定< pyshell#196>
b ['data']
文件C:\Python27\lib\site-packages\\\
umpy\lib\\\
pyio.py,第238行,在__getitem__
bytes = self.zip.read(key)
读取
中的文件C:\Python27\lib\zipfile.py,第828行返回self.open(name,r ,pwd).read()
打开
文件C:\Python27\lib\zipfile.py,第853行zef_file.seek(zinfo.header_offset,0)
ValueError:关闭文件的I / O操作
$ b
编辑
继续@Saullo Castro的回答我试过这个:
>>> np.savez_compressed('afile.npz',data = data)
>>> b = np.load('afile.npz')
>>> b.files
['data']
>>> b ['data']
出现以下错误:
Traceback(最近一次调用最后一次):
在< module>中,第1行的文件< pyshell#253>
b ['data']
文件C:\Python27\lib\site-packages\\\
umpy\lib\\\
pyio.py,第241行,在__getitem__
返回format.read_array(值)
文件C:\ Python27\lib\site-packages\\\
umpy\lib\format.py,行440,在read_array
形状, fortran_order,dtype = read_array_header_1_0(fp)
文件C:\Python27\lib\site-packages\\\
umpy\lib\format.py,第336行,在read_array_header_1_0
d = safe_eval(header)
文件C:\Python27\lib\site-packages\\\
umpy\lib\utils.py,行1156,在safe_eval
ast = compiler.parse (source,mode =eval)
文件C:\Python27\lib\compiler\transformer.py,第53行,解析
return Transformer()。parseexpr(buf )
文件C:\Python27\lib\compiler\transformer.py,第132行,在parseexpr
中返回self.transform(parser.expr(text))
文件C: \\ Python27 \lib\compiler\transformer.py,第124行,转换
返回self.compile_node(树)
文件C:\Python27\\lib\compiler\变量.py,第159行,在compile_node
中返回self.eval_input(node [1:])
文件C:\Python27\lib\compiler\transformer.py,行194,在eval_input
返回表达式(self.com_node(nodelist [0]))
文件C:\Python27\lib\compiler\transformer.py,行805,在com_node
返回self._dispatch [节点[0]](节点[1:])
文件C:\Python27\lib\compiler\transformer.py,第578行,在testlist
返回self.com_binary(Tuple,nodelist)
文件C:\Python27\lib\compiler\transformer.py,第1082行,在com_binary
中返回self.lookup_node (n)(n [1:])
文件C:\Python27\lib\compiler\transformer.py,行596,在测试
then = self.com_node(nodelist [0])
Fil eC:\Python27\lib\compiler\transformer.py,行805,com_node
返回self._dispatch [node [0]](node [1:])
文件C:\Python27\lib\compiler\transformer.py,第610行,在or_test
中返回self.com_binary(或者nodelist)
文件C:\Python27\\ \\ lib\compiler\transformer.py,第1082行,在com_binary
返回self.lookup_node(n)(n [1:])
文件C:\Python27\lib\\ \\ compiler \transformer.py,第615行,在and_test
返回self.com_binary(和,nodelist)
文件C:\Python27\lib\compiler\transformer.py ,第1082行,在com_binary
返回self.lookup_node(n)(n [1:])
文件C:\Python27\\lib\compiler\transformer.py,第619行,在not_test
result = self.com_node(nodelist [-1])$ b $ b文件C:\Python27\lib\compiler\transformer.py,行805,com_node
返回self._dispatch [node [0]](node [1:])
文件C:\Python27\lib\compiler\transformer.py,第626行,比较
node = self.com_node(nodelist [0])
文件C :\Python27\lib\compiler\transformer.py,第805行,com_node
返回self._dispatch [node [0]](node [1:])
文件C :\Python27\lib\compiler\transformer.py,第659行,在expr
返回self.com_binary(Bitor,nodelist)
文件C:\Python27\lib\\ \\ compiler \transformer.py,第1082行,在com_binary
中返回self.lookup_node(n)(n [1:])
文件C:\Python27\\lib\compiler\\ \\ transformer.py,第663行,在xor_expr
返回self.com_binary(Bitxor,nodelist)
文件C:\Python27\lib\compiler\transformer.py,第1082行,在com_binary
中返回self.lookup_node(n)(n [1:])
文件C:\Python27\lib\compiler\transformer.py,行667和in_expr
返回self.com_binary(Bitand, nodelist)
文件C:\Python27\lib\compiler\transformer.py,第1082行,在com_binary
中返回self.lookup_node(n)(n [1:])
文件C:\Python27\lib\compiler\transformer.py,行671,在shift_expr
node = self.com_node(nodelist [0])
文件C :\Python27\lib\compiler\transformer.py,第805行,com_node
返回self._dispatch [node [0]](node [1:])
文件C :\Python27\lib\compiler\transformer.py,第683行,在arith_expr
node = self.com_node(nodelist [0])
文件C:\Python27\ lib_\\compiler\transformer.py,第805行,com_node
返回self._dispatch [node [0]](node [1:])
文件C:\Python27\ lib \ compiler \transformer.py,行695,在
节点= self.com_node(nodelist [0])
文件C:\Python27\lib\compiler\变压器.py,第805行,在com_node
中返回self._dispatch [node [ 0]](node [1:])
文件C:\Python27\lib\compiler\transformer.py,第715行,因子
node = self.lookup_node(nodelist [-1])(nodelist [-1] [1:])
文件C:\Python27\lib\compiler\transformer.py,行727,电源
节点= self.com_node(nodelist [0])
文件C:\Python27\lib\compiler\transformer.py,行805,com_node
返回self._dispatch [node [ 0]](node [1:])
原子
中的文件C:\Python27\lib\compiler\transformer.py,第739行返回self._atom_dispatch [nodelist [ 0] [0]](nodelist)
文件C:\Python27\lib\compiler\transformer.py,第754行,在atom_lbrace
返回self.com_dictorsetmaker(nodelist [1 ])
文件C:\Python27\lib\compiler\transformer.py,行1214,在com_dictorsetmaker
断言nodelist [0] == symbol.dictorsetmaker
AssertionError
编辑2
上面的错误是在IDLE中。它使用Ipython。当使用 numpy.load
一个href =http://docs.scipy.org/doc/numpy/reference/generated/numpy.load.html =noreferrer>可以传递文件名,如果扩展名是 .npz
,它将首先解压:
pre $ n $ c $ np.savez_compressed('filename .npz',array1 = array1,array2 = array2)
b = np.load('filename.npz')
并执行 b ['array1']
等等来检索每个数组中的数据...
I have an array:
>>> data = np.ones((1,3,128))
I save it to file using 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:
>>> 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
The above error was in IDLE. It worked using Ipython.
When using numpy.load
you can pass the file name, and if the extension is .npz
, it will first 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...
这篇关于使用numpy.load从文件加载压缩数据(.npz)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!