python - 有没有办法 pickle scipy.interpolate.Rbf() 对象?

标签 python scipy pickle

我正在为相当大的数据集创建径向基函数插值模型。主调用 `scipy.interpolate.Rbf(,) 大约需要一分钟和 14 GB 的 RAM。 因为不是每台应该运行的机器都能做到这一点,而且由于程序会经常在同一个数据集上运行,所以我想将结果保存到一个文件中。这是一个简化的例子:

import scipy.interpolate as inter
import numpy as np
import cPickle

x = np.array([[1,2,3],[3,4,5],[7,8,9],[1,5,9]])
y = np.array([1,2,3,4])

rbfi = inter.Rbf(x[:,0], x[:,1], x[:,2], y)

RBFfile = open('picklefile','wb')
RBFpickler = cPickle.Pickler(RBFfile,protocol=2)
RBFpickler.dump(rbfi)
RBFfile.close()

RBFpickler.dump()调用结果为 can't pickle <type 'instancemethod'>错误。 据我了解,这意味着其中某处有一个方法(好吧,rbfi() 是可调用的),并且由于某种我不太明白的原因不能被 pickle 。

有谁知道用其他方式 pickle 它或保存 inter.Rbf() 的结果的方法吗?以其他方式调用?

那里有一些形状为 (nd,n) 和 (n,n) 的数组(rbfi.Arbfi.xirbfi.di ...),我假设它们存储了所有有趣的信息。我想我可以只 pickle 那些数组,但我不确定如何将对象再次组合在一起......

编辑: 附加约束:我不允许在系统上安装附加库。我可以包含它们的唯一方法是如果它们是纯 Python,我可以将它们包含在脚本中而无需编译任何东西。

最佳答案

我会使用 dill 来序列化结果……或者如果你想要一个缓存函数,你可以使用 klepto 来缓存函数调用,这样你就可以最小化重新评估功能。

Python 2.7.6 (default, Nov 12 2013, 13:26:39) 
[GCC 4.2.1 Compatible Apple Clang 4.1 ((tags/Apple/clang-421.11.66))] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import scipy.interpolate as inter
>>> import numpy as np
>>> import dill
>>> import klepto
>>> 
>>> x = np.array([[1,2,3],[3,4,5],[7,8,9],[1,5,9]])
>>> y = np.array([1,2,3,4])
>>> 
>>> # build an on-disk archive for numpy arrays,
>>> # with a dictionary-style interface  
>>> p = klepto.archives.dir_archive(serialized=True, fast=True)
>>> # add a caching algorithm, so when threshold is hit,
>>> # memory is dumped to disk
>>> c = klepto.safe.lru_cache(cache=p)
>>> # decorate the target function with the cache
>>> c(inter.Rbf)
<function Rbf at 0x104248668>
>>> rbf = _
>>> 
>>> # 'rbf' is now cached, so all repeat calls are looked up
>>> # from disk or memory
>>> d = rbf(x[:,0], x[:,1], x[:,2], y)
>>> d
<scipy.interpolate.rbf.Rbf object at 0x1042454d0>
>>> d.A
array([[ 1.        ,  1.22905719,  2.36542472,  1.70724365],
       [ 1.22905719,  1.        ,  1.74422655,  1.37605151],
       [ 2.36542472,  1.74422655,  1.        ,  1.70724365],
       [ 1.70724365,  1.37605151,  1.70724365,  1.        ]])
>>> 

继续……

>>> # the cache is serializing the result object behind the scenes
>>> # it also works if we directly pickle and unpickle it
>>> _d = dill.loads(dill.dumps(d))
>>> _d
<scipy.interpolate.rbf.Rbf object at 0x104245510>
>>> _d.A
array([[ 1.        ,  1.22905719,  2.36542472,  1.70724365],
       [ 1.22905719,  1.        ,  1.74422655,  1.37605151],
       [ 2.36542472,  1.74422655,  1.        ,  1.70724365],
       [ 1.70724365,  1.37605151,  1.70724365,  1.        ]])
>>>

在这里获取 kleptodill:https://github.com/uqfoundation

关于python - 有没有办法 pickle scipy.interpolate.Rbf() 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23997431/

相关文章:

python - xy点的测量结构-python

python - Numpy .A1 数组并不总是存在

python - 保存和加载 scikit-learn 机器学习模型和函数

Python 版本混在一起。请求特定版本会启动意外版本

python - scapy sr 函数不返回答案

python - 检索对象时包括整个外键对象

python - 在 python 中 pickle 时出现 EOF 错误

python - 问题是生成一个带有字典的表。为什么键和值使用方括号和大括号?

python - matlab 的 fminsearch 的 numpy/scipy 模拟

tensorflow - 如何通过 tensorflow 的 tf.data API 加载 pickle 文件