有没有办法腌制 scipy.interpolate.Rbf() 对象?

Zak*_*Zak 5 python pickle scipy

我正在为相当大的数据集创建径向基函数插值模型。主要调用 `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()
Run Code Online (Sandbox Code Playgroud)

RBFpickler.dump()调用导致can't pickle <type 'instancemethod'>错误。据我了解,这意味着那里有一个方法(好吧,rbfi()是可调用的),并且由于某种我不太明白的原因不能腌制。

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

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

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

Mik*_*rns 2

我会用来dill序列化结果\xe2\x80\xa6,或者如果你想要一个缓存函数,你可以使用它klepto来缓存函数调用,这样你就可以最大限度地减少函数的重新计算。

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

继续\xe2\x80\xa6

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

获取klepto这里dill: https: //github.com/uqfoundation

\n