__init__在 Python 3 的 unpickling 期间,是否有足够短的方法来调用类的构造函数?这样做的通常方法是__getinitargs__像这样使用
from __future__ import print_function
import pickle
class Car:
def __init__(self, model, number):
self.model = model
self.number = number
print("constructed with", model, number)
# many other things to do
def __getstate__(self):
# intentionally returns None
pass
def __setstate__(self, state):
pass
def __getinitargs__(self):
# save some information when pickling
# (will be passed to the constructor upon unpickling)
return self.model, self.number
c = Car("toyota", 1234)
d = pickle.loads(pickle.dumps(c))
print("reconstructed with", d.model, d.number)
Run Code Online (Sandbox Code Playgroud)
但是, …
有没有方法将numpy memmap数组保存到.npy文件中?显然,有一种方法可以从.npy文件中加载这样的数组,如下所示
data = numpy.load("input.npy", mmap_mode='r')
Run Code Online (Sandbox Code Playgroud)
但刷新文件并不等同于以.npy格式存储它.
如果冲洗是唯一的方法,那么有没有办法推断存储阵列的形状?我更喜欢在另一个脚本中自动存储和检索动态形状(可能再次作为memmap).
我在各个地方搜索过这个,但没有找到任何结果.我.npy现在的存储方式是
numpy.save(output.filename, output.copy())
Run Code Online (Sandbox Code Playgroud)
这违背了使用memmap但保留形状的想法.
注意:我知道hdf5和h5py,但我想知道是否有一个纯粹的numpy解决方案.