简体   繁体   English

如何pickle scipy.stats发行版(不能pickle instancemethod对象)

[英]How to pickle a scipy.stats distribution (can't pickle instancemethod objects)

How can I save a scipy.stats distribution? 如何保存scipy.stats发行版?

For example: 例如:

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

Doing this I get a TypeError: can't pickle instancemethod objects 这样做我得到一个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: “解决”你的问题的更简单方法是挑选参数,并在unpickling时创建一个新对象:

>>> 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 scipy.stats 中获取分布模式 - How to get the mode of distribution in scipy.stats scipy.stats 中的毛刺分布 - Burr distribution in scipy.stats 为了泡菜,如何在不修改原始类的情况下删除instancemethod对象 - How to remove instancemethod objects, for the sake of pickle, without modifying the original class 如何从 scipy.stats 对象获取分发类类型? - How to get distribution class type from scipy.stats object? 如何在scipy.stats中指定泊松分布的尾值? - How to specify tail value of a Poisson distribution in scipy.stats? 如何使用列表设置scipy.stats分发的参数 - How to set parameters for scipy.stats distribution with a list 多处理:池和泡菜错误—泡菜错误:不能泡菜 <type 'instancemethod'> :属性查找__builtin __。instancemethod失败 - Multiprocessing: Pool and pickle Error — Pickling Error: Can't pickle <type 'instancemethod'>: attribute lookup __builtin__.instancemethod failed python cPickle.PicklingError:无法腌制 <type 'instancemethod'> 我知道原因,但不知道如何解决 - python cPickle.PicklingError: Can't pickle <type 'instancemethod'> .I know the reason, but I don't know how to solve it 泡菜不能泡一个命名元组 - Pickle can't pickle a namedtuple 使用泡菜时发生错误,TypeError:无法泡菜ElementBase对象 - Error when using pickle, TypeError: can't pickle ElementBase objects
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM