web-dev-qa-db-ja.com

numpy.loadを使用してファイルから圧縮データ(.npz)をロードする

私は配列を持っています:

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

savez_compressedを使用してファイルに保存します。

>>> with open('afile','w') as f:
        np.savez_compressed(f,data=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

私は明らかに間違っていることをしていますか?

[〜#〜]編集[〜#〜]

@Saullo Castroの答えに続いて、私はこれを試しました:

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

次のエラーが発生しました:

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

編集2

上記のエラーはIDLEにありました。 Ipythonを使用して動作しました。

15
atomh33ls

numpy.loadを使用する場合 ファイル名を渡すことができます で、拡張子が.npzの場合、最初に解凍されます。

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

b['array1']などを実行して、各配列からデータを取得します...

16

f属性を使用して、np.ndarrayを残すこともできます。

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

.npzファイル内の配列の名前/キー(例:arr_0)は、

images.keys()

f属性は、ロードのdocstringに記載されていません。 loadがnpzファイルを読み取ると、class NpzFileのインスタンスを返します。このクラスはnumpy.lib.npyio.NpzFileとして利用できます。 NpzFileクラスのdocstringはf属性を説明します。 (これを書いている時点では、クラスのソースコードは here にあります。

7
mrk

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を再インストールしましたが、問題はすべてなくなりました。基本的に、コンパイラーがアラートを送信しているため、問題はnumpyではなくpythonにあります。したがって、バージョンが問題になる可能性があります。

しかし、npyはnumpyのシリアライズ可能なタイプですが、大きなマトリックスに対してsavez_compressedを使用しても、ファイルが十分に小さいとは思いません。

うまくいけば、あなたの問題は私のものと同じです

1
NineSPRing

ファイルをバイナリとして開いてみてください。

with open('afile','rb') as f:
1
Paul Evans