4

如何保存 scipy.stats 分布?

例如:

a = [scipy.stats.norm(0,1), scipy.stats.norm(0,2)]
with open("distro.pickle", 'w') as f:
    pickle.dump(a, f)

这样做我得到一个TypeError: can't pickle instancemethod objects

4

1 回答 1

7

他们不支持酸洗。“解决”问题的更简单方法是腌制参数,并在取消腌制时创建一个新对象:

>>> from collections import namedtuple
>>> Norm = namedtuple('Norm', 'mu variance')
>>> def pickle_norm(n):
...     return pickle.dumps(Norm(*n.args))
... 
>>> def unpickle_norm(s):
...     return scipy.stats.norm(*pickle.loads(s))
... 
>>> s = pickle_norm(scipy.stats.norm(5, 10))
>>> d = unpickle_norm(s)
于 2013-01-09T13:11:07.297 回答