2013-11-21 42 views
5

我試圖壓縮我的數組這樣與pytables壓縮陣列

import numpy as np 
import tables 
from contextlib import closing 

FILTERS = tables.Filters(complib='zlib', complevel=5) 

data = np.zeros(10**7) 

with closing(tables.open_file('compressed', mode='w', filters=FILTERS)) as hdf: 
    hdf.create_array('/', 'array', obj=data) 

with closing(tables.open_file('uncompressed', mode='w')) as hdf: 
    hdf.create_array('/', 'array', obj=data) 

但它不會在所有

-rw-rw-r-- 1 user user 80002360 2013-11-21 15:27 compressed 
-rw-rw-r-- 1 user user 80002304 2013-11-21 15:28 uncompressed 

工作,我在這裏幹什麼什麼了嗎?

回答

7

陣列本身不能被壓縮。壓縮需要分塊,因此您必須使用分塊數組(CArray)或可擴展數組(EArray)。這可能是1個字符的變化,因爲您只需調用create_carray()方法而不是create_array()方法。

import numpy as np 
import tables 
from contextlib import closing 

FILTERS = tables.Filters(complib='zlib', complevel=5) 

data = np.zeros(10**7) 

with closing(tables.open_file('compressed', mode='w', filters=FILTERS)) as hdf: 
    hdf.create_carray('/', 'array', obj=data) 

with closing(tables.open_file('uncompressed', mode='w')) as hdf: 
    hdf.create_array('/', 'array', obj=data) 
+0

謝謝你的回答。我沒有在doc中關注這一刻。 – qweqwegod

+0

很高興能幫到你! –