2014-11-05 181 views

回答

1

下面是代码joblib的关键部分,应该说明一些情况。

def _write_array(self, array, filename): 
    if not self.compress: 
     self.np.save(filename, array) 
     container = NDArrayWrapper(os.path.basename(filename), 
            type(array)) 
    else: 
     filename += '.z' 
     # Efficient compressed storage: 
     # The meta data is stored in the container, and the core 
     # numerics in a z-file 
     _, init_args, state = array.__reduce__() 
     # the last entry of 'state' is the data itself 
     zfile = open(filename, 'wb') 
     write_zfile(zfile, state[-1], 
          compress=self.compress) 
     zfile.close() 
     state = state[:-1] 
     container = ZNDArrayWrapper(os.path.basename(filename), 
             init_args, state) 
    return container, filename 

基本上,joblib.dump可以任选地与numpy.save压缩阵列,它要么存储到磁盘,或(对于压缩)存储压缩文件。此外,joblib.dump存储NDArrayWrapper(或用于压缩的ZNDArrayWrapper),它是一个轻量级对象,用于存储包含数组内容的save/zip文件的名称以及数组的子类。