<p>我会使用<code>dill</code>来序列化结果……或者如果你想要一个缓存函数,你可以使用<code>klepto</code>来缓存函数调用,这样就可以最大限度地减少对函数的重新评估。在</p>
<pre><code>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. ]])
>>>
</code></pre>
<p>继续</p>
^{pr2}$
<p>获取<code>klepto</code>和{<cd1>}这里:<a href="https://github.com/uqfoundation" rel="nofollow">https://github.com/uqfoundation</a></p>