Python中的numpy.save()和joblib.dump()有什么区别?

时间:2014-11-05 20:34:05

标签: python numpy pickle joblib

我在Python中保存了很多离线模型/矩阵/数组,并遇到了这些函数。有人可以通过列出numpy.save()和joblib.dump()的优点和缺点来帮助我吗?

1 个答案:

答案 0 :(得分: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存储到磁盘,或者(用于压缩)存储一个zip文件。此外,joblib.dump存储NDArrayWrapper(或ZNDArrayWrapper进行压缩),这是一个轻量级对象,用于存储带有数组内容的save / zip文件的名称,以及阵列。