本文介绍了如何腌制scipy.stats分布(不能腌制instancemethod对象)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何保存scipy.stats分发?
How can I save a scipy.stats distribution?
例如:
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
推荐答案
它们不支持酸洗.解决问题的更简单方法是对参数进行腌制,并在解腌时创建一个新对象:
They do not support pickling. The easier way to "solve" your problem is to pickle the arguments and, when unpickling, create a new object:
>>> 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)
这篇关于如何腌制scipy.stats分布(不能腌制instancemethod对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!