2013-08-14 88 views
6

Tôi có một mảng:tải nén dữ liệu (.npz) từ tập tin sử dụng numpy.load

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

tôi lưu nó vào tập tin sử dụng savez_compressed:

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

Khi tôi cố gắng để tải nó tôi dường như không thể truy cập dữ liệu:

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

Tôi đang làm điều gì đó rõ ràng là sai?

EDIT

Sau câu trả lời @Saullo Castro Tôi cố gắng này:

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

và đã nhận lỗi sau:

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

Lỗi trên là IDLE. Nó hoạt động bằng cách sử dụng Ipython.

Trả lời

8

Khi sử dụng numpy.load bạn can pass the file name, và nếu phần mở rộng là .npz, nó sẽ descompress đầu tiên:

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

và làm b['array1'] và vân vân để truy xuất dữ liệu từ mỗi mảng ...

+0

Cảm ơn, tôi nhận được một lỗi xác nhận khi tôi thử điều này mặc dù .. truy nguyên trong câu hỏi đã chỉnh sửa – atomh33ls

+0

Loại dữ liệu nào bạn có trong 'dữ liệu', đây có phải là một mảng các đối tượng không? –

+0

Nổi. Tôi đã sử dụng 'data = np.ones ((1,3,128))' trong trường hợp này. – atomh33ls

0

Thử mở file như nhị phân:

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

Cảm ơn, tôi vẫn gặp lỗi tương tự mặc dù .. – atomh33ls

+0

Hãy thử thụt lề tất cả các dòng sau khi mở! Tệp của bạn sẽ bị đóng khi nó rời khỏi khối 'with'. –

+0

Cảm ơn, đó là lỗi đánh máy trong câu hỏi - đã sửa nó. Tôi vẫn gặp lỗi tương tự. – atomh33ls

0

Tôi có cùng một vấn đề (AssertionError) khi sử dụng ng ần 1.7.1/1.8.0 với python 2.7.6 cả trên MAC OS và Windows. Nhưng vấn đề đã được tự động cố định sau khi tôi chuyển sang Linux với python 2.7.5. Sau đó, tôi cài đặt lại python 2.7.5 trên MACOS và Windows và tất cả các vấn đề đã biến mất. Về cơ bản vấn đề là với python thay vì numpy như trình biên dịch gửi cảnh báo. Rất có thể là phiên bản quan trọng.

Nhưng mặc dù npy là loại có thể tuần tự được gọn gàng, tôi không nghĩ tệp đó đủ nhỏ ngay cả khi savez_compressed cho ma trận lớn.

Hy vọng rằng vấn đề của bạn là như nhau với tôi

1

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

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

Tên/chìa khóa của mảng bên trong .npz-file (ví dụ arr_0) có thể được tìm thấy thông qua

images.keys() 

Lưu ý: Thuộc tính f không được ghi lại trong chuỗi tải. Khi tải đọc một tập tin npz, nó trả về một thể hiện của class NpzFile. Lớp học này có sẵn là numpy.lib.npyio.NpzFile. Chuỗi tài liệu của lớp NpzFile mô tả thuộc tính f. (Theo văn bản này, mã nguồn của lớp có thể được tìm thấy here.